2016-02-28 31 views
0

我在這裏研究了所有相關的問題,但仍無法解決此問題。 我有一個帶有自定義適配器的GridView。這個GridView應該顯示兩列中的方形項目。我爲項目佈局的SquareLinearLayout類:帶有方形項目的GridView不會在Android 4上滾動

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

網格佈局適配器充氣鑑於此:

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

啓動GridVi後新聞顯示項目6全尺寸項目和滾動條拇指。然後拇指消失。沒有任何滾動條,我不能滾動它。我嘗試了3列,並獲得了15個沒有滾動條的可見項目。 這僅發生在Android 4中。 Android 5允許滾動。

我做錯了什麼?

回答

0

好的。我發佈了一個問題,然後找到了答案。所以我把它留在這裏,因爲它可能對某人有用。

問題:這個GridView放在片段中。片段加載到包含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的地圖網頁流量滾動和縮放幫助...

相關問題