Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set View Pager Height Dynamically

I have View Pager as a List Item row, and I am setting data to the pager's adapter to show in the list view.Here is my code :

Pager_Item Layout

<?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 View Row 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 which I am facing is that when I fix the height of the View Pager in XML file, then my code shows the data, else if I set my pager's height as WRAP_CONTENT or MATCH_PARENT, no views are shown at the runtime of the code on the screen.

Why it is behaving like this. I donot want to hard code the height of the View Pager.

Please help me in this, let me know if you have any queries?

like image 509
Gaurav Arora Avatar asked Nov 22 '13 09:11

Gaurav Arora


1 Answers

in your instantiate item, use container which is your View Pager, to set layout params on it

like image 162
Maciej Boguta Avatar answered Nov 15 '22 01:11

Maciej Boguta