zoukankan      html  css  js  c++  java
  • ScrollView嵌套ListView显示不完全、嵌套TextView不能滚动解决办法

    目录:


    一、情景说明

    二、最初做法

    三、解决办法


    一、情景说明


    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控件中直接添加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处于最顶部,这样写可以防止这种现象。

  • 相关阅读:
    Codeforces.1051G.Distinctification(线段树合并 并查集)
    BZOJ.4818.[SDOI2017]序列计数(DP 快速幂)
    BZOJ.2159.Crash的文明世界(斯特林数 树形DP)
    Codeforces.1110F.Nearest Leaf(线段树)
    Codeforces.1110E.Magic Stones(思路 差分)
    Yahoo Programming Contest 2019.D.Ears(DP)
    BZOJ.5251.[八省联考2018]劈配mentor(最大流)
    Codeforces Round #538 (Div. 2)
    BZOJ.5249.[九省联考2018]iiidx(贪心 线段树)
    Hello 2019 (D~G)
  • 原文地址:https://www.cnblogs.com/ssqqhh/p/5694851.html
Copyright © 2011-2022 走看看