使用recycleView处理元素的单击

时间:2017-05-25 14:01:00

标签: java android android-recyclerview

我使用recycleView / CardLayout建立一个简单的应用程序,我跟着this教程。

我看到许多问题已经解决了卡片点击问题,但我需要的是在用户点击标题或用户点击每张卡片中的图片时处理不同的操作。

这就是我现在所拥有的:

public class SimiliarPlantsAdapter extends RecyclerView.Adapter<SimiliarPlantsAdapter.PlantViewHolder>{

    ArrayList<Plant> plants = new ArrayList<Plant>();
    Context context;

    public static class PlantViewHolder extends RecyclerView.ViewHolder {
        CardView cv;
        TextView plantName;
        CheckBox plantCheck;
        ImageView plantPhoto;

        PlantViewHolder(View itemView) {
            super(itemView);
            cv = (CardView)itemView.findViewById(R.id.cv);
            plantName = (TextView)itemView.findViewById(R.id.plantName);
            plantCheck = (CheckBox)itemView.findViewById(R.id.plantCheck);
            plantPhoto = (ImageView)itemView.findViewById(R.id.plantPhoto);
        }

    }

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

    @Override
    public void onBindViewHolder(PlantViewHolder holder, int position) {
        holder.plantName.setText(plants.get(position).getSpecie());
        holder.plantCheck.setText("Are you sure this is the plant?");


        Log.d("foto",String.valueOf(holder.plantName));


        String urlFoto = "http://10.0.2.2:3000/images/" + holder.plantName.getText().toString() + "/Thumbnail.jpg";
        Picasso.with(context)
                .load(urlFoto)
                .resize(250, 250)
                .into(holder.plantPhoto);

    }

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

    public SimiliarPlantsAdapter(ArrayList<Plant> plants,Context context) {
        this.plants = plants;
        this.context = context;
    }

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

}

我的活动

public class SimiliarPhotos extends AppCompatActivity implements IResult {

    RecyclerView rv;
    LinearLayoutManager llm;
    ArrayList<Plant> plants = new ArrayList<Plant>();
    SimiliarPlantsAdapter adapter;

    VolleyService mVolleyService;
    IResult mResultCallback = null;
    final String GETREQUEST = "GETCALL";

    //login url connection
    final String URL = "http://10.0.2.2:3000/plants";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_similiar_photos);

        rv = (RecyclerView)findViewById(R.id.rv);
        llm = new LinearLayoutManager(this);
        llm.setAutoMeasureEnabled(true);
        rv.setLayoutManager(llm);


        initializeAdapter();

        initVolleyCallback();

        mVolleyService = new VolleyService(mResultCallback,this);

        mVolleyService.getDataVolley(GETREQUEST,URL);
    }


    @Override
    public void notifySuccess(String requestType, JSONObject response) {
        Log.d("resposta",response.toString());
    }

    @Override
    public void notifySuccess(String requestType, JSONArray response) {
        Log.d("resposta",response.toString());
    }

    @Override
    public void notifyError(String requestType, VolleyError error) {
        Log.d("resposta",error.toString());
    }

    void initVolleyCallback(){
        mResultCallback = new IResult() {
            @Override
            public void notifySuccess(String requestType, JSONObject response) {
            }

            @Override
            public void notifySuccess(String requestType, JSONArray response) {
                Plant plant;
                Log.d("ENTERED","ENTEREDHERE1");
                // iterate over the JSONArray response
                for (int i=0; i < response.length(); i++) {
                    try {
                        JSONObject object = response.getJSONObject(i); // get the individual object from JSONArray
                        int id = Integer.parseInt(object.getString("id")); // get the unique identifier from the object
                        String specie = object.getString("specie"); // get the name of the specie from the object
                        String description = object.getString("description"); // get the description of the object
                        plant = new Plant(id,specie,description); // construct the object
                        Log.d("plant",String.valueOf(plant));
                        plants.add(plant); // add the object to the arraylist so it can be used on the cardLayout

                    } catch (JSONException e) {
                        Log.d("ENTERED",e.toString());
                        e.printStackTrace();
                    }
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void notifyError(String requestType, VolleyError error) {
                Log.d("resposta",error.toString());
            }
        };
    }

    public void initializeAdapter(){
        Log.d("plants",String.valueOf(plants.size()));
        adapter = new SimiliarPlantsAdapter(plants,SimiliarPhotos.this);
        rv.setAdapter(adapter);
    }

}

volley init是请求,对问题不是那么重要,因为我正确地获取了数据

1 个答案:

答案 0 :(得分:0)

首先,您必须添加界面来处理Adapter中的点击并将其添加到构造函数中:

    public class SimiliarPlantsAdapter extends RecyclerView.Adapter<SimiliarPlantsAdapter.PlantViewHolder>{

  private OnItemClickListener listener;

    public interface OnItemClickListener {
            void onTitleClicked(int position, String title, View clickedview);
            void onImageClicked(int position, View clickedview);
        }

    }
    public SimiliarPlantsAdapter(ArrayList<Plant> plants,Context context, OnItemClickListener listener ) {
        this.plants = plants;
        this.context = context;
        this.listener = listener;
    }

现在,您必须在onClick内的onBindViewHolder方法中进行设置:

 holder.plantName.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onTitleClicked(holder.getAdapterPosition(), plants.get(position).getSpecie(), view);
                    }
                }
            });

 holder.plantPhoto.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClicked(holder.getAdapterPosition(), view);
                    }
                }
            });
        }
    }

最后,在Activity

中实施界面
    public class SimiliarPhotos extends AppCompatActivity implements IResult, SimiliarPlantsAdapter.OnItemClickListener {

     @Override
        public void onTitleClicked(int position, String title, View clickedView) {
           //Handle your title click here
        }

      @Override
        public void onImageClicked(int position, View clickedView) {
            //Handle your image click here
        }

 public void initializeAdapter(){
        Log.d("plants",String.valueOf(plants.size()));
        adapter = new SimiliarPlantsAdapter(plants,SimiliarPhotos.this, SimilarPhotos.this);
        rv.setAdapter(adapter);
 }