I have a View Pager as a list item line, and I set the data for the pager adapter to display as a list. Here is my code:
Layout 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" /> </LinearLayout>
List item list item
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <android.support.v4.view.ViewPager android:id="@+id/mypager" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
ListView GetView
ViewHolder holder; if(rowView==null){ holder = new ViewHolder(); LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); rowView = inflater.inflate(R.layout.horizontal_list_item, parent,false); MyPagerAdapter adapter = new MyPagerAdapter(context); ViewPager myPager = (ViewPager) rowView.findViewById(R.id.mypager); myPager.setAdapter(adapter); myPager.setCurrentItem(4, true); rowView.setTag(holder); } else{ holder = (ViewHolder) rowView.getTag(); }
MyPagerAdapter
@Override public Object instantiateItem(View container, final int position) { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); LinearLayout layout = (LinearLayout) inflater.inflate( R.layout.inner_layout_file, null); TextView tv_view = (TextView) layout.findViewById(R.id.text); tv_view.setText("Example Item " + position); ((ViewPager) container).addView(layout); }); return layout;
Problem . The problem I am facing is that when I capture the height of the View Pager in an XML file, then my code shows the data, otherwise if I set the height of my pager as WRAP_CONTENT or MATCH_PARENT, no views are displayed during code execution on screen.
Why is this so. I do not want to hard code the height of the View Pager.
Please help me with this, let me know if you have any questions?
android height android-viewpager
Gaurav arora
source share