Android ListView在ScrollView困境中-此应用程序是如何做到这一点的?
因此,基本上您不能将ListView放置在ScrollView中,因为滚动功能在两种布局中都会发生冲突。当我尝试这样做时,ListView变得完全无用,并且出现了许多其他问题 Facebook是如何做到这一点的 如您所见,工作部分是一个列表视图,也是一个可滚动的布局,因此用户可以向下滚动查看教育部分,这也是一个列表视图 我的代码:Android ListView在ScrollView困境中-此应用程序是如何做到这一点的?,android,listview,android-listview,Android,Listview,Android Listview,因此,基本上您不能将ListView放置在ScrollView中,因为滚动功能在两种布局中都会发生冲突。当我尝试这样做时,ListView变得完全无用,并且出现了许多其他问题 Facebook是如何做到这一点的 如您所见,工作部分是一个列表视图,也是一个可滚动的布局,因此用户可以向下滚动查看教育部分,这也是一个列表视图 我的代码: <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" andr
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff"
android:layout_marginBottom="40dp">
<!-- More layouts -->
<ListView
android:id="@+id/work_list"
android:layout_below="@+id/recentpic"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</ScrollView >
<RelativeLayout
android:id="@+id/work_list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
我不想要ListView滚动条
因此,滚动困境从等式中完全消除。即使禁用滚动条,问题仍然存在
我想到的解决方案:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff"
android:layout_marginBottom="40dp">
<!-- More layouts -->
<ListView
android:id="@+id/work_list"
android:layout_below="@+id/recentpic"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</ScrollView >
<RelativeLayout
android:id="@+id/work_list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
生成XML行(ListView的每个工作区)并将其注入布局,避免使用ListView,类似于使用Javascript生成HTML代码
你认为Facebook在他们的android应用程序中使用了什么方法来实现这一点,我应该对我的代码做什么更改?:) 您是否尝试过使用
NestedScrollView
?我认为这是一个嵌套的滚动视图
,它包含一个列表视图
,整个内容都包含在滚动视图中。此链接可能有助于:
好的,所以我设法编写了我提到的我自己的想法。这是一个非常“性感”的代码,它完成了任务:D
给你们,伙计们。我希望它能帮助某人:)
因此,基本上,我是用多个子布局动态地扩展父布局,并完全消除视图中的ListView。这使得在ScrollView中使用它非常简单,并且忘记了这个难题
父布局:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff"
android:layout_marginBottom="40dp">
<!-- More layouts -->
<ListView
android:id="@+id/work_list"
android:layout_below="@+id/recentpic"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</ScrollView >
<RelativeLayout
android:id="@+id/work_list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
子布局-work\u single\u item.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff">
<ImageView
android:id="@+id/work_pic"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:src="@mipmap/image_placeholder"/>
</RelativeLayout>
在父布局的OnCreate函数中对以下行进行编码。
RelativeLayout parent = (RelativeLayout) findViewById(R.id.work_list);
//array containing all children ids
ArrayList<Integer> children = new ArrayList<>();
//adding 10 children to the parent
for(int i=0;i<10;i++) {
RelativeLayout child = new RelativeLayout(this);
View tempchild = getLayoutInflater().inflate(R.layout.work_single_item, null);
child.addView(tempchild);
child.setId(i); //setting an id for the child
children.add(i); //adding the child's id to the list
if(i!=0) //if it isn't the 1st child, stack them below one another, since the 1st child does not have a child to stack below
{
RelativeLayout.LayoutParams params
= new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, children.get(i - 1)); //stack it below the previous child
child.setLayoutParams(params);
}
parent.addView(child); //add the new child
}
RelativeLayout parent=(RelativeLayout)findViewById(R.id.work\u列表);
//包含所有子ID的数组
ArrayList子项=新的ArrayList();
//向父级添加10个子级
对于(int i=0;您正在考虑的解决方案是最好的方法。在滚动视图中使用列表视图也不是一个好主意。@rahultwari我设法编写了它。如果您感兴趣,请查看我的答案:)接受您的答案:)我设法编写了它。如果你感兴趣,请查看我的答案:)代码是干净的,简直太棒了。。。你在那里做得很好。