如何为回收站设置OnClick查看具有位置

时间:2017-05-25 01:58:08

标签: java android

我刚刚创建了一个包含卡片的回收者视图所有我想要setOnCLick用于此回收者视图吐司与位置项目点击它是第一次使用回收者视图我总是使用列表视图与setOnItemClickListner所以 这是我的适配器类

package abtech.waiteriano.com.retailstar.adapters;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import abtech.waiteriano.com.retailstar.R;

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> {

    public static class PersonViewHolder extends RecyclerView.ViewHolder {

        CardView cv;
        Context c;
        TextView InvoiceNumber;
        TextView InvoiceType;
        TextView InvoiceDate;
        TextView Status;
        TextView CustomerID;

        PersonViewHolder(View itemView) {
            super(itemView);
            cv = (CardView)itemView.findViewById(R.id.cv);
            InvoiceNumber = (TextView)itemView.findViewById(R.id.invoiceNo);
            InvoiceType = (TextView)itemView.findViewById(R.id.tvType);
            InvoiceDate = (TextView)itemView.findViewById(R.id.tvDate);
            Status = (TextView)itemView.findViewById(R.id.statusTv);
            CustomerID = (TextView)itemView.findViewById(R.id.cutomerId);
        }
    }

    List<Invoice> Invoices;

    RVAdapter(List<Invoice> Invoices){
        this.Invoices = Invoices;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    @Override
    public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
        PersonViewHolder pvh = new PersonViewHolder(v);
        return pvh;
    }

    @Override
    public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
        personViewHolder.InvoiceNumber.setText(Invoices.get(i).invoiceNo);
        personViewHolder.InvoiceType.setText(Invoices.get(i).invoiceType);
        personViewHolder.InvoiceDate.setText(Invoices.get(i).invoiceDate);
        personViewHolder.Status.setText(Invoices.get(i).status);
        personViewHolder.CustomerID.setText(Invoices.get(i).cutomerId);

    }

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

这里是包含回收器VIew的片段类

package abtech.waiteriano.com.retailstar.adapters;

import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import abtech.waiteriano.com.retailstar.R;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link InvoiceFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link InvoiceFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class InvoiceFragment extends Fragment {
    // TODO: Reinvoice_number parameter arguments, choose invoice_numbers that match
    View rootView;
    private List<Invoice> Invoices;
    private RecyclerView rv;




    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Reinvoice_number and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    public InvoiceFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment InvoiceFragment.
     */
    // TODO: Reinvoice_number and change types and number of parameters
    public static InvoiceFragment newInstance(String param1, String param2) {
        InvoiceFragment fragment = new InvoiceFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView = inflater.inflate(R.layout.fragment_invoice, container, false);
        rv=(RecyclerView)rootView.findViewById(R.id.rv);

        LinearLayoutManager llm = new LinearLayoutManager(getContext());
        rv.setLayoutManager(llm);
        rv.setHasFixedSize(true);

        initializeData();
        initializeAdapter();


        return rootView;

    }
    private void initializeData(){
        Invoices = new ArrayList<>();
        Invoices.add(new Invoice("204", "Regular","15/5/2017","Regular","100200000194"));
        Invoices.add(new Invoice("205", "Regular","20/5/2017","Regular","100200000195"));
        Invoices.add(new Invoice("206", "Regular","30/5/2017","Regular","100200000196"));
        Invoices.add(new Invoice("207", "Regular","5/6/2017","Regular","100200000197"));
        Invoices.add(new Invoice("208", "Regular","10/6/2017","Regular","100200000198"));
        Invoices.add(new Invoice("209", "Regular","15/6/2017","Regular","100200000199"));
        Invoices.add(new Invoice("210", "Regular","20/6/2017","Regular","100200000200"));
        Invoices.add(new Invoice("211", "Regular","25/6/2017","Regular","100200000201"));
        Invoices.add(new Invoice("212", "Regular","30/6/2017","Regular","100200000202"));
    }

    private void initializeAdapter(){
        RVAdapter adapter = new RVAdapter(Invoices);
        rv.setAdapter(adapter);
    }

    // TODO: Reinvoice_number method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and invoice_number
        void onFragmentInteraction(Uri uri);
    }
}

2 个答案:

答案 0 :(得分:3)

您需要做的就是在您的recyclerview适配器中的PersonViewHolder类中实现onClickListener,如下所示:

  1. 制作构造函数

  2. 在itemview

  3. 上设置onClickListener
  4. 为点击的项目

  5. 在onClick函数中设置toast

    最终编辑将如下所示 -

    public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> {
    
        private Context mContext;
    
        public RVAdapter(Context context) {
                mContext = context;
    
            }
    
        public static class PersonViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    
            CardView cv;
            Context c;
            TextView InvoiceNumber;
            TextView InvoiceType;
            TextView InvoiceDate;
            TextView Status;
            TextView CustomerID;
    
            PersonViewHolder(View itemView) {
                super(itemView);
                cv = (CardView)itemView.findViewById(R.id.cv);
                InvoiceNumber = (TextView)itemView.findViewById(R.id.invoiceNo);
                InvoiceType = (TextView)itemView.findViewById(R.id.tvType);
                InvoiceDate = (TextView)itemView.findViewById(R.id.tvDate);
                Status = (TextView)itemView.findViewById(R.id.statusTv);
                CustomerID = (TextView)itemView.findViewById(R.id.cutomerId);
                //set OnclickListener
                itemView.setOnClickListener(this);
            }
            @Override
            public void onClick(View view) {
    
                int position = getAdapterPosition();
                //Do whatever you what with the position
                Toast.makeText(mContext, "Clicked Position"+position, Toast.LENGTH_SHORT).show();
    
            }
        }
    
        List<Invoice> Invoices;
    
        RVAdapter(List<Invoice> Invoices){
            this.Invoices = Invoices;
        }
    
        @Override
        public void onAttachedToRecyclerView(RecyclerView recyclerView) {
            super.onAttachedToRecyclerView(recyclerView);
        }
    
        @Override
        public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
            PersonViewHolder pvh = new PersonViewHolder(v);
            return pvh;
        }
    
        @Override
        public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
            personViewHolder.InvoiceNumber.setText(Invoices.get(i).invoiceNo);
            personViewHolder.InvoiceType.setText(Invoices.get(i).invoiceType);
            personViewHolder.InvoiceDate.setText(Invoices.get(i).invoiceDate);
            personViewHolder.Status.setText(Invoices.get(i).status);
            personViewHolder.CustomerID.setText(Invoices.get(i).cutomerId);
    
        }
    
        @Override
        public int getItemCount() {
            return Invoices.size();
        }
    }
    

答案 1 :(得分:0)

您可以从构造函数获取上下文,然后在onBindViewHolder()中创建toast:

Context context;
RVAdapter(Context context, List<Invoice> Invoices){
    this.Invoices = Invoices;
    this.context = context;
}

@Override
public void onBindViewHolder(PersonViewHolder personViewHolder, int position) {
    personViewHolder.InvoiceNumber.setText(Invoices.get(i).invoiceNo);
    personViewHolder.InvoiceType.setText(Invoices.get(i).invoiceType);
    personViewHolder.InvoiceDate.setText(Invoices.get(i).invoiceDate);
    personViewHolder.Status.setText(Invoices.get(i).status);
    personViewHolder.CustomerID.setText(Invoices.get(i).cutomerId);

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeToast(context, "Clicked position is : "+position, Toast.LENGTH_SHORT);
        }
    });
}

在片段中:

RVAdapter adapter = new RVAdapter(getActivity(), Invoices);