2016-11-26 29 views
0

我怎樣才能讓StaggeredGridLayoutManager auto_fit與所有的屏幕大小,設置數列自動StaggeredGridLayoutManager使auto_fit列

recyclerViewadapter = new RecyclerViewAdapter(GetDataAdapter1, this); 
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL); 
recyclerView.setLayoutManager(layoutManager); 
recyclerView.setAdapter(recyclerViewadapter); 
+0

其中thridparty StaggeredGridlayout [Rü使用 – Nithinlal

+0

對不起,我在新的Android和你是什麼意思「thridparty」我從教程這個代碼? – fekra

回答

0

試試這個

可以使用Activity Context代替getActivity()

StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(getSpan(), StaggeredGridLayoutManager.VERTICAL); 

public int getSpan() { 
    int orientation = getScreenOrientation(getActivity()); 
    if (isTV(getActivity())) return 4; 
    if (isTablet(getActivity())) 
     return orientation == Configuration.ORIENTATION_PORTRAIT ? 2 : 3; 
    return orientation == Configuration.ORIENTATION_PORTRAIT ? 2 : 3; 
} 


public static boolean isTV(Context context) { 
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2 && ((UiModeManager) context 
      .getSystemService(Context.UI_MODE_SERVICE)) 
      .getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION; 
} 

public static int getScreenOrientation(Context context) { 
    return context.getResources().getDisplayMetrics().widthPixels < 
      context.getResources().getDisplayMetrics().heightPixels ? 
      Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE; 
} 

public static boolean isTablet(Context context) { 
    return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE; 
} 
+0

這使2卡在正常和3在橫向視圖我不希望我想設置它自動取決於屏幕 – fekra

+0

大小好吧。如果您發現任何解決方案,請告訴我。這樣我就可以使用它 – young

0

如果你的項目以相同的寬度,你可以這樣做:

mRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(
     new ViewTreeObserver.OnGlobalLayoutListener() { 
      @Override 
      public void onGlobalLayout() { 
       mRecyclerView.getViewTreeObserver().removeOnGLobalLayoutListener(this); 
       int viewWidth = mRecyclerView.getMeasuredWidth(); 
       float cardViewWidth = getActivity().getResources().getDimension(R.dimen.cardview_layout_width); 
       int newSpanCount = (int) Math.floor(viewWidth/cardViewWidth); 
       mLayoutManager.setSpanCount(newSpanCount); 
       mLayoutManager.requestLayout(); 
      } 
     }); 
+0

我得到錯誤removeOnGLobalLayoutListener – fekra

0

創建幫助類獲取尺寸。

public class GetSize { 
    public static int getColumnCount(Context context, int dp) { 
     if ((double) (getScreenWidth(context)/getPointOfDp(context, dp)) <= 1) return 1; 
     else return getScreenWidth(context)/getPointOfDp(context, dp); 
    } 

    public static int getScreenWidth(Context context){ 

     WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
     Display display = wm.getDefaultDisplay(); 

     Point point = new Point(); 
     display.getSize(point); 

     return point.x; //Screen width 
    } 

    private static int getPointOfDp(Context context, int dp) { //Convert from dp to screen dots 
     return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics()); 
    } 
} 

爲了獲得一個列數需要設置一些最小寬度的項目。

int columnCount = GetSize.getColumnCount(context, 288); 

layoutManager = new StaggeredGridLayoutManager(columnCount, StaggeredGridLayoutManager.VERTICAL); 
recyclerView.setLayoutManager(layoutManager); 
相關問題