在Android中的Gridview内可滚动自定义Listview

时间:2016-06-01 08:35:55

标签: android

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

* activity_layout.xml *

 > react-native run-android

* custom_calendar.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>

视图是...... calendar View

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

3 个答案:

答案 0 :(得分:0)

尝试&#34;强迫&#34; scrollView不可滚动,通过覆盖scrollView上的onTouch并指定视图不处理任何事件:

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我从这里找到了解决方案...... 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);
    }
}