2016-09-24 92 views
-1

CODE:如何在Recyclerview中刪除此填充?

private void configViews(View view) { 
    int spanCount = 3; // 3 columns 
    int spacing = 3; 
    boolean includeEdge = true; 
    mRecyclerView = (RecyclerView) view.findViewById(R.id.rv_flower_red); 
    mRecyclerView.addItemDecoration(new GridSpacingItemDecoration(spanCount, spacing, includeEdge)); 
    mRecyclerView.setHasFixedSize(true); 
    mRecyclerView.setRecycledViewPool(new RecyclerView.RecycledViewPool()); 
    mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity().getApplicationContext(), spanCount)); 
    mFlowerAdapter = new FlowerAdapter_grid(this); 
    mRecyclerView.setAdapter(mFlowerAdapter); 
} 

public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration { 

    private int spanCount; 
    private int spacing; 
    private boolean includeEdge; 

    public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) { 
     this.spanCount = spanCount; 
     this.spacing = spacing; 
     this.includeEdge = includeEdge; 
    } 

    @Override 
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { 
     int position = parent.getChildAdapterPosition(view); // item position 
     int column = position % spanCount; // item column 

     if (includeEdge) { 
      outRect.left = spacing - column * spacing/spanCount; // spacing - column * ((1f/spanCount) * spacing) 
      outRect.right = (column + 1) * spacing/spanCount; // (column + 1) * ((1f/spanCount) * spacing) 

      if (position < spanCount) { // top edge 
       outRect.top = spacing; 
      } 
      outRect.bottom = spacing; // item bottom 
     } else { 
      outRect.left = column * spacing/spanCount; // column * ((1f/spanCount) * spacing) 
      outRect.right = spacing - (column + 1) * spacing/spanCount; // spacing - (column + 1) * ((1f/ spanCount) * spacing) 
      if (position >= spanCount) { 
       outRect.top = spacing; // item top 
      } 
     } 
    } 
} 

該代碼的核心部分是從https://stackoverflow.com/a/30701422/6736285

但是,當我使用該代碼,enter image description here

GridLayout的對圖像的頂部或底部填充。

正如你所看到的,有太多的垂直填充。

問題:我該如何刪除該填充?請你讓我知道答案?

這裏是我的FlowerAdapter_grid:

public class FlowerAdapter_grid extends RecyclerView.Adapter<FlowerAdapter_grid.Holder> { 

    private static final Object TAG = FlowerAdapter_grid.class.getSimpleName(); 
    private final FlowerClickListener mListener; 
    private List<Flower> mFlowers; 
    public FlowerAdapter_grid(FlowerClickListener listener) { 
     mFlowers = new ArrayList<>(); 
     mListener = listener; 
    } 
    @Override 
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View row = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item_grid, null, false); 
     return new Holder(row); 
    } 
    @Override 
    public void onBindViewHolder(Holder holder, int position) { 
     Flower currFlower = mFlowers.get(position); 
     Glide.with(holder.itemView.getContext()) 
       .load(currFlower.getImage()) 
       .thumbnail(0.5f) 
       .override(300, 300) 
       .centerCrop() 
       .into(holder.mImage); 
    } 

    @Override 
    public int getItemCount() { 
     return mFlowers.size(); 
    } 

    public void addFlower(Flower flower) { 
     mFlowers.add(flower); 
     notifyDataSetChanged(); 
    } 
    public void clear(){ 
     mFlowers.clear(); 
     notifyDataSetChanged(); 
    } 

    public Flower getSelectedFlower(int position) { 
     return mFlowers.get(position); 
    } 

    public class Holder extends RecyclerView.ViewHolder implements View.OnClickListener { 
     private ImageView mImage; 
     public Holder(View itemView) { 
      super(itemView); 
      mImage = (ImageView) itemView.findViewById(R.id.iv_photo); 
      itemView.setOnClickListener(this); 
     } 
     @Override 
     public void onClick(View view) { 
      mListener.onClick(getLayoutPosition()); 
     } 
    } 

    public interface FlowerClickListener { 
     void onClick(int position); 
    } 
} 

row_item_grid

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

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/iv_photo" 
     /> 
</LinearLayout> 

我的片段佈局

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/swipeContainer_red" 
    > 
    <android.support.v7.widget.RecyclerView 
     android:id="@+id/rv_flower_red" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scrollbars="none"> 
    </android.support.v7.widget.RecyclerView> 
</android.support.v4.widget.SwipeRefreshLayout> 

嘗試後,它返回了這個。什麼是問題?

enter image description here

+0

把你的FlowerAdapter_grid和xml代碼 –

+0

@PriyankPatel我補充說,我的滑翔通過網址獲取圖像。 –

+1

嘗試用'spacing = 50;'50px –

回答

3

我也面臨過這樣的問題,當我開始用回收視圖中工作。

問題是你設置網格的垂直滾動網格,設置的行數爲3

所以它會調整你的row_item_grid佈局,以適應在一排3個項目。所以這裏的問題是你在同一個佈局中使用匹配父對象的高度父LinearLayout。但圖像是位於LinearLayout中心的300dpx300dp。

因此,額外填充是因爲您的LinearLayout。 相反,你可以簡單的有

row_item_grid.xml

<ImageView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/iv_photo" 
     /> 

老實說,你不需要家長佈局時,你的觀點是由單一的佈局。我沒有檢查你的itemDecoration代碼,但我的知識,你不會需要它的垂直間距,如果你按照這些步驟

+0

感謝您的建議,穆罕默德阿提夫。 –