on click RecyclerView go to another activity with data in android

我的未来我决定 提交于 2019-12-24 01:13:27

问题


i am using RecyclerVeiw to display some images and on click its should start New Activity showing id, name of the RecyclerView. I implemented the code but its now working, its not clickable. below is my adapter

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;

import zesteve.com.myapplication.Catagory;
import zesteve.com.myapplication.CatagoryVendListActivity;
import zesteve.com.myapplication.R;

/**
 * Created by Ravi Shankar on 5/13/2017.
 */

public class CatagoryAdapter extends RecyclerView.Adapter<CatagoryAdapter.MyViewHolder>{

    private Context mContext;
    private ArrayList<Catagory> CatagoryList;

    public CatagoryAdapter(Context mContext, ArrayList<Catagory> CatagoryList) {
        this.mContext = mContext;
        this.CatagoryList = CatagoryList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.catagory_card, parent, false);

        return new MyViewHolder(itemView,mContext,CatagoryList);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        Catagory catagory = CatagoryList.get(position);
        holder.title.setText(catagory.getName());
        holder.count.setText(catagory.getNumOfVend() + " Places");

        // loading album cover using Picasso library
        Picasso.with(mContext).load(catagory.getThumbnail()).into(holder.thumbnail);
    }

    @Override
    public int getItemCount() {
        return CatagoryList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        public TextView title, count;
        public ImageView thumbnail;

        ArrayList<Catagory> CatagoryList = new ArrayList<Catagory>();
        Context mContext;

        public MyViewHolder(View view , Context mContext, ArrayList<Catagory> CatagoryList ) {
            super(view);
            this.CatagoryList = CatagoryList;
            this.mContext = mContext;
            view.setOnClickListener(this);
            title = (TextView) view.findViewById(R.id.title);
            count = (TextView) view.findViewById(R.id.count);
            thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
        }

        @Override
        public void onClick(View view) {
            int position = getAdapterPosition();
            Catagory catagory = this.CatagoryList.get(position);
            Intent intent = new Intent(this.mContext, CatagoryVendListActivity.class);
            intent.putExtra("CatId",catagory.getVendId());
            intent.putExtra("CatName",catagory.getName());
            this.mContext.startActivity(intent);


        }
    }

}

and my Catatgory.java

public class Catagory {
    private String name;
    private int numOfVend;
    private int thumbnail;
    private int VendId;

    public Catagory() {
    }

    public Catagory(String name, int numOfVend, int VendId , int thumbnail) {
        this.name = name;
        this.VendId = VendId;
        this.numOfVend = numOfVend;
        this.thumbnail = thumbnail;

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getNumOfVend() {
        return numOfVend;
    }

    public void setNumOfVend(int numOfVend) {
        this.numOfVend = numOfVend;
    }

    public int getThumbnail() {
        return thumbnail;
    }

    public void setThumbnail(int thumbnail) {
        this.thumbnail = thumbnail;
    }

    public int getVendId() {
        return VendId;
    }

    public void setVendId(int VendId) {
        this.VendId = VendId;
    }
}

and my fragment

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;

import zesteve.com.myapplication.adapter.CatagoryAdapter;

/**
 * Created by Ratan on 7/29/2015.
 */
public class PrimaryFragment extends Fragment {

    FloatingActionButton fab;

    private RecyclerView recyclerView;
    private  CatagoryAdapter adapter;
    private  ArrayList<Catagory> catagoryList;

    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.primary_layout,container,false);

        recyclerView = (RecyclerView) root.findViewById(R.id.recycler_view);

        fab = (FloatingActionButton) root.findViewById(R.id.post_new_event);

        catagoryList = new ArrayList<>();
        adapter = new CatagoryAdapter(getActivity(), catagoryList);

        RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 2);
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(10), true));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(adapter);

         prepareCatagory();

        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(),PostNewEvent.class);
                startActivity(intent);
            }
        });
        return root;
    }

    private void prepareCatagory() {
        int[] covers = new int[]{
                R.drawable.album1,
                R.drawable.album2,
                R.drawable.album3,
                R.drawable.album4,
                R.drawable.album5,
                R.drawable.album6,
                R.drawable.album7,
                R.drawable.album8,
                R.drawable.album9,
                R.drawable.album10,
                R.drawable.album11
        };

        Catagory a = new Catagory("True Romance", 13, 1, covers[0]);
        catagoryList.add(a);

        a = new Catagory("Xscpae", 8, 2 ,covers[1]);
        catagoryList.add(a);

        a = new Catagory("Maroon 5",3 ,11, covers[2]);
        catagoryList.add(a);

        a = new Catagory("Born to Die",4, 12, covers[3]);
        catagoryList.add(a);

        a = new Catagory("Honeymoon",5, 14, covers[4]);
        catagoryList.add(a);

        a = new Catagory("I Need a Doctor",6, 1, covers[5]);
        catagoryList.add(a);

        a = new Catagory("Loud", 11,7, covers[6]);
        catagoryList.add(a);

        a = new Catagory("Legend", 14,8, covers[7]);
        catagoryList.add(a);

        a = new Catagory("Hello", 11,9, covers[8]);
        catagoryList.add(a);

        a = new Catagory("Greatest Hits",10, 17, covers[9]);
        catagoryList.add(a);

        adapter.notifyDataSetChanged();
    }

    /**
     * RecyclerView item decoration - give equal margin around grid item
     */
    public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {

        private int spanCount;
        private int spacing;
        private boolean includeEdge;

        public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {
            this.spanCount = spanCount;
            this.spacing = spacing;
            this.includeEdge = includeEdge;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            int position = parent.getChildAdapterPosition(view); // item position
            int column = position % spanCount; // item column

            if (includeEdge) {
                outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
                outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)

                if (position < spanCount) { // top edge
                    outRect.top = spacing;
                }
                outRect.bottom = spacing; // item bottom
            } else {
                outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
                outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f /    spanCount) * spacing)
                if (position >= spanCount) {
                    outRect.top = spacing; // item top
                }
            }
        }
    }

    /**
     * Converting dp to pixel
     */
    private int dpToPx(int dp) {
        Resources r = getResources();
        return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()));
    }


}

when i click on cardview nothing is happening, no error message also. Below is my app screen shot

please comment for any doubts.


回答1:


Step-1: Make an interface like below code :

public interface RecyclerViewItemClickListener {
    void onClickListenerForItem(int position);

   }

Step-2: Implements this interface :

 RecyclerViewItemClickListener recyclerItemClickListener = new RecyclerViewItemClickListener() {

        @Override
        public void onClickListenerForItem(int tournamentIndex, int matchIndex) {
//Todo implement your Code here
           }
}

Step-3: send implemented interface in adapter class like below code:

mAdapter = new RecyclerViewAdapte(getContext(), recyclerItemClickListener);

Step-4: Implement callback for position in Adapter class:

 private final RecyclerViewItemClickListener mListener;
 public RecyclerViewAdapter(Context context, RecyclerViewItemClickListener mListener) {
        this.context = context;
        this.mListener = mListener;

    }

Step-5: finaly set position in OnBindViewHolder():

holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mListener.onClickListenerForItem(position);
        }
    });


来源:https://stackoverflow.com/questions/43962027/on-click-recyclerview-go-to-another-activity-with-data-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!