使ViewPager的高度等于PagerAdapter中的最高项目 [英] Making the height of the ViewPager equal to the highest item in the PagerAdapter
问题描述
我有一个 ViewPager
,并用它在视图之间浏览,而不是Fragments.当我指定 View Pager
wrap_content高度时,它什么也没有显示.所以我不得不给它一个固定的高度.但是我还有另一个问题,当项目的高度大于固定高度时,视图无法正确显示(并且我将 TextView
用作视图).因此,我应该怎么做才能使 ViewPager
的高度等于最高的高度.
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 .
我使用 PagerAdapter
像下面的代码.
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);
}
}
在这里,我在 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
代码.
I don't quite understand why you posted your IndicatorViewPagerAdapter
code.
您的适配器代码用于填充数据,它与视图的大小无关,即 ViewPager
.
Your adapter code is used to populate data, it has nothing to do with the size of the view, which is the ViewPager
.
对我来说,尝试自定义viewpager并应用手动调整大小逻辑可能更有意义.所以我在stackoverflow上寻找了这个,发现了一个流行的stackoverflow帖子,您甚至在上面发表了评论,但是似乎您也对如何实际实现它感到困惑.需要说明的是,我根本没有测试过,但是看来您对适配器和视图之间的差异感到困惑.
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);
}
}
然后将您的XML更改为
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>
这篇关于使ViewPager的高度等于PagerAdapter中的最高项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!