3

I have a ViewPager and use it to swipe between views not Fragments . And when I give the View Pager wrap_content height , it doesn't show anything . So I had to give it a fixed height . But I had another problem , when the item's height is larger than the fixed one , the view doesn't be shown correctly (And I use TextView as the View) . So what should I do to make the height of the ViewPager is equal to the highest one .

I use PagerAdapter like the code below .

public class IndicatorViewPagerAdapter extends PagerAdapter {
    private Context context;
    public int[] images = {R.drawable.userprofile,R.drawable.userprofile,R.drawable.userprofile,R.drawable.userprofile,R.drawable.userprofile};
    public int[] texts = {R.string.first_text,R.string.second_text,R.string.third_text,R.string.fourth_text,R.string.fifth_text};
    LayoutInflater layoutInflater;
    Typeface typeface;
    String lang;
    public IndicatorViewPagerAdapter(Context context,String lang) {
        this.context = context;
        this.lang=lang;
    }

    @Override
    public int getCount() {
        return texts.length;
    }

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

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.feeds_item_layout,null);
        ImageView imageView = view.findViewById(R.id.image);
        TextView textView = view.findViewById(R.id.desc);
        imageView.setImageResource(images[position]);
        String text = context.getResources().getString(texts[position]);
        Log.e("text"," "+text);
        textView.setText(text);
        //ViewPager viewPager = (ViewPager) container;
        if(lang.equals("en")) {
            typeface = ResourcesCompat.getFont(context, R.font.knowledge_regular);
        }
        else {
            typeface = ResourcesCompat.getFont(context, R.font.thesans_plain);
        }
        textView.setTypeface(typeface);
        container.addView(view);


        return view;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        ViewPager viewPager = (ViewPager) container;
        View view = (View) object;
        viewPager.removeView(view);
    }


}

and Here I use the ViewPager in XML

<LinearLayout
                android:layout_width="match_parent"
                android:layout_height="@dimen/_150sdp"
                android:layout_marginTop="10dp"
                android:background="#85d7d5d6"
                android:orientation="horizontal"
                android:padding="10dp">

                <TextView
                    android:id="@+id/leftArrowTv"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:textColor="@color/arrow_color"
                    android:textSize="@dimen/_20ssp" />

                <androidx.viewpager.widget.ViewPager
                    android:id="@+id/viewPager"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1" />

                <TextView
                    android:id="@+id/rightArrowTv"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:textColor="@color/arrow_color"
                    android:textSize="@dimen/_20ssp" />

            </LinearLayout>
Ahmed Elsayed
  • 231
  • 3
  • 23
  • Any one tell me if my question is not clear – Ahmed Elsayed Mar 30 '21 at 10:44
  • Try providing a parent view for the inflation like this: `View view = layoutInflater.inflate(R.layout.feeds_item_layout, container);` – Cheticamp Apr 02 '21 at 13:10
  • @Cheticamp I already added this line in the code , please see it again – Ahmed Elsayed Apr 02 '21 at 22:06
  • I don't see that line. I see something a line that has `null` as the second argument but not what I suggest with `container` as the second argument. – Cheticamp Apr 02 '21 at 22:38
  • @Cheticamp when I add the container I get this error lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. – Ahmed Elsayed Apr 02 '21 at 23:55
  • Setting `false` as the third parameter to the `inflate()` will probably get rid of that problem but,. pon reflection, I don't think this is where you problem is. – Cheticamp Apr 03 '21 at 13:07
  • Why not using a ScrollView as root element of the ViewPager page view – Zain Apr 03 '21 at 15:51
  • @Zain I use NestedScrollView – Ahmed Elsayed Apr 03 '21 at 19:54
  • [This](https://stackoverflow.com/questions/32330639/dynamic-height-viewpager/32410274#32410274) will certainly help you. I've added this code a while back and it worked like a charm. 1 thing to note though, if you have a TabLayout, you need to ADD it's height to the total, otherwise it'll be that much shorther than it needs to be. – Vucko Apr 05 '21 at 14:36
  • @Vucko Thanks but it's not helping – Ahmed Elsayed Apr 06 '21 at 12:56
  • If you could be slightly more elaborate, it'd help you get this sorted – Vucko Apr 07 '21 at 07:38
  • @Vucko what exactly you need of the code ? – Ahmed Elsayed Apr 07 '21 at 10:45

2 Answers2

0

I don't quite understand why you posted your IndicatorViewPagerAdapter code.

Your adapter code is used to populate data, it has nothing to do with the size of the view, which is the ViewPager.

To me it probably makes more sense to try and customize the viewpager and apply manual resize logic. So I looked for this on stackoverflow, as found a popular stackoverflow post where you even commented on, but it seems you were also confused on how to actually implement it. To be clear, I haven't tested this at all but it seems you were confused by the difference between adapters and views.

public class WrapContentHeightViewPager extends ViewPager {

    public WrapContentHeightViewPager(Context context) {
        super(context);
    }

    public WrapContentHeightViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int height = 0;
        for(int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            int h = child.getMeasuredHeight();
            if(h > height) height = h;
        }

        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

}

Then change your XML to

            <TextView
                android:id="@+id/leftArrowTv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:textColor="@color/arrow_color"
                android:textSize="@dimen/_20ssp" />

            <my.package.name.WrapContentHeightViewPager
                android:id="@+id/viewPager"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fillViewport="true" />

            <TextView
                android:id="@+id/rightArrowTv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:textColor="@color/arrow_color"
                android:textSize="@dimen/_20ssp" />

        </LinearLayout>
Edward van Raak
  • 4,841
  • 3
  • 24
  • 38
  • When I use this function in the class https://stackoverflow.com/a/20784791/11567530 it make the height of the viewPager equal to the highest item , bit I found a problem , if one of the item in the ViewPager is an image , the viewPager's heigh become big because of the image , so how can I fix this problem ? – Ahmed Elsayed Apr 03 '21 at 20:31
  • Are you still there? – Ahmed Elsayed Apr 05 '21 at 13:58
0

I tried your code and it's working with a slight change. You just need to check your ViewPager item's design. ViewPager displays even with wrap_content height. Look at the below code:

@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
    View view = LayoutInflater.from(context).inflate(R.layout.feeds_item_layout,container, false);
    ...
    container.addView(view);
    return view;
}
Krishna Vyas
  • 1,009
  • 8
  • 25