【问题标题】:Scrollable Custom Listview inside Gridview in androidandroid中Gridview内的可滚动自定义Listview
【发布时间】:2016-06-01 08:35:55
【问题描述】:

我有一个显示项目的 GridView,每个项目都是一个 ListView。 问题是我无法滚动 ListView 项目,似乎 GridView 正在获取焦点或阻止它从 ListView

* activity_layout.xml *

<GridView
            android:id="@+id/listMonthly"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:cacheColorHint="@android:color/transparent"
            android:divider="@drawable/above_shadow"
            android:numColumns="7"
            android:clipToPadding="true"
            android:fitsSystemWindows="true"
            android:dividerHeight="10dp"
            android:footerDividersEnabled="true"
            android:headerDividersEnabled="true"
            android:listSelector="@android:color/transparent"
            android:padding="10dp" android:stretchMode="columnWidth">

</GridView>

* custom_calendar.xml *

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1"
    android:background="@drawable/border"
    android:orientation="horizontal">

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:dividerHeight="0dp"
        android:layout_weight="1"
        android:scrollbars="vertical"
        android:overScrollMode="always"
        android:id="@+id/customList"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="right"
        android:layout_weight="1"
        android:id="@+id/txtdt"/>

</LinearLayout>

视图是...calendar View

如图所示 .. 我在每个 gridview 项目上都有一个列表视图。 如何使这个列表视图可滚动。

【问题讨论】:

    标签: android


    【解决方案1】:

    尝试通过覆盖滚动视图上的onTouch 并指定视图不处理任何事件来“强制”滚动视图不可滚动:

    gridview.setOnTouchListener(new OnTouchListener(){
      @Override
      public boolean onTouch(View v, MotionEvent event) {
          return false;
      }
    });
    

    【讨论】:

    • 查看我的编辑,这种方式应该排除任何事件事件被gridView处理
    • 不。这个答案也没有奏效。 Listview 仍然无法滚动。
    【解决方案2】:

    不鼓励在 Android 的另一个可滚动视图中使用可滚动。但在最新的支持库中,您可以找到为此类情况添加的NestedScrollView。你可以用它代替你ListViewGridView。 您可以在this 教程中找到更多信息。

    【讨论】:

    • 感谢您的教程(非常感谢)。但是两个可滚动视图都处于不同的布局中。那么我该如何使用 NestedScrollView.
    • 您可以将垂直 LinearLayout 放入 NestedSctollView 并通过 addView() 添加列表视图中的所有项目。
    • 它们在不同的文件中并不重要。 custom_calendar.xml 布局将作为单独的视图附加到 GridView。
    • 好的,知道了。我试试看。
    • 谢谢。动态添加视图很好但很难。我找到了一个简单的解决方案。
    【解决方案3】:

    我从这里找到了解决方案...https://stackoverflow.com/a/33185470/6334037

    我对其进行了一些更改,并且效果很好。

    <com.exapmle.util.NestedListView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:dividerHeight="0dp"
            android:layout_weight="1"
            android:scrollbars="vertical"
            android:overScrollMode="always"/>
    

    * NestedListView.java *

    public class NestedListView extends ListView implements View.OnTouchListener, AbsListView.OnScrollListener {
        private int listViewTouchActionDown;
        private static final int MINIMUM_LIST_ITEMS_VIEWABLE = 1;
    
        public NestedListView(Context context, AttributeSet attrs) {
            super(context, attrs);
            listViewTouchActionDown = 0;
            setOnScrollListener(this);
            setOnTouchListener(this);
        }
    
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
    
        }
    
        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            if (getAdapter() != null && getAdapter().getCount() > MINIMUM_LIST_ITEMS_VIEWABLE) {
                if (listViewTouchActionDown == MotionEvent.ACTION_MOVE) {
                    scrollBy(0, -1);
                }
            }
        }
    
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (getAdapter() != null && getAdapter().getCount() > MINIMUM_LIST_ITEMS_VIEWABLE) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    scrollBy(0, 1);
                } else if (event.getAction() == MotionEvent.ACTION_UP){
                    scrollBy(0, -1);
                }
            }
            return false;
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    
            int newHeight = 0;
            final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
            int heightSize = MeasureSpec.getSize(heightMeasureSpec);
            if (heightMode != MeasureSpec.EXACTLY) {
                ListAdapter listAdapter = getAdapter();
                if (listAdapter != null && !listAdapter.isEmpty()) {
                    int listPosition = 0;
                    for (listPosition = 0; listPosition < listAdapter.getCount()
                            && listPosition < MINIMUM_LIST_ITEMS_VIEWABLE; listPosition++) {
                        View listItem = listAdapter.getView(listPosition, null, this);
                        //now it will not throw a NPE if listItem is a ViewGroup instance
                        if (listItem instanceof ViewGroup) {
                            listItem.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                        }
                        listItem.measure(widthMeasureSpec, heightMeasureSpec);
                        newHeight += listItem.getMeasuredHeight();
                    }
                    newHeight += getDividerHeight() * listPosition;
                }
                if ((heightMode == MeasureSpec.AT_MOST) && (newHeight > heightSize)) {
                    if (newHeight > heightSize) {
                        newHeight = heightSize;
                    }
                }
            } else {
                newHeight = getMeasuredHeight();
            }
            setMeasuredDimension(getMeasuredWidth(), newHeight);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-12
      • 1970-01-01
      • 2012-02-29
      • 2021-07-17
      • 2011-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多