Android 为什么不能在滚动视图中使用ListView?
我找到了很多教程和示例,但是为什么不能在ScrollView中使用ListView呢 唯一的答案是 使用ListView使其不滚动非常昂贵,这违背了ListView的全部目的。 我有以下xml文件Android 为什么不能在滚动视图中使用ListView?,android,listview,scrollview,Android,Listview,Scrollview,我找到了很多教程和示例,但是为什么不能在ScrollView中使用ListView呢 唯一的答案是 使用ListView使其不滚动非常昂贵,这违背了ListView的全部目的。 我有以下xml文件 ......... <ScrollView android:id="@+id/sv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_belo
.........
<ScrollView
android:id="@+id/sv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2">
<ListView
android:id="@+id/list"
android:layout_width="275dp"
android:layout_height="200dp">
</ListView>
</ScrollView>
.........
但不起作用。
特定身高和体重的可选滚动列表项是什么
但是我试过这个:
<ListView
android:id="@+id/list"
android:layout_width="275dp"
android:layout_height="200dp">
</ListView>
或
<ScrollView android:layout_width="fill_parent" android:id="@+id/sv"
android:layout_height="wrap_content" android:layout_below="@+id/textView2">
<LinearLayout android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<ListView android:id="@+id/list" android:layout_width="275dp"
android:layout_height="240dp"></ListView>
</LinearLayout>
</ScrollView>
没有scollview,但是滚动列表项是可以的,但不是很慢。
我已经试过了,但没有更多的想法
编辑:
<ListView
android:id="@+id/list"
android:layout_width="275dp"
android:layout_height="200dp">
</ListView>
Listview是否具有内置滚动功能。因此,无需在scrollview中定义listview。这意味着不需要滚动视图?这是因为两个视图都是可滚动的。 您是否已经尝试过搜索/谷歌搜索它?有一千根火柴
这是因为两个视图都可以滚动。 您是否已经尝试过搜索/谷歌搜索它?有一千根火柴
ListView具有内置的滚动功能。您不需要在ScrollView中再次定义它,ListView具有内置的滚动功能。您不需要在ScrollView中再次定义它,问题不在于ListView,而在于ScrollView 这两个视图需要控制垂直滚动,如果您有一个滚动视图和一个列表视图在同一个布局中,并且用户向下滚动,那么哪个视图必须获得焦点和滚动 这是一个常见的问题,有一个通用且简单的解决方案(这个解决方案来自谷歌Android团队的Romain Guy!):不要在滚动视图中使用listview强>
如果你想要一个有趣的视频/演讲,你可以从谷歌I/O演讲中观看:我真的建议你看一看:)问题不在于ListView,而在于ScrollView 这两个视图需要控制垂直滚动,如果您有一个滚动视图和一个列表视图在同一个布局中,并且用户向下滚动,那么哪个视图必须获得焦点和滚动 这是一个常见的问题,有一个通用且简单的解决方案(这个解决方案来自谷歌Android团队的Romain Guy!):不要在滚动视图中使用listview强>
如果你想要一个有趣的视频/演讲,你可以从谷歌I/O演讲中观看:我真的建议你看看:)列表视图不应该出现在ScrollView中。即使你黑客,我的意思是大黑客,这两个视图通过扩展他们,让他们滚动,也可以点击等,一些东西最终会打破。也不要将ListView添加到卷轴
视图。列表视图不应进入ScrollView。即使你黑客,我的意思是大黑客,这两个视图通过扩展他们,让他们滚动,也可以点击等,一些东西最终会打破。也不要将ListView添加到卷轴 查看。编辑: 是的,android doc说不要在scrollview中使用listview,因为两者都有相同的垂直触摸手势。滚动项目时会出现问题。还有适配器大小的问题,这就是为什么我们无法看到整个项目的原因 最后我得到了最好的解决方案,我想和大家分享。请不要在Scrollview中使用Listview,而应使用LinearLayout,并使用View Infaltor绑定项目
<LinearLayout
android:id="@+id/linear_scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linear_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="@string/bottom_item"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="5dp"
android:text="@string/androidhub4you" />
</LinearLayout>
</ScrollView>
在java类中-
for (int i = 0; i < mArrayListData.size(); i++) {
/**
* inflate items/ add items in linear layout instead of listview
*/
LayoutInflater inflater = null;
inflater = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View mLinearView = inflater.inflate(R.layout.row, null);
/**
* getting id of row.xml
*/
TextView mFirstName = (TextView) mLinearView
.findViewById(R.id.textViewName);
TextView mLastName = (TextView) mLinearView
.findViewById(R.id.textViewLastName);
/**
* set item into row
*/
final String fName = mArrayListData.get(i).getmFirstName();
final String lName = mArrayListData.get(i).getmLastName();
mFirstName.setText(fName);
mLastName.setText(lName);
/**
* add view in top linear
*/
mLinearListView.addView(mLinearView);
/**
* get item row on click
*
*/
mLinearView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "Clicked item;" + fName,
Toast.LENGTH_SHORT).show();
}
});
}
Read more: http://www.androidhub4you.com/2014/03/android-listview-into-scrollview-issue.html#ixzz2xc7Ag74u
for(int i=0;i
编辑:
是的,android doc说不要在scrollview中使用listview,因为两者都有相同的垂直触摸手势。滚动项目时会出现问题。还有适配器大小的问题,这就是为什么我们无法看到整个项目的原因
最后我得到了最好的解决方案,我想和大家分享
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:quilt="http://schemas.android.com/apk/res-auto"
android:id="@+id/scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/inner_listview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</ScrollView>