I am struggling to make my RecyclerView GridLayout for it move to another activity. I can’t find a way to insert the setOnClickListener. Additionally, is it also possible to make the setOnClickListener if else? e.g if(…) setOnClickListener else if(…){}
This is my TrackActivity(or my MainActivity) below:
public class TrackActivity extends AppCompatActivity implements TrackAdapter.ItemListener{
RecyclerView makatiTrackRecyclerView;
ArrayList arrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_makati_track);
makatiTrackRecyclerView = (RecyclerView)findViewById(R.id.makatiTrackGridView);
arrayList = new ArrayList();
arrayList.add(new TrackData("Latest News", R.drawable.icon_latest_news, "#64C0E2"));
arrayList.add(new TrackData("Open Business", R.drawable.icon_open_business, "#9CD081"));
arrayList.add(new TrackData("Voter's Info", R.drawable.icon_voters_info, "#F7B36A"));
arrayList.add(new TrackData("City Officials", R.drawable.icon_city_officials, "#C78EF7"));
arrayList.add(new TrackData("City Donation", R.drawable.icon_city_donation, "#FFACAC"));
arrayList.add(new TrackData("Report", R.drawable.icon_report, "#E3D672"));
TrackAdapter trackAdapter = new TrackAdapter(this, arrayList, this);
makatiTrackRecyclerView.setAdapter(trackAdapter);
GridLayoutManager manager = new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false);
makatiTrackRecyclerView.setLayoutManager(manager);
@Override
public void onItemClick(TrackData data) {
Toast.makeText(getApplicationContext(), data.text + " is clicked", Toast.LENGTH_SHORT).show();
}
}
And here is my Adapter for my RecyclerView:
public class ReportAdapter extends RecyclerView.Adapter<ReportAdapter.ViewHolder> {
ArrayList mValues;
Context mContext;
protected ItemListener2 mListener2;
public ReportAdapter(Context context, ArrayList values,ItemListener2 itemListener2) {
mValues = values;
mContext = context;
mListener2 = itemListener2;
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView textView2;
public ImageView imageView2;
public RelativeLayout relativeLayout2;
ReportData data2;
public ViewHolder(View v) {
super(v);
v.setOnClickListener(this);
textView2 = (TextView) v.findViewById(R.id.makatiReportTextView);
imageView2 = (ImageView) v.findViewById(R.id.makatiReportImageView);
relativeLayout2 = (RelativeLayout) v.findViewById(R.id.makatiReportRelativeLayout);
}
public void setData(ReportData data2) {
this.data2 = data2;
textView2.setText(data2.text2);
imageView2.setImageResource(data2.icon2);
relativeLayout2.setBackgroundColor(Color.parseColor(data2.gridBGColor2));
}
@Override
public void onClick(View view) {
if (mListener2 != null) {
mListener2.onItemClick2(data2);
}
}
}
@Override
public ReportAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.makati_recycler_grid_report, parent, false);
return new ReportAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ReportAdapter.ViewHolder Vholder2, int position) {
if(Vholder2 instanceof ReportAdapter.ViewHolder){
((ReportAdapter.ViewHolder)Vholder2).setData((ReportData) mValues.get(position));
}
}
@Override
public int getItemCount() {
return mValues.size();
}
public interface ItemListener2 {
void onItemClick2(ReportData data2);
}
}
I want to be able to click the gridlayout and will redirect to another Activity. How do I do this?
Thank you for the time and answers 🙂
2
Answers
change this in TrackActivity
I hope thats work with your code, cheers
I checked the code and tested a demo based on the code(for simple, I changed some code), it worked for me.
If you want to move to another Activity, just added the code here:
My test code is as below, the activity code:
the activity_test_gridlayout is:
The class TrackAdapter:
The related to item_layout:
Regards the class GridlayoutDetail, just create an empty Activity for test.
If my code is not enough, you can debug your code by adding some log you wanted.