点击RecyclerView转到另一个带有android数据的活动

时间:2017-05-14 08:48:04

标签: android android-recyclerview android-cardview recycler-adapter

我使用RecyclerVeiw显示一些图像,点击它应该启动显示ID,RecyclerView名称的新活动。我实现了代码,但它现在正在工作,它不可点击。下面是我的适配器

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);


        }
    }

}

和我的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;
    }
}

和我的片段

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()));
    }


}

当我点击cardview时没有发生任何事情,也没有错误信息。以下是我的应用屏幕截图

enter image description here

请对任何疑问发表评论。

1 个答案:

答案 0 :(得分:1)

第1步:制作如下代码的界面:

public interface RecyclerViewItemClickListener {
    void onClickListenerForItem(int position);

   }

步骤2:实现此界面:

 RecyclerViewItemClickListener recyclerItemClickListener = new RecyclerViewItemClickListener() {

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

步骤3:在适配器类中发送已实现的接口,如下面的代码:

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

步骤4:在Adapter类中实现位置回调:

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

    }

步骤5:最终在OnBindViewHolder()中设置位置:

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