Thursday, 1 April 2021

Making the height of the ViewPager equal to the highest item in the PagerAdapter

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>


from Making the height of the ViewPager equal to the highest item in the PagerAdapter

No comments:

Post a Comment