使用Recycler View和Card View获取旋转木马的点击位置

时间:2017-08-31 15:46:10

标签: java android android-layout android-fragments

我正在使用此CarouselView library

我使用了Recycler View和Card View来创建以下UI。

enter image description here

在每张卡片视图中,我添加了此轮播视图,并在旋转木马内包含图像。

此库具有setImageClickListener,它为每张卡片视图中的每个点击图片提供索引。

customCarouselView.setImageClickListener(new ImageClickListener() {
        @Override
        public void onClick(int position) {
            Toast.makeText(SampleCarouselViewActivity.this, "Clicked item: "+ position, Toast.LENGTH_SHORT).show();
        }
    });

我想知道点击了哪张图片以及该图片属于哪张卡片,因为有多张卡片视图。

我尝试在ImageView中注册click事件,并使用adapterPosition获取点击的cardview的getAdapterPosition()

fruitImageView.setOnClickListener(new View.OnClickListener() {   /*Hits only when another click event (setImageClickListner) is not registered*/
                @Override
                public void onClick(View view) {
                    int a = getAdapterPosition(); //gives the index of clicked cardview position.
                }
            });

当两个事件都被注册后,只有点击ImageView(fruitImageView.setOnClickListener)才会被命中,这会给出适配器的位置。我也想知道点击了哪个图像。

当我删除此ImageView Click侦听器时,轮播视图单击customCarouselView.setImageClickListener会被点击,这会给出单击图像的位置而不是适配器位置。

这有什么工作吗?请帮忙。

我的适配器类完整代码:

public class GridAdapter extends RecyclerView.Adapter<GridAdapter.ViewHolder> {
private ArrayList<HomeSalesModel> salesLists;
int[] sampleImages = {R.drawable.album1, R.drawable.album2, R.drawable.album3, R.drawable.album4, R.drawable.album5};
String[] sampleTitles = {"A", "B", "C", "D", "E"};
Context myContext = null;

public GridAdapter(ArrayList<HomeSalesModel> salesLists) {
    this.salesLists = salesLists;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    myContext = parent.getContext();
    return new ViewHolder(LayoutInflater.from(parent.getContext()), parent);
}

@Override
public void onBindViewHolder(GridAdapter.ViewHolder viewHolder, int i) {
    viewHolder.tv_country.setText(salesLists.get(i).getTitle());
}

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

public class ViewHolder extends RecyclerView.ViewHolder {
    private CarouselView customCarouselView;
    private TextView tv_country;
    private CardView cardView;

    public ViewHolder(LayoutInflater inflater, ViewGroup parent) {
        super(inflater.inflate(R.layout.grid_card_row, parent, false));
        tv_country = (TextView) itemView.findViewById(R.id.tv_country);
        customCarouselView = (CarouselView) itemView.findViewById(R.id.customCarouselView);
        cardView = (CardView) itemView.findViewById(R.id.card);
        customCarouselView.setPageCount(sampleImages.length);
        //customCarouselView.setSlideInterval(4000);
        customCarouselView.setViewListener(viewListener);
        customCarouselView.setImageClickListener(imageClickListner);
    }

    ViewListener viewListener = new ViewListener() {
        @Override
        public View setViewForPosition(int position) {

            LayoutInflater li = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View customView = li.inflate(R.layout.view_custom, null);

            TextView labelTextView = (TextView) customView.findViewById(R.id.labelTextView);
            ImageView fruitImageView = (ImageView) customView.findViewById(R.id.fruitImageView);
            fruitImageView.setImageResource(sampleImages[position]);
            labelTextView.setText(sampleTitles[position]);
            fruitImageView.setOnClickListener(new View.OnClickListener() {   /*Works when another click event (imageClickListner) is not registered*/
                @Override
                public void onClick(View view) {
                    int a = getAdapterPosition(); //gives the index cardview position.
                }
            });
            //labelTextView.setText(salesLists.get(position).getProducts().get(position).getSlug());
            return customView;
        }
    };

    ImageClickListener imageClickListner = new ImageClickListener() {
        @Override
        public void onClick(int position) {
            Toast.makeText(myContext, "Clicked item: " + position, Toast.LENGTH_SHORT).show();  //gives index the clicked image.
        }
    };
}

}

1 个答案:

答案 0 :(得分:0)

只需在ImageClickListener内获取适配器位置。

int postion = getAdapterPosition();