Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View Pager as List View Row Item

I have a list view with 20 rows and I want to setup a view pager as each row in a list view. As the items coming in my row of the list view may one or more than one, and I want to show the list view row items using a view pager.

For this I am using the following code:

Custom Layout which is to be shown in the list view row (as the pager item)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/inner_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginRight="6.0dip"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:text="Example Value"
        android:textAppearance="?android:textAppearanceMedium" />

Main List View Custom Row Item having View Pager

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


         <android.support.v4.view.ViewPager
        android:id="@+id/mypager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

MyPagerAdapter Class

public class MyPagerAdapter extends PagerAdapter {

    private Context context;

    public MyPagerAdapter(Context context) {
        this.context = context;
    }

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

    @Override
    public Object instantiateItem(View container, int position) {
        LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout view = inflater.inflate(R.layout.inner_layout_file, null);
        ((ViewPager) container).addView(view, position);
        return view;
    }

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

    @Override
    public void destroyItem(View container, int position, Object object) {
        // TODO Auto-generated method stub
        ((ViewPager) container).removeView((View) object);
    }
}

List View Adapter GET VIEW METHOD

@Override
public View getView(int position, View convertView, ViewGroup parent) {


    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.horizontal_list_item, null,false);

    MyPagerAdapter adapter = new MyPagerAdapter(context);
    ViewPager myPager = (ViewPager) convertView.findViewById(R.id.mypager);
    myPager.setAdapter(adapter);

    myPager.setCurrentItem(0);

    return convertView;
}

I am getting no error and the view which is rendered after running it is always empty.

like image 658
Gaurav Arora Avatar asked Nov 14 '13 10:11

Gaurav Arora


People also ask

What is a View Pager?

ViewPager is a layout manager that allows the user to flip left and right through pages of data. It is mostly found in apps like Youtube, Snapchat where the user shifts right – left to switch to a screen. Instead of using activities fragments are used.

How View Pager works?

ViewPager uses PagerAdapter objects as a supply for new pages to display, so the PagerAdapter will use the fragment class that you created earlier. Create an activity that does the following things: Sets the content view to be the layout with the ViewPager .

How ViewPager works in Android?

ViewPager - How It Works The idea is that ViewPager works with a PageAdapter to supply the Views that represent each page. The basic idea is that ViewPager keeps track of what page should be visible on the screen and then asks the PagerAdapter to get the View hierarchy that needs to be displayed.

Why use ViewPager?

ViewPager in Android is a class that allows the user to flip left and right through pages of data. This class provides the functionality to flip pages in app. It is a widget found in the support library. To use it you'll have to put the element inside your XML layout file that'll contain multiple child views.


2 Answers

I have faced the same issue and resolved it by providing id to the viewpager dynamically in the getView() method.

@Override
public View getView(int position, View convertView, ViewGroup parent) {


    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.horizontal_list_item, null,false);

    MyPagerAdapter adapter = new MyPagerAdapter(context);
    ViewPager myPager = (ViewPager) convertView.findViewById(R.id.mypager);

    **myPager.setId(position + getCount());**

    myPager.setAdapter(adapter);

    myPager.setCurrentItem(0);

    return convertView;
}

Hope this may help you.

like image 138
Anand Suthar Avatar answered Oct 21 '22 23:10

Anand Suthar


I faced same problem. Just give height to both viewpager and if needed your List View Row layout's main container.

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


         <android.support.v4.view.ViewPager
        android:id="@+id/mypager"
        android:layout_width="match_parent"
        android:layout_height="40dip" />

</RelativeLayout>

Height must be set by fix value. If you want to make it dynamic you can try from setting it in your coding at run time.

like image 3
Dharmik Avatar answered Oct 21 '22 23:10

Dharmik