0

I have a GridView that displays items, each item is a ListView. The problem is that I can't scroll the ListView items, seems like the GridView is obtaining the focus or preventing it from the 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>

The View is ... calendar View

as figure shows .. i have a listview on every gridview item. how to make this listview scrollable.

Jitesh Prajapati
  • 2,533
  • 4
  • 29
  • 51

3 Answers3

0

Try to "force" the scrollView not to be scrollable, by overriding the onTouch on the scrollView and specifying that the view does not handle any event :

gridview.setOnTouchListener(new OnTouchListener(){
  @Override
  public boolean onTouch(View v, MotionEvent event) {
      return false;
  }
});
NSimon
  • 5,212
  • 2
  • 22
  • 36
0

It was discouraged to use scrollable inside another scrollable view in Android. But in the latest support library you can find NestedScrollView which was added for such cases. You can use it instead of you ListView or GridView. More info you can find in this tutorial.

amukhachov
  • 5,822
  • 1
  • 41
  • 60
0

I found solution from here... https://stackoverflow.com/a/33185470/6334037

I made some changes in it and it works perfectly.

<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);
    }
}
Community
  • 1
  • 1
Jitesh Prajapati
  • 2,533
  • 4
  • 29
  • 51