Android 可滚动分页器
我做了一个Android 可滚动分页器,android,xml,android-studio,scrollview,android-pageradapter,Android,Xml,Android Studio,Scrollview,Android Pageradapter,我做了一个PagerAdapter,它工作得很好,但当我水平放置手机时,很多东西,如TextView都不可见,因为它们位于手机屏幕的“下方”。我知道我可以放置一个滚动视图,但是这个选项会产生很多错误 除了滚动视图之外,还有其他选项吗?或者错误是什么意思 这是我的代码,带有LinearLayout,没有任何错误,但是PagerAdapter不可滚动 Java代码: public class SlideAdapter_info extends PagerAdapter { Context c
PagerAdapter
,它工作得很好,但当我水平放置手机时,很多东西,如TextView
都不可见,因为它们位于手机屏幕的“下方”。我知道我可以放置一个滚动视图
,但是这个选项会产生很多错误
除了滚动视图
之外,还有其他选项吗?或者错误是什么意思
这是我的代码,带有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
code:
<?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>
....
....
这是结果:(水平)
这是结果:(垂直)
如果我没有使用LinearLayout
而是使用ScrollView
,Android Studio
会出现以下错误:
java.lang.IllegalStateException:ScrollView只能承载一个直接子对象
在esame.progetto.xhondar.github.com.info.slidedapter\u info.instanceItem(slidedapter\u info.java:87)
这是行:
视图=充气机。充气(R.layout.slide\u信息,容器,错误)
java.lang.IllegalStateException:ScrollView只能承载一个直接子对象
正在准确地告诉您问题所在。只需将线性布局
转换为滚动视图
,您就有了多个子项。如果希望当前布局可以在ViewPager
中滚动,则需要在布局xml文件中使用ScrollView
将LinearLayout
包装起来
<?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>
....
....
这是如何放置图像的语法:“![](图像)”要在此基础上展开,包含的LinearLayou
t将只有一个子视图,其所有子视图都是该视图的一部分ScrollView
为包含的单个视图提供了一个包装器,使其可以在提供给ScrollView
的布局空间内滚动。我已经尝试过这个解决方案,但是android给我这个错误:java.lang.ClassCastException:android.widget.ScrollView不能强制转换为android.widget.LinearLayout您正在destroyItem()
和isViewFromObject()
中将父视图强制转换为LinearLayout
,因此,您当然会得到一个ClassCastException
。您复制/粘贴的异常正好解释了这一点。您需要查看导致问题的行,并查看您的代码是如何尝试进行滚动视图
a线性布局
。您也是对的xD,好的,我已经解决了问题,现在我的PagerAdapter可以滚动了