Android上无滚动的ListView
我想将listview设置为不滚动显示所有项目。Android上无滚动的ListView,android,listview,Android,Listview,我想将listview设置为不滚动显示所有项目。 以下是我的布局: <LinearLayout android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id=
以下是我的布局:
<LinearLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:textColor="@android:color/black"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
<TextView
android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:textColor="@android:color/black"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
线性布局属于滚动视图。因此,我想设置listview所有项目并按父项滚动。
我怎么做 这是您在scrollView中设置listview的方式,但就像其他人回答的那样,您永远不应该在scrollView中放置listview,尤其是当listview包含大量项目时
ListView lv = (ListView)findViewById(R.id.list); // your listview inside scrollview
lv.setOnTouchListener(new ListView.OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
int action = event.getAction();
switch (action)
{
case MotionEvent.ACTION_DOWN:
// Disallow ScrollView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_UP:
// Allow ScrollView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
// Handle ListView touch events.
v.onTouchEvent(event);
return true;
}
});
不要将listview放在scrollview中 引用文件 永远不要将ScrollView与ListView一起使用,因为ListView负责自己的垂直滚动。最重要的是,这样做会破坏ListView中处理大型列表的所有重要优化,因为它有效地迫使ListView显示其整个项目列表,以填充ScrollView提供的无限容器 您可以将文本视图作为页眉和页脚添加到listview 检查上面链接中的页眉和页脚方法 您可以在顶部和底部使用相对布局添加文本视图。相对于文本视图,在文本视图的
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="TextView1" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignParentBottom="true"
android:text="TextView1" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/textView1"
android:layout_above="@+id/textView2"
>
</ListView>
</RelativeLayout>
我认为这是不可能的。如果我们使用父布局滚动覆盖列表视图滚动行为,则列表视图无法正常工作。如果此要求的原因与我需要的类似,则下面的类可以提供帮助。它是一个替代的列表视图
,它观察适配器及其变化并作出相应反应,但实际上在引擎盖下使用手动填充的线性布局
。我想我在网上的某个地方找到了这个代码,但是我现在无法找到它来链接到它
我的要求是利用列表视图
的优点,即其适配器功能,在一个页面上显示极少数项目(1到5),在一个屏幕上可能有多个这样的替换列表视图
。一旦显示,元素本身就成为较大页面的一部分,因此它们不会在自己的列表视图中单独滚动,而是作为一个整体滚动页面
public class StaticListView extends LinearLayout {
protected Adapter adapter;
protected Observer observer = new Observer(this);
public StaticListView(Context context) {
super(context);
}
public StaticListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setAdapter(Adapter adapter) {
if (this.adapter != null)
this.adapter.unregisterDataSetObserver(observer);
this.adapter = adapter;
adapter.registerDataSetObserver(observer);
observer.onChanged();
}
private class Observer extends DataSetObserver {
StaticListView context;
public Observer(StaticListView context) {
this.context = context;
}
@Override
public void onChanged() {
List<View> oldViews = new ArrayList<View>(context.getChildCount());
for (int i = 0; i < context.getChildCount(); i++)
oldViews.add(context.getChildAt(i));
Iterator<View> iter = oldViews.iterator();
context.removeAllViews();
for (int i = 0; i < context.adapter.getCount(); i++) {
View convertView = iter.hasNext() ? iter.next() : null;
context.addView(context.adapter.getView(i, convertView, context));
}
super.onChanged();
}
@Override
public void onInvalidated() {
context.removeAllViews();
super.onInvalidated();
}
}
}
public类StaticListView扩展了LinearLayout{
保护适配器;
受保护的观察者=新观察者(本);
公共静态列表视图(上下文){
超级(上下文);
}
公共静态列表视图(上下文、属性集属性){
超级(上下文,attrs);
}
公共void setAdapter(适配器适配器){
if(this.adapter!=null)
this.adapter.unregistedatasetobserver(observer);
this.adapter=适配器;
adapter.registerDataSetObserver(观察者);
observer.onChanged();
}
私有类Observer扩展了DataSetoObserver{
静态列表视图上下文;
公共观察者(StaticListView上下文){
this.context=上下文;
}
@凌驾
更改后的公共无效(){
List oldViews=new ArrayList(context.getChildCount());
for(int i=0;i
scrollview中的listview?这是不可能完成的任务。使用其他选项来显示固定数据。@PankajKumar这并不是不可能的任务,但大多数情况下这是一种不好的做法,请看下面我的回答使用页眉/页脚对于简单的布局效果很好,但如果您想在页眉/页脚中放置edittext等有状态字段,则会变成地狱