要以以下方式向gridview添加边距。 左边距为2,4,2,9,12,14,.. 右边距为3,5,8,10,13,15,..
这是我的代码段,我试图在网格视图中将边距设置为2x2项...
即位置-..,2,3,4,5,..,7,8,9,10,..,12,13,14,15,..,17,18,19,20, ..明智之举。2,3,4,5&12,13,14,15,..获得预期的输出。但是我的案件在7,8,9,10和17,18,19,20系列中均告失败。
帮我解决这个问题。困惑于解决这个数学难题。 预先感谢。
public class BoxFeedAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<BoxFeedResult> boxFeedResults;
private Context mContext;
private RecyclerViewItemClickListener viewItemClickListener;
private final int LARGE_TYPE = 0;
private final int SMALL_TYPE = 1;
private final int PROGRESS_TYPE = 2;
private int GRID_VIEW_MARGIN_LEFT = 0;
private int GRID_VIEW_MARGIN_RIGHT = 0;
private int GRID_VIEW_MARGIN_TOP = 0;
private int GRID_VIEW_MARGIN_BOTTOM = 0;
private int GRID_VIEW_ROW_MARGIN = 0;
public BoxFeedAdapter(Context context, RecyclerViewItemClickListener viewItemClickListener) {
this.mContext = context;
this.viewItemClickListener = viewItemClickListener;
boxFeedResults = new ArrayList<>();
GRID_VIEW_MARGIN_LEFT = context.getResources().getDimensionPixelOffset(R.dimen.empty_dimen);
GRID_VIEW_MARGIN_RIGHT = context.getResources().getDimensionPixelOffset(R.dimen.empty_dimen);
GRID_VIEW_MARGIN_TOP = context.getResources().getDimensionPixelOffset(R.dimen.channel_row_padding);
GRID_VIEW_MARGIN_BOTTOM = context.getResources().getDimensionPixelOffset(R.dimen.empty_dimen);
GRID_VIEW_ROW_MARGIN = context.getResources().getDimensionPixelOffset(R.dimen.channel_row_padding_left);
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (viewType == SMALL_TYPE) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_box_feed_series_banner, null);
return new BoxFeedSeriesListViewHolder(layoutView);
} else if (viewType == LARGE_TYPE) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_box_feed_episode_banner, null);
return new BoxFeedEpisodeViewHolder(layoutView);
} else {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.progress_item, null);
return new ProgressViewHolder(layoutView);
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof BoxFeedSeriesListViewHolder) {
StaggeredGridLayoutManager.LayoutParams layoutParams = new StaggeredGridLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setFullSpan(false);
if ((position - 1) % 2 == 0) {
layoutParams.setMargins(GRID_VIEW_ROW_MARGIN, GRID_VIEW_MARGIN_TOP, GRID_VIEW_MARGIN_RIGHT, GRID_VIEW_MARGIN_BOTTOM);
} else {
layoutParams.setMargins(GRID_VIEW_MARGIN_LEFT, GRID_VIEW_MARGIN_TOP, GRID_VIEW_ROW_MARGIN, GRID_VIEW_MARGIN_BOTTOM);
}
holder.itemView.setLayoutParams(layoutParams);
final BoxFeedResult singleFeed = boxFeedResults.get(position);
final BoxFeedSeriesListViewHolder viewHolder = (BoxFeedSeriesListViewHolder) holder;
Glide.with(mContext)
.load(singleFeed.getA4MediumUrl())
.placeholder(R.drawable.bg_black)
.into(viewHolder.seriesImage);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, singleFeed.getId(), Toast.LENGTH_SHORT).show();
viewItemClickListener.onItemClickListioner(singleFeed, viewHolder.seriesImage, position);
}
});
} else if (holder instanceof BoxFeedEpisodeViewHolder) {
StaggeredGridLayoutManager.LayoutParams layoutParams = new StaggeredGridLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setFullSpan(true);
holder.itemView.setLayoutParams(layoutParams);
final BoxFeedResult singleFeed = boxFeedResults.get(position);
final BoxFeedEpisodeViewHolder viewHolder = (BoxFeedEpisodeViewHolder) holder;
Glide.with(mContext)
.load(singleFeed.getAspectMediumUrl())
.placeholder(R.drawable.bg_black)
.into(viewHolder.episodeImage);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, singleFeed.getId(), Toast.LENGTH_SHORT).show();
}
});
} else {
StaggeredGridLayoutManager.LayoutParams layoutParams = new StaggeredGridLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setFullSpan(true);
holder.itemView.setLayoutParams(layoutParams);
}
}
@Override
public int getItemCount() {
return boxFeedResults.size();
}
@Override
public int getItemViewType(int position) {
switch (boxFeedResults.get(position).getTileType()) {
case “large":
return LARGE_TYPE;
case “small":
return SMALL_TYPE;
default:
return PROGRESS_TYPE;
}
}
public void addList(List<BoxFeedResult> feedResults) {
this.boxFeedResults = feedResults;
notifyDataSetChanged();
}
}