1

Hello I made a recycler view and I don't know how to configure for Onclick function on these items which can open another activity name (xyz.xml)

Main Activity.java

public class MainActivity extends AppCompatActivity {

    List<GetDataAdapter> GetDataAdapter1;

    RecyclerView recyclerView;

    RecyclerView.LayoutManager recyclerViewlayoutManager;

    RecyclerView.Adapter recyclerViewadapter;

    String GET_JSON_DATA_HTTP_URL = "http://platinummun.com/android_login_api/ImageJsonData.php";
    String JSON_IMAGE_TITLE_NAME = "image_title";
    String JSON_IMAGE_URL = "image_url";

    JsonArrayRequest jsonArrayRequest ;

    RequestQueue requestQueue ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        GetDataAdapter1 = new ArrayList<>();

        recyclerView = (RecyclerView) findViewById(R.id.recyclerview1);

        recyclerView.setHasFixedSize(true);

        recyclerViewlayoutManager = new LinearLayoutManager(this);

        recyclerView.setLayoutManager(recyclerViewlayoutManager);


                JSON_DATA_WEB_CALL();


    }

    public void JSON_DATA_WEB_CALL(){

        jsonArrayRequest = new JsonArrayRequest(GET_JSON_DATA_HTTP_URL,

                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {

                        JSON_PARSE_DATA_AFTER_WEBCALL(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });

        requestQueue = Volley.newRequestQueue(this);

        requestQueue.add(jsonArrayRequest);
    }



    public void JSON_PARSE_DATA_AFTER_WEBCALL(JSONArray array){

        for(int i = 0; i<array.length(); i++) {

            GetDataAdapter GetDataAdapter2 = new GetDataAdapter();

            JSONObject json = null;
            try {

                json = array.getJSONObject(i);

                GetDataAdapter2.setImageTitleNamee(json.getString(JSON_IMAGE_TITLE_NAME));

                GetDataAdapter2.setImageServerUrl(json.getString(JSON_IMAGE_URL));

            } catch (JSONException e) {

                e.printStackTrace();
            }
            GetDataAdapter1.add(GetDataAdapter2);
        }

        recyclerViewadapter = new RecyclerViewAdapter(GetDataAdapter1, this);

        recyclerView.setAdapter(recyclerViewadapter);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.android_examples.recyclerviewimagelistview_android_examplescom.MainActivity">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</RelativeLayout>

recyclerview_items.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cardview1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardElevation="3dp"
    card_view:contentPadding="3dp"
    card_view:cardCornerRadius="3dp"
    card_view:cardMaxElevation="3dp"
    >


    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <com.android.volley.toolbox.NetworkImageView
            android:id="@+id/VollyNetworkImageView1"
            android:layout_width="150dp"
            android:layout_height="100dp"
            android:src="@mipmap/ic_launcher"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Image Name"
            android:id="@+id/textView_item"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/VollyNetworkImageView1"
            android:layout_toEndOf="@+id/VollyNetworkImageView1"
            android:layout_marginLeft="20dp"/>

    </RelativeLayout>

</android.support.v7.widget.CardView>

Check image here

Please help me to do it

Igor Tyulkanov
  • 5,327
  • 2
  • 28
  • 48
Counter Aim
  • 11
  • 1
  • 7

4 Answers4

0

You can try this

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
        private ArrayList<String> list;

        public class MyViewHolder extends RecyclerView.ViewHolder {
            public TextView text;
            public String result;
            public MyViewHolder(View view) {
                super(view);
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //TODO startintent
                    }
                });
            }
        }

        public MyAdapter(ArrayList<String> list) {
            this.list = list;
        }

        // Create new views (invoked by the layout manager)
        @Override
        public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
                                                           int viewType) {
            // create a new view
            View v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.view, parent, false);

            MyViewHolder vh = new MyViewHolder(v);
            return vh;
        }

        // Replace the contents of a view (invoked by the layout manager)
        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            // - get element from your dataset at this position
            // - replace the contents of the view with that element
            holder.result = list.get(position);
            holder.text.setText(list.get(position).getContentTitle());
        }

        @Override
        public int getItemCount() {
            return list.size();
        }
    }
Muhammed GÜNEŞ
  • 185
  • 1
  • 13
0

You can use ItemClickSupport class to attach a listener to items in your RecyclerView

Hussain
  • 739
  • 6
  • 16
0

In your Adapter class,

you can implement click listener like this

@Override
public void onBindViewHolder(final YourHolder holder, final int position) {
    holder.cardView1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          Intent intent = new Intent(context,NextActivity.class);
        //if you want to pass data
          intent.putExtra("hi",list.get(position).getHi());
          startActivity(intent);
        }
    });
}
Ajay Shrestha
  • 2,283
  • 1
  • 16
  • 25
0

You can create an Interface in your adapter for items click.

For example:

interface ItemClickListener
{
      void onItemClicked();
}

View Holder:

 public static class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private YourModel mItem;
        private ItemClickListener mItemListener;

          public ItemViewHolder(View itemView, ItemClickListener listener) {
               super(itemView);
               ButterKnife.bind(this, itemView);
               mItemListener = listener;
               itemView.setOnClickListener(this);
      }

        @Override
    public void onClick(View v) {



        switch (v.getId()) {
            case R.id.view_id:
                mItemListener.onItemClicked(); // You can send any field or model as a param here.
                break;

            default:
                break;
        }

    }

 }

After that you can implement this interface in your Activity/Fragment and can write code for click event.

Ashish Tiwari
  • 1,881
  • 2
  • 25
  • 49