如何在Android

时间:2015-10-19 10:32:06

标签: android listview android-listview android-viewpager

我正在尝试构建一个需要在ListView中显示ViewPager的应用程序。这类似于FlipKart或SnapDeal应用程序。

虽然我不确定这是他们所做的,所以我只是猜测这可能已经实施了。所以,如果有更好的解决方案,那么我当然愿意接受它。

我跟着this linkthis link,我到目前为止所做的事情如下: -

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView listView= (ListView) findViewById(R.id.listView);
        ArrayList<MyPagerAdaper> adapers=new ArrayList<>();
        for(int i=0; i<10; i++){
            ArrayList<String> list=getNewList();
            MyPagerAdaper pagerAdaper=new MyPagerAdaper(list);
            adapers.add(pagerAdaper);
        }
        MyListAdapter listAdapter=new MyListAdapter(adapers);
        listView.setAdapter(listAdapter);
    }

    private ArrayList<String> getNewList() {
        ArrayList<String> list=new ArrayList<>();
        list.add("Pankaj");
        list.add("Manisha");
        list.add("Pari");
        list.add("Piggy");
        list.add("Harish");
        return list;
    }

    private class MyListAdapter extends BaseAdapter{
        private ArrayList<MyPagerAdaper> adapers;
        private LayoutInflater inflater;

        public MyListAdapter(ArrayList<MyPagerAdaper> adapers) {
            this.adapers = adapers;
            inflater=LayoutInflater.from(getBaseContext());
        }

        @Override
        public int getCount() {
            return adapers.size();
        }

        @Override
        public Object getItem(int position) {
            return adapers.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view=convertView;
            Viewholder viewholder;
            if(view==null){
                view=inflater.inflate(R.layout.layout_list_item, parent, false);
                viewholder=new Viewholder();
                viewholder.pager= (ViewPager) view.findViewById(R.id.viewPager);
                view.setTag(viewholder);
            }else {
                viewholder= (Viewholder) view.getTag();
            }
            MyPagerAdaper adaper= (MyPagerAdaper) getItem(position);
            viewholder.pager.setAdapter(adaper);
            return view;
        }
        private class Viewholder{
            ViewPager pager;
        }
    }

    private class MyPagerAdaper extends PagerAdapter{
        private ArrayList<String> list;
        private LayoutInflater inflater;

        public MyPagerAdaper(ArrayList<String> list) {
            this.list = list;
            inflater=LayoutInflater.from(getBaseContext());
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view=inflater.inflate(R.layout.layout_page, container, false);
            TextView textView= (TextView) view.findViewById(R.id.textView);
            String s=list.get(position);
            textView.setText(s);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view.equals(object);
        }
    }
}

我面临的问题是屏幕上没有任何内容,除了操作栏顶部的细灰色条。

EDIT1:
对不起,我错过了getCount()方法。我已经做了更正,但问题仍然存在。

EDIT2:
我在更改后更新了上面的代码。

EDIT3:
这就是屏​​幕的样子。 enter image description here 任何帮助是极大的赞赏。提前谢谢。

1 个答案:

答案 0 :(得分:0)

在MyPagerAdaper内部,更改此方法

@Override
    public int getCount() {
        return 0;
    }

@Override
    public int getCount() {
        return list.size();
    }

和Inside instantiateItem方法将视图添加到容器中,如下所示:

 @Override
    public Object instantiateItem(ViewGroup container, int position) {
.
.  
.
container.addView(view);
return view;
}