我有ViewGroup
一个ViewPager
小时候的习俗。这是因为我想在其中添加一个静态标题ViewPager
并弄乱标题onDraw()
以使标题随ViewPager
内容垂直滚动。我小时候ViewPager
有一个。ListView
我能够创建ListView
和 适配器,甚至可以将适配器设置为ListView
. 当我阅读wallList.getAdapter().getItemAt(0).toString()
它时,它会返回我希望它返回的数据。但由于某种原因,我根本看不到ListView
。
仍然按预期工作,ViewPager
其他四页暂时有任意文本。但是应该包含的中心页面ListView
显示一个空白屏幕。我不知道发生了什么,但这听起来与我之前在这里遇到的问题相似,并且能够破解我的方式以获得可接受的东西,但无法回答我原来的问题。
似乎您只能setAdapter()
在某些地方调用,不仅必须在 UI 线程上(我认为),而且在某些自定义或覆盖的方法中似乎很难做到这一点。这是我的自定义 ViewGroup 代码。
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;
}
}
}