我刚从ListView
转到RecyclerView
我遇到了一个问题:我无法让ImageView's
onClickListener正常工作。基本上我想要的是在用户点击它时在新活动中以全屏模式显示图像。
这样的事情:
我尝试了什么:
@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
出现了问题。
答案 0 :(得分:2)
对不起我迟到的回答,我最终解决了这个问题。我所要做的就是删除recyclerView.addOnItemTouchListener(...);
,然后在我的适配器中实现View.OnClickListener
,然后为OnClickListener
和TextView
设置ImageView
方法,像这样:
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();
}
}
答案 1 :(得分:0)
现在,当我点击图片时,就像我点击该行一样, 它启动了错误的活动。 ImageView的onClickListener不是 工作
使用setTag
和getTag
holder.img.setTag(position);
holder.img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Integer pos = (Integer)view.getTag();
// returns an object hence the casting
Bundle bundle = new Bundle();
//add data to your bundle
Log.i("Position is",""+pos.intValue);
bundle.putInt("id", pos.intValue());
// get int value
//create intent
Intent mainIntent = new Intent(context, Extra1.class);
//add bundle to intent
mainIntent.putExtras(bundle);
//start activity
context.startActivity(mainIntent);
}
});
编辑:
正如pskink建议你只需在ViewHolder中使用点击监听器即可消除setTag和getTag的需要。这将确保您单击正确的行项目并转到下一个活动。
要获得职位,您可以在RecyclerView视图持有者中使用getLayoutPosition()
编辑2:
代码:
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "Element at " + getLayoutPosition () + " clicked.");
Bundle bundle = new Bundle();
//add data to your bundle
bundle.putInt("id", getLayoutPosition());
//create intent
Intent mainIntent = new Intent(context, Extra1.class);
//add bundle to intent
mainIntent.putExtras(bundle);
//start activity
context.startActivity(mainIntent);
}
});
编辑3:
您的ViewHolder类可以实现接口View.OnClickListener
然后只需覆盖onClick
并将OnClickListener
设置为您的观看次数。
答案 2 :(得分:0)
与您有完全相同的问题。需要的RecyclerView
如果点击行内的视图,则会调用onClick from the view,以及行上的onItemClick。
解决方案是,为RecyclerView onItemClick返回false,而是将onClick侦听器设置为ViewHolder主行布局(与在行中设置视图侦听器相同,将其设置为行的主要布局)。 / p>
为您的RecyclerView
返回onItemClick的false@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
在onBindViewHolder
中获取主行布局,并在那里设置onClickListener
holder.mainRowLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Handle row item click
}
});
如果您需要任何进一步的帮助,请联系我们。
答案 3 :(得分:0)
尝试以下代码来帮助您:
#!/bin/bash
>combined.csv
for xml in *.xml
do
echo "Processing $xml";
id=$({ xmllint --xpath "string(//customer/id)" $xml; echo ","; } | tr "\n" " ");
firstname=$({ xmllint --xpath "string(//customer/firstName)" $xml; echo ","; } | tr "\n" " ");
phonenumber=$(xmllint --xpath "string(//customer/phoneNumbers)" $xml);
line="${id}${firstname}${phonenumber}\n"
printf "$line" >> combined.csv
done
在下一个活动中,您可以获得将全屏显示的图像。
Intent i = new Intent(this, ShowActivity.class);
i.putExtra("filepath", filepath.get(position));
startActivity(i);
答案 4 :(得分:-1)
使用此代码:
holder.img.setId(position);
holder.img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bundle bundle = new Bundle();
//add data to your bundle
bundle.putInt("id", view.getId());
//create intent
Intent mainIntent = new Intent(context, Extra1.class);
//add bundle to intent
mainIntent.putExtras(bundle);
//start activity
context.startActivity(mainIntent);
}
});