Android listview不滚动
我有一个布局,它的起始标记(父标记)是Android listview不滚动,android,android-layout,android-listview,android-scrollview,android-scroll,Android,Android Layout,Android Listview,Android Scrollview,Android Scroll,我有一个布局,它的起始标记(父标记)是 后来我有很多图像,文本视图,编辑文本和其他东西,最后是一个列表视图。 我已经按照规定将listview的高度定义为230dp。 问题是整个布局滚动良好,但是listview中的内容没有单独滚动。这就是xml的外观 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"
后来我有很多图像,文本视图,编辑文本和其他东西,最后是一个列表视图。
我已经按照规定将listview的高度定义为230dp。
问题是整个布局滚动良好,但是listview中的内容没有单独滚动。这就是xml的外观
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="BISCOOT"
android:textSize="25dp"
android:background="@drawable/item_bg_light"
android:textColor="@android:color/white"
android:gravity="center"/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="200dp" android:background="#ff000000">
<ImageSwitcher android:id="@+id/switcher1" android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_alignParentLeft="true" android:layout_alignParentRight="true" >
</ImageSwitcher>
<ProgressBar
android:id="@+id/progressBar2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_centerHorizontal="true"
/>
<ImageButton android:id="@+id/imageButton1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentRight="true"
android:layout_centerVertical="true" android:src="@android:drawable/ic_media_ff" />
<ImageButton android:id="@+id/imageButton2" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/imageButton1" android:src="@android:drawable/ic_media_rew" />
</RelativeLayout>
<TextView android:layout_width="fill_parent"
android:layout_height="30dp"
android:text="Latest headLines"
android:background="@drawable/item_bg_light"
android:textColor="@android:color/white"
android:gravity="center"/>
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:scrollbarFadeDuration="0"
android:scrollbarStyle="outsideOverlay"
android:smoothScrollbar="true"
android:layout_height="260dp">
</ListView>
</LinearLayout>
<!--
<TextView android:layout_width="fill_parent"
android:layout_height="30dp"
android:text="Heroin On the Rocks"
android:background="@drawable/item_bg_light"
android:textColor="@android:color/white"
android:gravity="center"/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="200dp" android:background="#ff000000">
<ImageSwitcher android:id="@+id/switcher2" android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_alignParentLeft="true" android:layout_alignParentRight="true" />
<ImageButton android:id="@+id/imageButton3" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentRight="true"
android:layout_centerVertical="true" android:src="@android:drawable/ic_media_ff" />
<ImageButton android:id="@+id/imageButton4" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/imageButton1" android:src="@android:drawable/ic_media_rew" />
</RelativeLayout>
-->
<TextView android:layout_width="fill_parent"
android:layout_height="30dp"
android:text="Watch Movies"
android:background="@drawable/item_bg_light"
android:textColor="@android:color/white"
android:gravity="center"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="100dp" android:orientation="horizontal"
android:weightSum="6"
>
<ImageView
android:layout_height="100dp"
android:layout_width="wrap_content" android:layout_weight="1"
android:layout_marginLeft="2dp" android:layout_marginRight="2dp"
android:id="@+id/watchmoviesimg1"
/>
<ImageView android:layout_height="100dp"
android:layout_width="wrap_content" android:layout_weight="1"
android:layout_marginLeft="2dp" android:layout_marginRight="2dp"
android:id="@+id/watchmoviesimg2"/>
<ImageView android:layout_height="100dp" android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_marginLeft="2dp" android:layout_marginRight="2dp"
android:id="@+id/watchmoviesimg3"/>
<ProgressBar
android:id="@+id/progressBar3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<ImageView android:layout_height="100dp" android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_marginLeft="2dp" android:layout_marginRight="2dp"
android:id="@+id/watchmoviesimg4"/>
<ImageView android:layout_height="100dp" android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_marginLeft="2dp" android:layout_marginRight="2dp"
android:id="@+id/watchmoviesimg5"/>
<ImageView android:layout_height="100dp" android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_marginLeft="2dp" android:layout_marginRight="2dp"
android:id="@+id/watchmoviesimg6"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:orientation="vertical"
>
<TextView android:layout_width="fill_parent"
android:layout_height="30dp"
android:text="Photo Stories"
android:background="@drawable/item_bg_light"
android:textColor="@android:color/white"
android:gravity="center"/>
<ProgressBar
android:id="@+id/progressBar4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<ImageView
android:id="@+id/photostoryimageview1"
android:layout_height="200dp" android:layout_weight="1"
android:layout_width="fill_parent"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:orientation="vertical"
>
<TextView android:layout_width="fill_parent"
android:layout_height="30dp"
android:text="Star Special"
android:background="@drawable/item_bg_light"
android:textColor="@android:color/white"
android:gravity="center"/>
<ProgressBar
android:id="@+id/progressBar5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
<ImageView
android:id="@+id/starSpecialimageView1"
android:layout_height="200dp" android:layout_weight="1"
android:layout_width="fill_parent"
/>
</LinearLayout>
</LinearLayout>
</ScrollView>
只需从布局中删除
滚动视图。并且,将线性布局设置为父级。然后,试着运行你的应用程序。它将滚动列表
因为ListView
类实现了它自己的滚动,并且它不接收手势,因为它们都是由父级ScrollView
处理的,所以我强烈建议您以某种方式简化布局。例如,您可以将要滚动到列表视图的视图添加为页眉或页脚
看看
更新
以一个父布局和两个子布局为例。而且,在一个布局中,它应该包含ScrollView
和您的内容。并且,在另一个布局中包含您的列表视图
尝试像他那样做 您不应该将ListView放在ScrollView中,因为ListView类实现了自己的滚动,并且不接收手势,因为它们都由父ScrollView处理。您无法在一个布局中执行ScrollViw和ListView。如果您已放置ScrollView,则ListView将不起作用;如果您未放置ScrollView,则ListView将起作用以滚动
更新
即使是scrollview中的listview或listview中的scrollview都可以工作。您需要相应地处理它的触摸。只要添加这个,它就会工作
listView.setOnTouchListener(new View.OnTouchListener() {
// Setting on Touch Listener for handling the touch inside ScrollView
@Override
public boolean onTouch(View v, MotionEvent event) {
// Disallow the touch request for parent scroll on touch of child view
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
不能在滚动视图中添加ListView,因为列表视图也会滚动,并且ListView滚动和scroll View滚动之间可能存在同步问题。您可以创建CustomList视图并将此方法添加到其中
@Override public boolean onInterceptTouchEvent(MotionEvent ev)
{
/*
* Prevent parent controls from stealing our events once we've gotten a touch down
*/
if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
ViewParent p = getParent();
if (p != null) {
p.requestDisallowInterceptTouchEvent(true);
}
}
return false;
}
尝试下面的类,它可能会帮助您在ScrollView中使用ListView
Utility.java
导入android.view.view;
导入android.view.ViewGroup;
导入android.view.ViewGroup.LayoutParams;
导入android.widget.ListAdapter;
导入android.widget.ListView;
公用事业{
公共静态无效setListViewHeightBasedOnChildren(ListView ListView){
ListAdapter ListAdapter=listView.getAdapter();
如果(listAdapter==null){
//前提条件
返回;
}
int totalHeight=listView.getPaddingTop()+listView.getPaddingBottom();
对于(int i=0;i
最后,在将适配器设置为listview之后使用此行-
Utility.setListViewHeightBasedOnChildren(您的\u listview)我又找到了一个解决这个滚动问题的方法。实际上,在scrollview中使用listview有时可能会产生问题
我们可以删除这个滚动视图,只需要一个列表视图。现在创建三个布局:第一个主布局为listview(我们想在列表上方显示),第二个主布局为页眉,第三个主布局为页脚(我们将在列表下方显示),最后在列表中添加页眉和页脚。这将添加页眉和页脚作为列表项
注意:此解决方案适用于小列表,如列表中的2或3项。我按您所说的那样尝试过,但不起作用,而是整个布局保持scroolingthanx以获得您的支持,但使用如此巨大的xml,在父级内部提供线性布局,然后滚动视图,然后再次使用线性布局。。强制关闭是否应删除最上面的父滚动视图以生成父线性布局,然后删除两个子布局一个是滚动视图,另一个是线性布局。将listview置于第二个线性布局中;我认为把Listview放在ScrollView里面不是一个好主意一个列表视图有内置的滚动为什么你要在它上面放一个scroll视图因为我的布局很长,整个xml应该有一个父滚动,Listview应该有一个单独的滚动。但是,你的xml似乎只包含Listview
本身。那么,为什么它需要在滚动视图中
您的XML中还有其他内容吗?我的意思是,发布xml代码,如果完整的部分。或者你只是发布了相关的代码?你不能在一个布局中完成ScrollViw和ListView。如果你已经放置了ScrollView,那么ListView将不起作用,如果你没有放置ScrollView,那么ListView将可以滚动。非常感谢,伙计,这节省了我的时间。belphegor解决方案适合我。当您有一个像ScrollView一样滚动的父元素,并且希望像ListView一样滚动子元素时,就会出现此问题。belphegor解决方案将触摸聚焦到ListView(子项)以进行滚动,如果触摸外部,则作为父项滚动。
@Override public boolean onInterceptTouchEvent(MotionEvent ev)
{
/*
* Prevent parent controls from stealing our events once we've gotten a touch down
*/
if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
ViewParent p = getParent();
if (p != null) {
p.requestDisallowInterceptTouchEvent(true);
}
}
return false;
}
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ListAdapter;
import android.widget.ListView;
public class Utility {
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
if (listItem instanceof ViewGroup) {
listItem.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
}