在 Recycler GridLayoutManager 中的 3 和 4 行项目之间提供更多间距
Giving more spacing between 3 and 4 row item in Recycler GridLayoutManager
我将 RecyclerView 与 GridLayoutManager 一起用作巴士票预订的布局。一切正常,但除了我坚持逻辑的地方,我必须使用它在所有行中的 3 和 4 列项目之间留出更多间距。基本上我的布局应该像下面的截图一样。
我的要求:
当前输出:
我也把我完整的 RecyclerView GridLayoutManager 贴出来供大家参考如下
MultiSelectRecyclerViewAdapter.java
public class MultiSelectRecyclerViewAdapter extends SelectableAdapter<MultiSelectRecyclerViewAdapter.ViewHolder> {
private ArrayList<String> mArrayList;
private Context mContext;
private ViewHolder.ClickListener clickListener;
public MultiSelectRecyclerViewAdapter (Context context, ArrayList<String> arrayList,ViewHolder.ClickListener clickListener) {
this.mArrayList = arrayList;
this.mContext = context;
this.clickListener = clickListener;
}
// Create new views
@Override
public MultiSelectRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
R.layout.row_multiselect, null);
ViewHolder viewHolder = new ViewHolder(itemLayoutView,clickListener);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
viewHolder.tvName.setText(mArrayList.get (position));
viewHolder.selectedOverlay.setVisibility(isSelected(position) ? View.VISIBLE : View.INVISIBLE);
}
@Override
public int getItemCount() {
return mArrayList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,View.OnLongClickListener {
public TextView tvName;
private ClickListener listener;
private final View selectedOverlay;
public ViewHolder(View itemLayoutView,ClickListener listener) {
super(itemLayoutView);
this.listener = listener;
tvName = (TextView) itemLayoutView.findViewById(R.id.tvName);
selectedOverlay = (View) itemView.findViewById(R.id.selected_overlay);
itemLayoutView.setOnClickListener(this);
itemLayoutView.setOnLongClickListener (this);
}
@Override
public void onClick(View v) {
if (listener != null) {
listener.onItemClicked(getAdapterPosition ());
}
}
@Override
public boolean onLongClick (View view) {
if (listener != null) {
return listener.onItemLongClicked(getAdapterPosition ());
}
return false;
}
public interface ClickListener {
public void onItemClicked(int position);
public boolean onItemLongClicked(int position);
}
}
}
MultiSelectRecyclerViewActivity.java
public class MultiSelectRecyclerViewActivity extends AppCompatActivity implements MultiSelectRecyclerViewAdapter.ViewHolder.ClickListener {
private Toolbar toolbar;
private RecyclerView mRecyclerView;
private MultiSelectRecyclerViewAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private ArrayList<String> mArrayList = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView(R.layout.activity_multiselect);
toolbar = (Toolbar) findViewById(R.id.toolbar);
for (int i = 1; i <= 48; i++) {
mArrayList.add ( ""+ i);
}
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("MultiSelectRecylcerView");
}
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new GridLayoutManager (this,4));
mAdapter = new MultiSelectRecyclerViewAdapter (MultiSelectRecyclerViewActivity.this,mArrayList,this);
mRecyclerView.setAdapter (mAdapter);
}
@Override
public void onItemClicked (int position) {
toggleSelection(position);
}
@Override
public boolean onItemLongClicked (int position) {
toggleSelection(position);
return true;
}
private void toggleSelection(int position) {
mAdapter.toggleSelection (position);
}
}
我在编码中的尝试:
我已按照此 gist 使用 ItemDecoration 为我的网格项分配间距,如下所示。但不幸的是,我只能在所有项目之间平均分配间距。
mRecyclerView.setLayoutManager(new GridLayoutManager(context, NUM_COLUMNS);
ItemOffsetDecoration itemDecoration = new ItemOffsetDecoration(context, R.dimen.item_offset);
mRecyclerView.addItemDecoration(itemDecoration);
请帮助我提供任何提示和解决方案来解决我的问题。我完全不知道如何使用我认为应该是优化方法的 RecyclerView 来处理这个要求和逻辑。提前致谢。
在这里使用物品装饰可能会产生一些问题。如果您打算处理点击该项目,点击空白 space 将被解释为点击位于内部列中的项目。
您可以配置 GridLayoutManager 以处理多一列并将 space 处理为特定的视图类型。 Here 我放了实现这种方法的代码。
希望这对您有所帮助。
我将 RecyclerView 与 GridLayoutManager 一起用作巴士票预订的布局。一切正常,但除了我坚持逻辑的地方,我必须使用它在所有行中的 3 和 4 列项目之间留出更多间距。基本上我的布局应该像下面的截图一样。
我的要求:
当前输出:
我也把我完整的 RecyclerView GridLayoutManager 贴出来供大家参考如下
MultiSelectRecyclerViewAdapter.java
public class MultiSelectRecyclerViewAdapter extends SelectableAdapter<MultiSelectRecyclerViewAdapter.ViewHolder> {
private ArrayList<String> mArrayList;
private Context mContext;
private ViewHolder.ClickListener clickListener;
public MultiSelectRecyclerViewAdapter (Context context, ArrayList<String> arrayList,ViewHolder.ClickListener clickListener) {
this.mArrayList = arrayList;
this.mContext = context;
this.clickListener = clickListener;
}
// Create new views
@Override
public MultiSelectRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
R.layout.row_multiselect, null);
ViewHolder viewHolder = new ViewHolder(itemLayoutView,clickListener);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
viewHolder.tvName.setText(mArrayList.get (position));
viewHolder.selectedOverlay.setVisibility(isSelected(position) ? View.VISIBLE : View.INVISIBLE);
}
@Override
public int getItemCount() {
return mArrayList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,View.OnLongClickListener {
public TextView tvName;
private ClickListener listener;
private final View selectedOverlay;
public ViewHolder(View itemLayoutView,ClickListener listener) {
super(itemLayoutView);
this.listener = listener;
tvName = (TextView) itemLayoutView.findViewById(R.id.tvName);
selectedOverlay = (View) itemView.findViewById(R.id.selected_overlay);
itemLayoutView.setOnClickListener(this);
itemLayoutView.setOnLongClickListener (this);
}
@Override
public void onClick(View v) {
if (listener != null) {
listener.onItemClicked(getAdapterPosition ());
}
}
@Override
public boolean onLongClick (View view) {
if (listener != null) {
return listener.onItemLongClicked(getAdapterPosition ());
}
return false;
}
public interface ClickListener {
public void onItemClicked(int position);
public boolean onItemLongClicked(int position);
}
}
}
MultiSelectRecyclerViewActivity.java
public class MultiSelectRecyclerViewActivity extends AppCompatActivity implements MultiSelectRecyclerViewAdapter.ViewHolder.ClickListener {
private Toolbar toolbar;
private RecyclerView mRecyclerView;
private MultiSelectRecyclerViewAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private ArrayList<String> mArrayList = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView(R.layout.activity_multiselect);
toolbar = (Toolbar) findViewById(R.id.toolbar);
for (int i = 1; i <= 48; i++) {
mArrayList.add ( ""+ i);
}
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("MultiSelectRecylcerView");
}
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new GridLayoutManager (this,4));
mAdapter = new MultiSelectRecyclerViewAdapter (MultiSelectRecyclerViewActivity.this,mArrayList,this);
mRecyclerView.setAdapter (mAdapter);
}
@Override
public void onItemClicked (int position) {
toggleSelection(position);
}
@Override
public boolean onItemLongClicked (int position) {
toggleSelection(position);
return true;
}
private void toggleSelection(int position) {
mAdapter.toggleSelection (position);
}
}
我在编码中的尝试:
我已按照此 gist 使用 ItemDecoration 为我的网格项分配间距,如下所示。但不幸的是,我只能在所有项目之间平均分配间距。
mRecyclerView.setLayoutManager(new GridLayoutManager(context, NUM_COLUMNS);
ItemOffsetDecoration itemDecoration = new ItemOffsetDecoration(context, R.dimen.item_offset);
mRecyclerView.addItemDecoration(itemDecoration);
请帮助我提供任何提示和解决方案来解决我的问题。我完全不知道如何使用我认为应该是优化方法的 RecyclerView 来处理这个要求和逻辑。提前致谢。
在这里使用物品装饰可能会产生一些问题。如果您打算处理点击该项目,点击空白 space 将被解释为点击位于内部列中的项目。
您可以配置 GridLayoutManager 以处理多一列并将 space 处理为特定的视图类型。 Here 我放了实现这种方法的代码。
希望这对您有所帮助。