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
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>
嘗試後,它返回了這個。什麼是問題?
把你的FlowerAdapter_grid和xml代碼 –
@PriyankPatel我補充說,我的滑翔通過網址獲取圖像。 –
嘗試用'spacing = 50;'50px –