I am using RecyclerView with GridLayoutManager as a layout for the bus ticket booking. Everything works fine but except the place where I stuck with the logics that I have to use to leave more spacing between 3 and 4 column item in all the rows. Basically my layout should be like as the screenshot attached below.
My Requirement:
Current Output:
I am also posting my complete RecyclerView GridLayoutManager for your reference as follows
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);
}
}
What I have tried in coding:
I have followed this gist to assign the spacing for my grid items using ItemDecoration as below. But unfortunately I could able to assign spacing only equally between all the items.
mRecyclerView.setLayoutManager(new GridLayoutManager(context, NUM_COLUMNS);
ItemOffsetDecoration itemDecoration = new ItemOffsetDecoration(context, R.dimen.item_offset);
mRecyclerView.addItemDecoration(itemDecoration);
Kindly please help me with any kind of tips and solutions to solve my case. I am completely stuck up how to proceed with this requirement and logic using RecyclerView which I considered should be the optimized approach. Thanks in advance.
Using item decoration here could create some problems. If you are planning to handle click on the item, clicking on blank space would be interpreted as a click on items located in inner columns.
You can configure GridLayoutManager for handling one more column and handle space as a specific view type. Here I put code implementing this approach.
Hope this could help.