gpt4 book ai didi

Android:处理放置在 RecyclerView 中的 ImageView 上的点击

转载 作者:太空宇宙 更新时间:2023-11-03 10:42:07 26 4
gpt4 key购买 nike

我刚从 ListView 移动到 RecyclerView,我遇到了一个问题:我无法让 ImageView 的 onClickListener 工作。基本上我想要的是当用户点击它时在新 Activity 中以全屏模式显示图像。

像这样:

enter image description here

我尝试过的:

@Override
public PaletteViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.
from(parent.getContext()).
inflate(R.layout.card_view, parent, false);

return new PaletteViewHolder(itemView);

}
@Override
public void onBindViewHolder(PaletteViewHolder holder,final int position) {
Palette palette = palettes.get(position);
holder.text1.setText(palette.getName());
holder.text2.setText(palette.getHexValue());
final Context context = holder.img.getContext();
Picasso.with(context)
.load(DaciaData.DaciaModele[position])
.placeholder(R.drawable.placeholder)
.error(R.drawable.error)
.resizeDimen(R.dimen.list_detail_image_size, R.dimen.list_detail_image_size)
.centerInside()
.tag(context)
.into(holder.img);

holder.img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bundle bundle = new Bundle();
//add data to your bundle
bundle.putInt("id", position);
//create intent
Intent mainIntent = new Intent(context, Extra1.class);
//add bundle to intent
mainIntent.putExtras(bundle);
//start activity
context.startActivity(mainIntent);
}
});
}

编辑

RecyclerItemClickListener:

public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;

public interface OnItemClickListener {
void onItemClick(View view, int position);
}

GestureDetector mGestureDetector;

public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
}

@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

}

@Override public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildPosition(childView));
return true;
}
return false;
}

@Override public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { }
}

MainRecyclerView 这是我定义 RecyclerView 以及 onItemTouchListener 的地方:

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerModels);
LinearLayoutManager linearLM = new LinearLayoutManager(getActivity());
linearLM.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLM);

recyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Bundle bundle = new Bundle();
bundle.putInt("id", position);

Intent i = new Intent(getActivity(), MainViewPager.class);
i.putExtras(bundle);
startActivity(i);
}
})
);

删除 recyclerView.addOnItemTouchListener(); 将解决图像长按的问题,但我将无法启动 ViewPager,我想有问题RecyclerItemClickListener 错误。

最佳答案

对不起,我迟到了,我最终解决了这个问题。我所要做的就是删除 recyclerView.addOnItemTouchListener(...);,然后我在适配器中实现了 View.OnClickListener,然后我设置了 TextViewImageView 的 OnClickListener 方法,如下所示:

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

protected TextView textView;
protected ImageView imageView;

public ContactViewHolder(View v) {
super(v);
textView = (TextView) v.findViewById(R.id.t1);
imageview = (ImageView) v.findViewById(R.id.img);

textView.setOnClickListener(this);
imageview.setOnClickListener(this);
}

@Override
public void onClick(View v) {

if (v.getId() == textView.getId()) {
Toast.makeText(context, "TextView Clicked!", Toast.LENGTH_LONG).show();
} else if (v.getId() == imageView.getId()) {
Toast.makeText(context, "ImageView Clicked!", Toast.LENGTH_LONG).show();
}
}

关于Android:处理放置在 RecyclerView 中的 ImageView 上的点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31658479/

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