使 ViewPager 的高度等于 PagerAdapter 中的最高项

如何解决使 ViewPager 的高度等于 PagerAdapter 中的最高项

我有一个 ViewPager 并用它在视图而不是 Fragments 之间滑动。 当我给 View Pager wrap_content height 时,它什么都不显示。所以我不得不给它一个固定的高度。但是我遇到了另一个问题,当项目的高度大于固定高度时,视图无法正确显示(我使用 TextView 作为视图)。那么我该怎么做才能使ViewPager的高度等于最高的一个

我像下面的代码一样使用 PagerAdapter

public class IndicatorViewPagerAdapter extends PagerAdapter {
    private Context context;
    public int[] images = {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);
    }


}

这里我使用了 ViewPager

中的 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>

解决方法

我不太明白你为什么发布 IndicatorViewPagerAdapter 代码。

您的适配器代码用于填充数据,它与视图的大小无关,即 ViewPager

对我来说,尝试自定义 viewpager 并应用手动调整大小逻辑可能更有意义。所以我在 stackoverflow 上寻找了这个,因为找到了一个流行的 stackoverflow 帖子,你甚至在那里发表了评论,但似乎你也对如何实际实现它感到困惑。需要明确的是,我根本没有对此进行过测试,但您似乎对适配器视图之间的区别感到困惑。

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);
    }

}

然后将您的 XML 更改为

            <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>
,

我试过你的代码,它的工作有轻微的变化。您只需要检查您的 ViewPager 项目的设计。即使使用 wrap_content 高度,ViewPager 也能显示。看下面的代码:

@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;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?