gpt4 book ai didi

java - 使用 Recycler View 和 Card View 获取轮播的点击位置

转载 作者:行者123 更新时间:2023-11-29 02:37:49 24 4
gpt4 key购买 nike

我正在使用这个 CarouselView library .

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

enter image description here

在每个卡片 View 中,我都添加了这个轮播 View 并在轮播中包含图像。

这个库有 setImageClickListener,它给出每个卡片 View 中每个点击图像的索引。

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

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

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

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 监听器时,旋转木马 View 点击 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.
}
};
}

}

最佳答案

只需在 ImageClickListener 中获取适配器位置即可。

int postion = getAdapterPosition();

关于java - 使用 Recycler View 和 Card View 获取轮播的点击位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45985465/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com