如何解决使 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 举报,一经查实,本站将立刻删除。