如何参考物品点击recyclerview

时间:2019-02-17 05:47:00

标签: android android-recyclerview onitemclicklistener

假设我有一个滚动的RecyclerView(图像水平滚动),单击任何图像后,它将出现在recyclerView下方。

主要活动:

package com.aariyan.testm;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;`enter code here`

import com.daimajia.slider.library.Animations.DescriptionAnimation;
import com.daimajia.slider.library.Indicators.PagerIndicator;
import com.daimajia.slider.library.SliderLayout;`enter code here`
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.daimajia.slider.library.SliderTypes.TextSliderView;
import com.daimajia.slider.library.Tricks.ViewPagerEx;

import java.util.ArrayList;
import java.util.HashMap;


public class MainActivity extends AppCompatActivity {

    //implements BaseSliderView.OnSliderClickListener, ViewPagerEx.OnPageChangeListener

    private SliderLayout mDemoSlider;
    private ImageView imageView;

    private String selectDrinkCategory;
    private String drinks = "drinks";

    //String i = "0";

    int j;
    RecyclerViewAdapter recyclerViewAdapter;

    private int[] images = {R.drawable.a,
            R.drawable.bb,
            R.drawable.c,
            R.drawable.d,
            R.drawable.e,
            R.drawable.d_2_1,
            R.drawable.d_2_2,
            R.drawable.d_2_3,
            R.drawable.d_2_4

    };

    private ArrayList<Integer> inputImages = new ArrayList<>();

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

        //get Intent
        Intent intent = getIntent();
        if (getIntent() != null) {
            selectDrinkCategory = intent.getStringExtra("drinks");
        }

        initUI();
        initBitmapImages();
        initRecyclerView();

        Toast.makeText(this, "J: "+j, Toast.LENGTH_SHORT).show();

    }

    private void initRecyclerView() {
        LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(manager);
        RecyclerViewAdapter adapter = new RecyclerViewAdapter(inputImages, this);

        Toast.makeText(this, " Main Value j : " + adapter.index, Toast.LENGTH_SHORT).show();
        imageView.setImageResource(images[j]);
        recyclerView.setAdapter(adapter);
    }

    private void initBitmapImages() {
        for (int i = 0; i < images.length; i++) {
            // inputImages.add(String.valueOf(images[i]));
            inputImages.add(images[i]);
        }

    }

    private void initUI() {
        //mDemoSlider = (SliderLayout) findViewById(R.id.slider);
        imageView = findViewById(R.id.viewSingleImages);
        //imageView.setImageResource((images[0]));
    }

    @Override
    protected void onStop() {
        // To prevent a memory leak on rotation, make sure to call stopAutoCycle() on the slider before activity or fragment is destroyed
        super.onStop();
    }
}

RecyclerView: 

点击任意图像后,我都会看到一个滚动的RecyclerView(图像在水平方向滚动)

package com.aariyan.testm;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
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.Toast;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

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

    private ArrayList<Integer> totalImages = new ArrayList<>();
    private Context context;

    //MainActivity mainActivity;

    int index = 0;


    public RecyclerViewAdapter(ArrayList<Integer> totalImages, Context context) {

        this.totalImages = totalImages;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.images, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {

        //index = 0;
        Glide.with(context).asBitmap().load(totalImages.get(i)).into(viewHolder.singleImages);

        viewHolder.setItemClickListener(new ItemClickListener() {
            @Override
            public void onClick(View view, int position, boolean isClicked) {
                Toast.makeText(context, "TotalImages : "+totalImages.get(i), Toast.LENGTH_SHORT).show();
                index = totalImages.get(i);
            }
        });

//        viewHolder.singleImages.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Toast.makeText(context, "" + viewHolder.getAdapterPosition(), Toast.LENGTH_SHORT).show();
//                //viewHolder.imageView.setImageResource(totalImages.get(viewHolder.getAdapterPosition()));
//                //index = viewHolder.getAdapterPosition();
//                index = i;
//                Toast.makeText(context, "Index : " + index, Toast.LENGTH_SHORT).show();
//            }
//        });
    }

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


    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        public ImageView singleImages;
        private ItemClickListener itemClickListener;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            singleImages = itemView.findViewById(R.id.imagesViewOnScroll);
            itemView.setOnClickListener(this);
        }

        public void setItemClickListener (ItemClickListener itemClickListener) {
            this.itemClickListener = itemClickListener;
        }

        @Override
        public void onClick(View v) {
            itemClickListener.onClick(v,getAdapterPosition(),false);
        }
    }
}


activity_main.xml : just have a recyclerView and image View
and images.xml : only a image view to set on RecyclerView

1 个答案:

答案 0 :(得分:0)

要获得recyckerview的点击项目,您必须使用界面。

这是代码示例:

首先创建一个如下所示的界面:

public interface ImageClickListener {
    void onImageClick(String path);
}

现在在您的活动中,使适配器的对象像这样

 RecyclerViewAdapter adapter = new RecyclerViewAdapter(inputImages, this, new ImageClickListener() {
            @Override
            public void onImageClick(String path) {
                Glide.with(context).load(path).into(imageView);
            }
        });

现在创建您的适配器并创建一个像这样的构造器

 public RecyclerViewAdapter(ArrayList<Integer> totalImages, Context context, ImageClickListener imageClickListener) {
            this.totalImages = totalImages;
            this.context = context;
            this.imageClickListener = imageClickListener;
        }

现在您有一个接口实例,其中有一个名为onImageClick()的方法,将在其中传递单击图像的路径。

点击图片后,请执行以下代码:

 viewHolder.singleImages.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imageClickListener.onImageClick(totalImages.get(viewHolder.getAdapterPosition()));
            }
        });

现在您的接口方法将调用&它将执行活动的图像视图以设置给定的路径。

享受编码:)