2
我正在使用此CarouselView library。使用回收器視圖和卡片視圖獲取旋轉木馬的點擊位置
我已使用Recycler視圖和卡片視圖來創建以下用戶界面。
在每個卡視圖,我已經加入此旋轉視圖,並且包含內輪播的圖像。
此庫有setImageClickListener
,它給出了每個卡片視圖中每個點擊圖像的索引。
customCarouselView.setImageClickListener(new ImageClickListener() {
@Override
public void onClick(int position) {
Toast.makeText(SampleCarouselViewActivity.this, "Clicked item: "+ position, Toast.LENGTH_SHORT).show();
}
});
我想知道哪個圖像被點擊以及從哪個圖像所屬的卡片,因爲有多個卡片視圖。
我試圖在ImageView中註冊點擊事件並使用getAdapterPosition()
獲取點擊卡片視圖的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點擊偵聽器,然後旋轉木馬查看點擊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.
}
};
}
}