Search code examples
androidandroid-listviewandroid-custom-viewviewgroup

ListView not visible in ViewPager in custom ViewGroup


I have a custom ViewGroup that has a ViewPager as a child. This is because I want to add a static header to the ViewPager and mess with onDraw() to make the header scroll vertically with the ViewPager content. My ViewPager has a ListView as a child. I'm able to create the ListView and the adapter and even set the adapter to the ListView. When I read wallList.getAdapter().getItemAt(0).toString() it returns the data I would expect it to return. But for some reason, I can't see the ListView at all.

The ViewPager still works as intended and the other four pages have arbitrary text for the time being. But the center page which is supposed to contain the ListView shows a blank screen. I can't figure out what's going on, but it sounds similar to a problem I had before here and was able to hack my way to get something acceptable, but wasn't able to answer my original question.

It seems like you can only call setAdapter() in certain places, not only does it have to be on the UI thread (I think) but it seems to have trouble doing it within certain methods, custom or overrided. Here's my custom ViewGroup code.

public class CustomProfilePager extends ViewGroup{

Bitmap coverPhoto, profilePhoto;
Paint coverStyle, profileStyle;
String name;
int coverHeight;
ViewPager pager;
ProfilePagerAdapter pagerAdapter;
ListView wallList;
Context context;

public CustomProfilePager(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context = context;
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    Log.i("CustomPager", "calling onLayout()");
    pager.layout(0, coverHeight+240, getWidth(), getHeight());
//      for(int i=0; i<getChildCount(); i++){
//          getChildAt(i).layout(l, t, r, b);
//      }
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

@Override
protected void onDraw(Canvas canvas) {
    Log.i("CustomPager", "calling onDraw()");
    super.onDraw(canvas);
    if(coverPhoto!=null){
        canvas.drawBitmap(coverPhoto, 0, 0, coverStyle);
    }
}

public void init(String name){
    Log.i("CustomPager", "calling init()");
    this.name = name;
    coverStyle = new Paint();
    coverHeight = (int) (getWidth()/2.7);
    profileStyle = new Paint();
    wallList = new ListView(context);
    pagerAdapter = new ProfilePagerAdapter();
    pager = new ViewPager(context);
    pager.setAdapter(pagerAdapter);
    addView(pager);
    pager.setCurrentItem(2);
    pager.setOffscreenPageLimit(4);
}

public void setCoverPhoto(Bitmap bitmap){
    Log.i("CustomPager", "calling setCoverPhoto()");
    int initialWidth = bitmap.getWidth();
    int initialHeight = bitmap.getHeight();
    int finalHeight = (int) (initialWidth/2.7);
    int initialYoffset = (int) (initialHeight-finalHeight)/2;
    this.coverPhoto = Bitmap.createBitmap(bitmap, 0, initialYoffset, bitmap.getWidth(), finalHeight);
    invalidate();
}

public void setProfilePhoto(Bitmap bitmap){
    this.profilePhoto = bitmap;
}

public ViewPager getViewPager(){
    return pager;
}

public void setWallAdapter(Profile.WallAdapter adapter){
    Log.i("CustomPager", "calling setWallAdapter()");
    wallList.setAdapter(adapter);
}

public class ProfilePagerAdapter extends PagerAdapter {
    @Override
    public void destroyItem(ViewGroup collection, int position, Object view) {
        ((ViewPager) collection).removeView((View) view);
    }
    @Override
    public Object instantiateItem(ViewGroup collection, int position) {
        ViewPager parent = (ViewPager) collection;
        switch (position) {
        case 2: // Wall
            wallList.setDividerHeight(0);
            parent.addView(wallList);
            return wallList;
        default:
            TextView testText = new TextView(context);
            testText.setText(String.valueOf(position) + ": " + name);
            testText.setTextSize(46);
            testText.setGravity(Gravity.CENTER);
            parent.addView(testText);
            return testText;
        }
    }
    @Override
    public int getCount() {
        return 5;
    }
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
}

}


Solution

  • The problem wasn't really obvious and I'm not sure exactly what caused it in the first place. All I had to do was sublass FrameLayout instead of ViewGroup and everything works.