Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of using a ViewPager as a Header in a ListView. We need to explicitly set the layout params of the Header view because its not known at initialization time, in my case I am asynchronously loading images into the ViewPager.
public class ProductDetailActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
final int width = displayMetrics.widthPixels;
mListView = (ListView)findViewById(R.id.product_detail_list_view);
// Initialize the header view
View headerView = (LinearLayout)getLayoutInflater().inflate(R.layout.product_detail_header_row, null);
mFeaturedImagePager = (ViewPager)headerView.findViewById(R.id.image_pager);
mFeaturedImageAdapter = new FeaturedImagesPagerAdapter(getSupportFragmentManager(), mFeaturedImageUrls.size());
mFeaturedImagePager.setAdapter(mFeaturedImageAdapter);
mFeaturedImagePager.setOffscreenPageLimit(2);
mListView.addHeaderView(headerView, null, false);
// The crucial part
AbsListView.LayoutParams headerViewParams = new AbsListView.LayoutParams(width, 400);
headerView.setLayoutParams(headerViewParams);
}
}
@luiszacheu

This comment has been minimized.

Copy link

commented Nov 4, 2014

How you solved the problem?
E/InputEventReceiver(30633): Exception dispatching input event.
When I scroller down and up the listview, throws this exception.

@Igorpi25

This comment has been minimized.

Copy link

commented Feb 5, 2015

Thank you! It solved my problem with GridView inside ListView's header.
1). I noticed that such problem will appear every time when the size(width/height) of layout/widget inside ListView's header is undefined when ListView inittializes itself. Be careful when use inside a header widgets with the custom background drawable.
2). If you have excess area inside header - try paint all layouts and widget (ALL WIDGETS AND LAYOUTS!) inside header in different colors. The cause of excess area might be unexpected widget from completely unexpected place (multilined EditText in my case)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.