Question

I'm trying to create a ListView that each cell can be shifted as ViewPager. Similar to Google Gmail app, that can shift emails in order to delete the emails. It is working BUT showing nothing.

I created a ListView with BaseAdapter. The Adapter create ViewPager with PagerAdapter that implements FragmentStatePagerAdapter. The PagerAdapter activate the Fragment that supposed to show the data at the cells in the pagers. Can you please help?

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;
        }
    }
}

XML for the ListView myList1.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 for the Pager 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>

The list unit 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>
Was it helpful?

Solution

ViewPager in ListView is a bad idea.

You can use this Swipe-to-Dismiss library for deleting https://github.com/romannurik/android-swipetodismiss

OTHER TIPS

You go through following link to implement gmail like delete from list function:

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top