skip to Main Content

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


  1. change this in TrackActivity

    public class TrackActivity extends AppCompatActivity {
    
    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);
        makatiTrackRecyclerView.setOnItemClickListener(itemClickListener);
             }
        private AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    
    
            if (position == 0) {
              // Do action Here
            } else if (position == 1) {
              // Do action Here
            } else if (position == 2) {
             // Do action Here
            } else if (position == 3) {
             // Do action Here
            } else if (position == 4) {
             // Do action Here
            }else if (position == 5) {
             // Do action Here   
            }
        }
    };
    }
    

    I hope thats work with your code, cheers

    Login or Signup to reply.
  2. 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:

    public void onItemClick(TrackData data) {
            Toast.makeText(getApplicationContext(), data.text + " is clicked", Toast.LENGTH_SHORT).show();
            // Open Activity
            Intent intent = new Intent(this, GridlayoutDetail.class);
            startActivity(intent);
    }
    

    My test code is as below, the activity code:

    public class TestGridlayout extends AppCompatActivity implements TrackAdapter.ItemListener2 {
        RecyclerView makatiTrackRecyclerView;
        ArrayList arrayList;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_test_gridlayout);
            makatiTrackRecyclerView = findViewById(R.id.recycle_view);
            
            arrayList = new ArrayList();
            arrayList.add(new TrackData("Latest News", "123"));
            arrayList.add(new TrackData("Open Business", "1234"));
            arrayList.add(new TrackData("Voter's Info", "123456"));
            arrayList.add(new TrackData("City Officials", "1234567"));
            arrayList.add(new TrackData("City Donation", "1234567"));
            arrayList.add(new TrackData("Report", "12345678"));
    
            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 onItemClick2(TrackData data2) {
            Toast.makeText(getApplicationContext(), data2.name + " is clicked", Toast.LENGTH_SHORT).show();
            Log.d("TANCOLO", "===> data2.name = " + data2.name);
            // Open Activity
            Intent intent = new Intent(this, GridlayoutDetail.class);
            startActivity(intent);
        }
    }
    

    the activity_test_gridlayout is:

    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".recycleview.TestGridlayout">
    
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recycle_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    The class TrackAdapter:

    public class TrackAdapter extends RecyclerView.Adapter<TrackAdapter.ViewHolder> {
        ArrayList mValues;
        Context mContext;
        protected ItemListener2 mListener2;
        
        public TrackAdapter(Context context, ArrayList values, ItemListener2 itemListener2) {
            mValues = values;
            mContext = context;
            mListener2 = itemListener2;
        }
    
        public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    
            public TextView name;
            public TextView id;
            public LinearLayout linearLayout;
            TrackData data2;
    
            public ViewHolder(View v) {
                super(v);
                v.setOnClickListener(this);
                name = v.findViewById(R.id.text_view_name);
                id = v.findViewById(R.id.text_view_ID);
                linearLayout = v.findViewById(R.id.ll_container);
            }
    
            public void setData(TrackData data2) {
                this.data2 = data2;
                name.setText(data2.name);
                id.setText(data2.id);
            }
    
            @Override
            public void onClick(View view) {
                if (mListener2 != null) {
                    mListener2.onItemClick2(data2);
                }
            }
        }
    
        @Override
        public TrackAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_gridlayout, parent, false);
            return new TrackAdapter.ViewHolder(view);
        }
    
        @Override
        public void onBindViewHolder(@NonNull TrackAdapter.ViewHolder Vholder2, int position) {
            if(Vholder2 instanceof TrackAdapter.ViewHolder){
                ((TrackAdapter.ViewHolder)Vholder2).setData((TrackData) mValues.get(position));
            }
        }
    
        @Override
        public int getItemCount() {
            return mValues.size();
        }
    
        public interface ItemListener2 {
            void onItemClick2(TrackData data2);
        }
    }
    

    The related to item_layout:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/ll_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:context=".recycleview.TestGridlayout">
    
        <ImageView
            android:id="@+id/image_view_avatar"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:src="@mipmap/ic_launcher" />
    
        <TextView
            android:id="@+id/text_view_name"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            tools:text="user name" />
    
        <TextView
            android:id="@+id/text_view_ID"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_marginLeft="30dp"
            tools:text="12345678" />
    
    </LinearLayout>
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search