Listview 中如何嵌套viewpager

twilighth 发布于 2016/05/06 20:28
阅读 572
收藏 0

问题状况:listview中可以无法显示Viewpager的控件。

这是fragment的 代码

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.home_fragment,null); home_list_view=(ListView)view.findViewById(R.id.home_list_view); viewPager=(ViewPager)view.findViewById(R.id.home_view_pager2); 
mViews.clear(); 
//初始化Listview item 的view  for(int i=0;i<10;i++){
        View mView=LayoutInflater.from(getActivity()).inflate(R.layout.home_list_item_pager,null); mViews.add(mView);
    }
mmViews.clear(); 
//初始化viewpager item 的view  for (int j=0;j<5;j++){
        View mmView=LayoutInflater.from(getActivity()).inflate(R.layout.home_list_pager_item,null); mmViews.add(mmView);
    }
    MyListAdapter myListAdapter=new MyListAdapter(mViews,mImage,getActivity()); home_list_view.setAdapter(myListAdapter); return view;
}
这是Listview adapter 


public MyListAdapter(List<View> mViews, int[] mImage, Context mContext) { this.mViews = mViews; this.mImage = mImage; this.mContext = mContext;
    } @Override  public int getCount() { return mViews.size();
    } @Override  public Object getItem(int i) { return 2;
    } @Override  public long getItemId(int i) { return i;
    } @Override  public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder mViewHolder = null; if (view == null) {
            mViewHolder = new ViewHolder();
            view = mViews.get(i);
            mViewHolder.textView = (TextView) view.findViewById(R.id.tv1);
            mViewHolder.viewPager=(ViewPager)view.findViewById(R.id.home_view_pager);
            view.setTag(mViewHolder);
        } else {
            mViewHolder = (ViewHolder) view.getTag();
        }
        mViewHolder.viewPager.setAdapter(new MyPagerAdapter(HomeFragment.mmViews)); return view;
    } private class ViewHolder {
        ViewPager viewPager;
        TextView textView;
    }
}


这是viewpager的adapter

public class MyPagerAdapter extends PagerAdapter{ private List<View>mViews=new ArrayList<View>(); public MyPagerAdapter(List<View>mViews){ this.mViews= mViews;
    } @Override  public int getCount() { return mViews.size();
    } @Override  public void setPrimaryItem(ViewGroup container, int position, Object object) {

    } @Override  public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(mViews.get(position));
    } @Override  public Object instantiateItem(ViewGroup container, int position) {
        container.addView(mViews.get(position)); return mViews.get(position);
    } @Override  public boolean isViewFromObject(View view, Object object) { return view==object;
    }
}

加载中
0
w
wanglinbo123
Listveiw 自定义一个 确保完全显示onmesure和 滑动冲突
返回顶部
顶部