您的位置:首页 > 其它

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

2017-02-12 10:15 441 查看

目录:

一、情景说明

二、最初做法

三、解决办法

一、情景说明

1、情景

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







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
控件中直接添加
TextView
ListView
控件:

<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处于最顶部,这样写可以防止这种现象。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐