2016-09-24 101 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

感谢您的建议,穆罕默德阿提夫。 –