【问题标题】:GridView with square items doesn't scroll on Android 4带有方形项目的 GridView 在 Android 4 上不滚动
【发布时间】:2016-03-02 19:26:13
【问题描述】:

我在这里研究了所有相关问题,但仍然无法解决这个问题。 我有一个带有自定义适配器的 GridView。此 GridView 应在两列中显示方形项目。我为项目布局制作了一个 SquareLinearLayout 类:

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 适配器在这里膨胀视图:

@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;
}

还有项目:

<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>

还有我的布局:

<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>

启动 GridView 后显示项目 6 全尺寸项目和滚动条拇指。然后拇指消失。而且没有任何滚动条,我无法滚动它。我尝试了 3 列,得到了 15 个没有滚动条的可见项目。 这仅在 Android 4 中发生。Android 5 允许滚动。

我做错了什么?

【问题讨论】:

    标签: android gridview


    【解决方案1】:

    好的。我发布了一个问题,然后找到了答案。所以我把它留在这里,因为它可能对某人有用。

    问题:这个GridView放在fragment中。并且片段正在加载到包含 ScrollView 的布局中。所以这个 GridView 位于 ScrollView 内!

    我在这里找到了答案:How to put GridView inside ScrollView

    我刚刚创建了不允许触摸拦截的扩展类,现在 GridView 可以在所有机器人上滚动!

    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);
        }
    }
    

    一些补充。这个 GridViewScrollable 类防止刷出抽屉。为了解决这个问题并停止 requestDisallowInterceptTouchEvent 传播,我在外部 ScrollView 之前添加了扩展的 LinearLayout。

    public class DisallowInterceptBarrier extends LinearLayout {
    
        // ... constructors skipped
    
        @Override
        public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
            // Stop propagate disallowInterceptTouchEvent here
        }
    }
    

    此解决方案还有助于 yandex 地图 webview 滚动和缩放...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-16
      • 2013-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多