Created
September 25, 2014 20:49
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
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
How you solved the problem?
E/InputEventReceiver(30633): Exception dispatching input event.
When I scroller down and up the listview, throws this exception.