android – 在listview中添加viewpager作为滚动标题

前端之家收集整理的这篇文章主要介绍了android – 在listview中添加viewpager作为滚动标题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在列表标题添加Viewpager(使用支持库4),但它没有显示任何内容.这是我的代码请帮忙.它将在列表标题中不作为项目,因此它不应该是一个问题.
public class CustomActivity extends ListActivity {

    private static int NUM_AWESOME_VIEWS = 20;
    private Context cxt;
    private CustomPageAdapter pageAdapter;

        /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String[] items = { "this","is","my","list","view","data" };
        this.cxt = this;

        ViewPager viewPager =  new ViewPager(this);

        pageAdapter = new CustomPageAdapter();
        viewPager.setAdapter(pageAdapter);
        viewPager.requestLayout();

        getListView().addHeaderView(viewPager);
        setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,items));

    }

    private class CustomPageAdapter extends PagerAdapter{


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

        /**
         * Create the page for the given position.  The adapter is responsible
         * for adding the view to the container given here,although it only
         * must ensure this is done by the time it returns from
         * {@link #finishUpdate()}.
         *
         * @param container The containing View in which the page will be shown.
         * @param position The page position to be instantiated.
         * @return Returns an Object representing the new page.  This does not
         * need to be a View,but can be some other container of the page.
         */
            @Override
            public Object instantiateItem(View collection,int position) {
                    TextView tv = new TextView(cxt);
                    tv.setText("Bonjour PAUG " + position);
                    tv.setTextColor(Color.WHITE);
                    tv.setTextSize(20);

                    ((ViewPager) collection).addView(tv,0);

                    return tv;
            }

        /**
         * Remove a page for the given position.  The adapter is responsible
         * for removing the view from its container,although it only must ensure
         * this is done by the time it returns from {@link #finishUpdate()}.
         *
         * @param container The containing View from which the page will be removed.
         * @param position The page position to be removed.
         * @param object The same object that was returned by
         * {@link #instantiateItem(View,int)}.
         */
            @Override
            public void destroyItem(View collection,int position,Object view) {
                    ((ViewPager) collection).removeView((TextView) view);
            }



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


        /**
         * Called when the a change in the shown pages has been completed.  At this
         * point you must ensure that all of the pages have actually been added or
         * removed from the container as appropriate.
         * @param container The containing View which is displaying this adapter's
         * page views.
         */
            @Override
            public void finishUpdate(View arg0) {

            }


            @Override
            public void restoreState(Parcelable arg0,ClassLoader arg1) {

            }

            @Override
            public Parcelable saveState() {
                    return null;
            }

            @Override
            public void startUpdate(View arg0) {

            }

}

}

解决方法

listadapter之后的listview页眉和页脚显示.

如果你尝试setadapter,并且看不到viewpager.检查viewpager的宽度和高度.

如果viewpager的宽度或高度值为0.
在viewgroup中创建LienarLayout(或anyViewGroup)和addview viewpager,其中layoutparame包括width和height设置.和addHeader viewgroup.

我的英语技能不好,如果不明白,请加我评论.

我希望有所帮助.

原文链接:https://www.f2er.com/android/318519.html

猜你在找的Android相关文章