Skip to content

Instantly share code, notes, and snippets.

@ruckus
Created September 25, 2014 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruckus/df82ff441994580ebac2 to your computer and use it in GitHub Desktop.
Save ruckus/df82ff441994580ebac2 to your computer and use it in GitHub Desktop.
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
Copy link

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

@Igorpi25
Copy link

Igorpi25 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