0

I studied all related questions here and still can't solve this. I have a GridView with custom adapter. This GridView should show square items in two columns. I made a SquareLinearLayout class for item layout:

public class SquareLinearLayout extends LinearLayout {
    public SquareLinearLayout(Context context) {
        super(context);
    }

    public SquareLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SquareLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
    }
}

GridLayout adapter inflate view here:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.category_entry, parent, false);
    }
    if (convertView != null) {
        ((TextView) convertView.findViewById(R.id.category_name)).setText(getItem(position).Name());
    }
    return convertView;
}

And item:

<xxx.SquareLinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical">

    <LinearLayout
        android:background="@color/colorPrimaryTransparent"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="5dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/category_name"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"/>
    </LinearLayout>
</xxx.SquareLinearLayout>

And my layout:

<RelativeLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical">

    <GridView
        android:id="@+id/categories"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="2"
        android:scrollbars="vertical"/>    
</RelativeLayout>

After starting GridView shows items 6 full-sized items and scrollbar thumb. Then thumb dissapears. And there is no any scrollbar and I can't scroll it. I tried 3 columns and got 15 visible items without scrollbar. This happens in Android 4 only. Android 5 allow scrolling.

What i'm doing wrong ?

Alexandr Shutko
  • 1,857
  • 2
  • 20
  • 27

1 Answers1

0

Ok. I posted a question and then found an answer. So I leave it here because it may be useful to someone.

The problem: This GridView is placed in fragment. And fragment is loading into layout containing ScrollView. So this GridView located inside a ScrollView!

I found an answer here: How to put GridView inside ScrollView

I just created extended class with disallowed touch intercept and now GridView is scrollable on all androids!

public class GridViewScrollable extends GridView {

    /* xxx constructors skiped */

    @Override
    public boolean onTouchEvent(MotionEvent ev){
        if (action == MotionEvent.ACTION_DOWN)
            requestDisallowInterceptTouchEvent(true);
        return super.onTouchEvent(ev);
    }
}

Some additions. This GridViewScrollable class prevent swipe out a drawer. To resolve this and stop requestDisallowInterceptTouchEvent propagation i added extended LinearLayout before outer ScrollView.

public class DisallowInterceptBarrier extends LinearLayout {

    // ... constructors skipped

    @Override
    public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
        // Stop propagate disallowInterceptTouchEvent here
    }
}

This solution also helps with yandex maps webview scrolling and zooming...

Community
  • 1
  • 1
Alexandr Shutko
  • 1,857
  • 2
  • 20
  • 27