ScrollView内部不可滚动的ListView

我试图做这样的布局:

布局

问题是我不希望ListViews是可滚动的。 我希望它们能够尽可能地高,并使整个屏幕可以滚动。 如果我将ListView的高度设置为wrap_content ,那不起作用。 使其工作的唯一方法是设置一个特定的高度 – 但我不知道在ListView中有多less项目。

我知道我不应该把ListView放在ScrollView中 ,但我不希望ListView是可滚动的,只是为了显示所有的项目。

也许有更好的方法来使它工作?

您创build自定义ListView这是不可滚动的

 public class NonScrollListView extends ListView { public NonScrollListView(Context context) { super(context); } public NonScrollListView(Context context, AttributeSet attrs) { super(context, attrs); } public NonScrollListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec( Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom); ViewGroup.LayoutParams params = getLayoutParams(); params.height = getMeasuredHeight(); } } 

在您的布局资源文件

 <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:fadingEdgeLength="0dp" android:fillViewport="true" android:overScrollMode="never" android:scrollbars="none" > <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <!-- com.Example Changed with your Package name --> <com.Example.NonScrollListView android:id="@+id/lv_nonscroll_list" android:layout_width="match_parent" android:layout_height="wrap_content" > </com.Example.NonScrollListView> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/lv_nonscroll_list" > <!-- Your another layout in scroll view --> </RelativeLayout> </RelativeLayout> </ScrollView> 

在Java文件中

创build一个你的customListview而不是像ListView的对象:
NonScrollListView non_scroll_list =(NonScrollListView)findViewById(R.id.lv_nonscroll_list);

根本不需要使用listview。

如果你真的想一想,在你的情况下,可以用一个线性布局replace一个列表视图。 适配器将是相同的,但是,而不是附加到您的列表视图的适配器,只需调用适配器的getView函数for循环对您的数据,并将检索到的视图添加到您的垂直linearlayout。

但是,只有在列表中有less量项目的情况下才build议使用此方法,并且这些项目的显示表示不是内存密集型的。

那么,有一个叫做Mark Murphy又名CommonsWare的Android家伙,他构build了一个非常有用的组件CWAC Merge Adapter ,它可以让你做你需要的。 您需要dynamic添加视图,而不是从XML中添加视图; 但考虑到你的简单的用户界面,这应该不成问题。 当我不知道我应该滚动什么样的数据时,我正在使用它来处理这种情况。

如果你不想使用它(也许是为了授权的原因),你可以在那里有一个listview(而不是那些连续的两个列表视图),并且覆盖getItemViewsCountgetItemViewTypegetView以便给出上面的布局。 如果你在Google上searchandroid listview different rows ,你会发现有用的信息。 例如这个链接 ,或者这个 。

但是,我会去合并适配器。

我有一个简单的解决scheme,如果有可能计算您的项目的高度/宽…您只需要创build一个ListView的父Linearlayout,然后修复Java的父高度。

在这里输入图像描述

假设你需要devise这个视图…现在父级布局的大小是从java定义…

 final LinearLayout parent=(LinearLayout)findViewById(R.id.parent); final ListView listview=(ListView)findViewById(R.id.listview); parent.setLayoutParams(new LinearLayout.LayoutParams(parent.getLayoutParams().width, dpToPx( number_of_item_in_list * per_item_size_in_dp ))); //number_of_item_in_list is list side ~ listArray.size(); //per_item_size_in_dp = calculate item size in dp. Ex: 80dp public static int dpToPx(int dp) { return (int) (dp * Resources.getSystem().getDisplayMetrics().density); } 

就这样 :)

做到这一点的最佳方法(在2017年)是使用带有NestedScrollView的RecyclerView。 没有需要的黑客,它可以像你想要的那样开箱即用。

select你想要滚动的ListView,然后添加方法“setOnTouchListener”来自动将系统插入到ListView中

 final ListView listview = (ListView) findViewById(R.id.list); listview.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.setClickable(true); 

我希望这对你有用

我的意见是,你不能在滚动视图内使用listview。 相反,您可以为这些创build单独的XML文件,并将其添加到列表视图使用addHeaderview和addFooterview

例如,

 View view=getLayoutInflater().inflate(R.layout.yourlayout,null); lv=(ListView)listview.findViewById(R.id.listView); lv.addHeaderView(view); 

它工作正常100%。