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.
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.
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 .
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With