使用android.support.v4.view.ViewPager创建ListView什么都不显示

时间:2013-12-12 09:20:14

标签: android listview android-viewpager pager fragmentpageradapter

我正在尝试创建一个ListView,每个单元格都可以作为ViewPager进行转换。 与Google Gmail应用类似,可以移动电子邮件以删除电子邮件。 它正在工作,但什么都没有。

我使用BaseAdapter创建了一个ListView。 适配器使用实现FragmentStatePagerAdapter的PagerAdapter创建ViewPager。 PagerAdapter激活片段,该片段应该在寻呼机的单元格中显示数据。 你能帮忙吗?

package com.tegrity.gui;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MyFregment extends Fragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    /**
     * simple ListView
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.my_view1, container, false);
        ListView mMyListView = (ListView) view.findViewById(R.id.myList1);

        // create the my adapter
        MyAdapter mMyAdapter = new MyAdapter(getActivity());
        mMyListView.setAdapter(mMyAdapter);

        // This is working but without the ListView
        // view = inflater.inflate(R.layout.connect_pager_view, null);
        // android.support.v4.view.ViewPager myPagerUnit =
        // (android.support.v4.view.ViewPager)
        // view.findViewById(R.id.connect_pager);
        // PagerAdapter pagerAdapter = new MyPagerAdapter(getFragmentManager(),
        // 0);
        // myPagerUnit.setAdapter(pagerAdapter);

        return view;
    }

    // the data
    private static ArrayList<String> mMyList0 = new ArrayList<String>();

    /**
     * my adapter
     */
    public class MyAdapter extends BaseAdapter {
        // the data
        private ArrayList<String> mMyList = new ArrayList<String>();

        private Context mContext;

        private LayoutInflater mInflater;

        public MyAdapter(Context context) {
            mContext = context;
            mInflater = LayoutInflater.from(mContext);

            mMyList.add("First line");
            mMyList.add("Second line");
            mMyList.add("Third line");
            mMyList0 = mMyList;
        }

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

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

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // optimization
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.connect_pager_view,
                        null);
            }
            android.support.v4.view.ViewPager myPagerUnit = (android.support.v4.view.ViewPager) convertView
                    .findViewById(R.id.connect_pager);
            PagerAdapter pagerAdapter = new MyPagerAdapter(
                    getFragmentManager(), position);
            myPagerUnit.setAdapter(pagerAdapter);
            myPagerUnit
                    .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                        @Override
                        public void onPageSelected(int position) {
                            ((Activity) mContext).invalidateOptionsMenu();
                        }
                    });

            return convertView;
        }
    }

    /**
     * A simple pager adapter
     */
    class MyPagerAdapter extends FragmentStatePagerAdapter {
        // parameters from the my adapter
        private int mPosition;

        public MyPagerAdapter(FragmentManager fm, int position) {
            super(fm);
            mPosition = position;
        }

        @Override
        public Fragment getItem(int pagePosition) {
            return MyUnitFragment.create(pagePosition, mPosition);
        }

        @Override
        public int getCount() {
            return 2; // pager of 2 cells
        }
    }

    /**
     * my basic unit
     */
    public static class MyUnitFragment extends Fragment {
        public static final String PAGE = "page";
        public static final String POSITION = "position";

        private int mPageNumber;

        // parameter from the my adapter
        private int mPosition;

        /**
         * Factory method for this fragment class. Constructs a new fragment for
         * the given page number.
         */
        public static MyUnitFragment create(int pageNumber, int position) {
            MyUnitFragment fragment = new MyUnitFragment();
            Bundle args = new Bundle();
            args.putInt(PAGE, pageNumber);
            args.putInt(POSITION, position);
            fragment.setArguments(args);
            return fragment;
        }

        public MyUnitFragment() {
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mPageNumber = getArguments().getInt(PAGE);
            mPosition = getArguments().getInt(POSITION);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // Inflate the layout containing a title and body text.
            View convertView = (View) inflater.inflate(R.layout.bookmark_unit,
                    container, false);

            // page parts
            String data = mMyList0.get(mPosition);

            TextView textView = (TextView) convertView
                    .findViewById(R.id.bookmarkText1);

            switch (mPageNumber) {
            case 0: {
                textView.setText(data + " at the first page");
                break;
            }
            case 1: {
                textView.setText(data + " at the second page");
                break;
            }
            }

            return convertView;
        }

        /**
         * Returns the page number represented by this fragment object.
         */
        public int getPageNumber() {
            return mPageNumber;
        }
    }
}

ListView myList1.xml的XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:orientation="vertical"
    android:background="@color/white" >

    <ListView android:id="@+id/myList1"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:divider="@drawable/course_divider"
        android:dividerHeight="2dp"
        android:cacheColorHint="#00000000" >
    </ListView>

</LinearLayout>

寻呼机的XML connect_pager_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/connect_pager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
</android.support.v4.view.ViewPager>

列表单元my_unit.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView android:id="@+id/myText1" 
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textColor="@color/black"
        android:layout_marginLeft="6dp">
    </TextView> 

</LinearLayout>

2 个答案:

答案 0 :(得分:1)

ListView中的ViewPager是个坏主意。

您可以使用此Swipe-to-Dismiss库删除https://github.com/romannurik/android-swipetodismiss

答案 1 :(得分:0)

您可以通过以下链接实现gmail,例如从列表功能中删除:

https://github.com/47deg/android-swipelistview