ScrollView嵌套ListView显示不完全、嵌套TextView不能滚动解决办法

时间:2024-03-24 14:06:38

目录:


一、情景说明

二、最初做法

三、解决办法

一、情景说明


1、情景

      最近项目刚好有一个需求,需要在一个界面中用ScrollView嵌套一个滚动的TextView和一个listView,既要不影响TextView内容的滚动,也要使整个ScrollView可以滚动起来,最终效果如下图所示:

ScrollView嵌套ListView显示不完全、嵌套TextView不能滚动解决办法ScrollView嵌套ListView显示不完全、嵌套TextView不能滚动解决办法ScrollView嵌套ListView显示不完全、嵌套TextView不能滚动解决办法



2、滚动的TextView的实现

xml布局文件:

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxHeight="100dp"
android:scrollbars="vertical"
android:text="啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦"
android:textSize="20dp" />

java代码:

textView.setMovementMethod(ScrollingMovementMethod.getInstance());//设置可以滑动

二、最初做法


      一开始以为只要在整个布局文件添加一个ScrollView控件,然后在里面放置一个LinearLayout控件且竖向布局,最后在LinearLayout控件中直接添加TextViewListView控件:

<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxHeight="100dp"
android:scrollbars="vertical"
android:text="啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦"
android:textSize="20dp" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
</ScrollView>

      但是直接这样布局会导致TextView内容无法滚动,且ListView只能显示一行,内容显示不全,无法达到预期效果。

三、解决办法


1、解决ScrollView嵌套TextView内容无法滚动问题

TextView控件添加触摸监听事件:

textView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
// 解决scrollView中嵌套textView导致其不能上下滑动的问题
view.getParent().requestDisallowInterceptTouchEvent(true);
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_UP:
view.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
return false;
}
});

2、解决ScrollView嵌套ListView显示不完整的问题

新建一个ListViewInScrollView类继承ListView并重写onMeasure方法:

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.ListView;
public class ListViewInScrollView extends ListView {
public ListViewInScrollView(Context context) {
super(context);
}
public ListViewInScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ListViewInScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public ListViewInScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}

在xml布局文件将ListView改成自定义的ListViewInScrollView控件:

<com.example.suqh.scrollviewlistview.ListViewInScrollView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.example.suqh.scrollviewlistview.ListViewInScrollView>

最后还要在MainActivity.java中补充:

scrollView.smoothScrollTo(0, 0);//一开始滚动到最顶部

这是由于我们重写的ListView会导致界面一打开就会使ListView处于最顶部,这样写可以防止这种现象。