设置onclick for recycleview每个视图并烘烤其参数

时间:2017-12-10 06:37:55

标签: android android-recyclerview

您好我想设置我的循环视图可点击。例如,当点击recycleview时,每个视图都会覆盖其参数(如名称)。请不要排名否定我是新手。

这是我显示recycleview的主要类

public class HomeFragment2 extends Fragment {

    List<GetDataAdapter> GetDataAdapter1;

    RecyclerView recyclerView;

    RecyclerView.LayoutManager recyclerViewlayoutManager;

    RecyclerView.Adapter recyclerViewadapter;


    ProgressBar progressBar;

    View view ;

    int RecyclerViewItemPosition ;

    ArrayList<String> ImageTitleNameArrayListForClick;


    String ID = "id";
    String SECTION = "section";
    String TIME = "time";
    String NAME = "name";

    Button button;

    JsonArrayRequest jsonArrayRequest ;

    RequestQueue requestQueue ;


    public static HomeFragment2 newInstance() {
        return new HomeFragment2();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_home2, container, false);

        GetDataAdapter1 = new ArrayList<>();

        recyclerView =v.findViewById(R.id.recyclerView1);

        progressBar =v.findViewById(R.id.progressBar1);

        button =v.findViewById(R.id.button);

        recyclerView.setHasFixedSize(true);

        recyclerViewlayoutManager = new LinearLayoutManager(getActivity());

        recyclerView.setLayoutManager(recyclerViewlayoutManager);


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                progressBar.setVisibility(View.VISIBLE);
                JSON_DATA_WEB_CALL();

            }
        });


        return v;
    }
    public void JSON_DATA_WEB_CALL(){

        jsonArrayRequest = new JsonArrayRequest(URLS.recycle_fetch,

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

                        progressBar.setVisibility(View.GONE);

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

                    }
                });

        requestQueue = Volley.newRequestQueue(getActivity());

        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.setId(json.getInt(ID));

                GetDataAdapter2.setName(json.getString(SECTION));

                GetDataAdapter2.setSubject(json.getString(TIME));

                GetDataAdapter2.setPhone_number(json.getString(NAME));

                // Adding image title name in array to display on RecyclerView click event.

            } catch (JSONException e) {

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

        recyclerViewadapter = new RecyclerViewAdapter(GetDataAdapter1, getActivity());

        recyclerView.setAdapter(recyclerViewadapter);
    }
}

和我的适配器

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

Context context;

List<GetDataAdapter> getDataAdapter;

public RecyclerViewAdapter(List<GetDataAdapter> getDataAdapter, Context context){

    super();

    this.getDataAdapter = getDataAdapter;
    this.context = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview, parent, false);


    cardView = (CardView) view.findViewById(R.id.mainCard);



    ViewHolder viewHolder = new ViewHolder(v);


    return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

    GetDataAdapter getDataAdapter1 =  getDataAdapter.get(position);

    holder.Id.setText(getDataAdapter1.getName());

    holder.Section.setText(String.valueOf(getDataAdapter1.getId()));

    holder.Time.setText(getDataAdapter1.getPhone_number());

    holder.Name.setText(getDataAdapter1.getSubject());
    cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //do something
        }
    });

}

@Override
public int getItemCount() {

    return getDataAdapter.size();
}

class ViewHolder extends RecyclerView.ViewHolder{

    public TextView Id;
    public TextView Section;
    public TextView Time;
    public TextView Name;


    public ViewHolder(View itemView) {

        super(itemView);

        Id =itemView.findViewById(R.id.textView2);
        Section =itemView.findViewById(R.id.textView4);
        Time =itemView.findViewById(R.id.textView6);
        Name =itemView.findViewById(R.id.textView8);


    }
}
}

任何人都可以帮助我吗?我想在点击它们时获得cardview的参数。

1 个答案:

答案 0 :(得分:0)

    public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

    Context context;

    List<GetDataAdapter> getDataAdapter;

    public RecyclerViewAdapter(List<GetDataAdapter> getDataAdapter, Context context){

        super();

        this.getDataAdapter = getDataAdapter;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview, parent, false);




//////////////////////////////////here you define your layout you wanna be clicked/////////////////////////

cardView = (CardView) view.findViewById(R.id.mainCard);








        ViewHolder viewHolder = new ViewHolder(v);


        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        GetDataAdapter getDataAdapter1 =  getDataAdapter.get(position);

        holder.Id.setText(getDataAdapter1.getName());

        holder.Section.setText(String.valueOf(getDataAdapter1.getId()));

        holder.Time.setText(getDataAdapter1.getPhone_number());

        holder.Name.setText(getDataAdapter1.getSubject());



///////////////////////////////// and here you add your on click listener////////////////////////////////

cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //do something
            }
        });







    }

    @Override
    public int getItemCount() {

        return getDataAdapter.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{

        public TextView Id;
        public TextView Section;
        public TextView Time;
        public TextView Name;


        public ViewHolder(View itemView) {

            super(itemView);

            Id =itemView.findViewById(R.id.textView2);
            Section =itemView.findViewById(R.id.textView4);
            Time =itemView.findViewById(R.id.textView6);
            Name =itemView.findViewById(R.id.textView8);


        }
    }
    }