【问题标题】:Scrollable PagerAdapter可滚动的 PagerAdapter
【发布时间】:2018-11-26 05:57:59
【问题描述】:

我做了一个PagerAdapter,效果很好,但是当我将手机水平放置时,很多像TextView 这样的东西是不可见的,因为它们在手机屏幕的“下方”。我知道我可以输入ScrollView,但是这个选项会产生很多错误。

除了ScrollView,还有其他选择吗?或者这个错误是什么意思?

这是我的代码,LinearLayout 没有任何错误,但 PagerAdapter 不可滚动。

Java 代码:

public class SlideAdapter_info extends PagerAdapter {
    Context context;
    LayoutInflater inflater;

    public SlideAdapter_info(Context context){
        this.context = context;
    }

    @Override
    public int getCount() {
        return 1;
    }

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

    @Override
    public void destroyItem(ViewGroup container, int position, Object object{
        container.removeView((LinearLayout)object);
    }

    @NonNull
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        inflater = (LayoutInflater) 
        context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.slide_info, container, false);
        //other code to set the textviews and the imageview
        container.addView(view);
        return view;
    }
}

xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:id="@+id/slidelinear_info"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:gravity="center"
     android:orientation="vertical">

<TextView /> ....

<!-- This allows me to have a circle shaped image-->
<de.hdodenhof.circleimageview.CircleImageView 
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/slideimg"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:paddingTop="10dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    app:civ_border_color="@color/black"
    app:civ_border_width="3dp" />

    <TextView /> ....

    </LinearLayout>

这是结果:(水平)

这是结果:(垂直)

如果我用ScrollView代替LinearLayoutAndroid Studio给我这个错误:

java.lang.IllegalStateException: ScrollView can host only one direct child

at esame.progetto.xhondar.github.com.info.SlideAdapter_info.instantiateItem(SlideAdapter_info.java:87)

这是行: View view = inflater.inflate(R.layout.slide_info, container, false);

【问题讨论】:

  • 这是如何放置图片的语法:"![](image)"

标签: android xml android-studio scrollview android-pageradapter


【解决方案1】:

java.lang.IllegalStateException: ScrollView can host only one direct child 准确地告诉你你的问题是什么。只需将您的LinearLayout 转换为ScrollView,您就有多个孩子。如果您希望当前布局在 ViewPager 中可滚动,则需要在布局 xml 文件中将 LinearLayoutScrollView 包装起来。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:layout_height="match_parent">
     <LinearLayout
      android:id="@+id/slidelinear_info"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:gravity="center"
      android:orientation="vertical">
         <TextView /> ....
         <de.hdodenhof.circleimageview.CircleImageView 
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:id="@+id/slideimg"
          android:layout_width="200dp"
          android:layout_height="200dp"
          android:paddingTop="10dp"
          android:layout_centerHorizontal="true"
          android:layout_centerVertical="true"
          app:civ_border_color="@color/black"
          app:civ_border_width="3dp" />
        <TextView /> ....
    </LinearLayout>
</ScrollView>

【讨论】:

  • 对此进行扩展,包含LinearLayout 将只是一个孩子,它的所有孩子都是该视图的一部分。 ScrollView 为包含的单个视图提供了一个包装器,可在提供给 ScrollView 的布局空间内滚动。
  • 我已经尝试过这个解决方案,但是android给我这个错误:java.lang.ClassCastException: android.widget.ScrollView cannot be cast to android.widget.LinearLayout
  • 你在destroyItem()isViewFromObject()中将你的父视图投射到LinearLayout,所以你当然会得到一个ClassCastException
  • 您复制/粘贴的异常解释了这一点。您需要查看导致问题的行并查看您的代码如何尝试将ScrollView 变为LinearLayout
  • 你说得也对 xD,好的,我已经解决了问题,现在我的 PagerAdapter 可以滚动了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多