Gaurav Arora
Gaurav Arora

Reputation: 8362

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?

Upvotes: 7

Views: 6849

Answers (2)

yaircarreno
yaircarreno

Reputation: 4267

Absolutely, the best solution for that is using a custom viewpager like https://stackoverflow.com/a/34524240/3232778

enter image description here

I hope it will help you.

Upvotes: 0

Maciej Boguta
Maciej Boguta

Reputation: 1374

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

Upvotes: 1

Related Questions