Java 片段中的Android-findviewbyd方法,用于从自定义布局中查找项
首先,我搜索了很多其他帖子,但仍然没有找到一个解决方案Java 片段中的Android-findviewbyd方法,用于从自定义布局中查找项,java,android,listview,android-fragments,findviewbyid,Java,Android,Listview,Android Fragments,Findviewbyid,首先,我搜索了很多其他帖子,但仍然没有找到一个解决方案 MainActivity是一个选项卡式活动(2个选项卡),第一个选项卡包含一个ListView。该列表中的单个项目来自一个自定义行布局文件,该文件包含一个id为deleteItemImage的ImageView 如何在Tab1\u类中使用findViewById从自定义布局文件获取对该图像的引用,以便能够setOnClickListener(…) 我已经尝试过使用(在onCreateView(…)method上)view.findViewB
MainActivity
是一个选项卡式活动(2个选项卡),第一个选项卡包含一个ListView
。该列表中的单个项目来自一个自定义行布局文件,该文件包含一个id为deleteItemImage的ImageView
如何在Tab1\u类中使用findViewById从自定义布局文件获取对该图像的引用,以便能够setOnClickListener(…)
我已经尝试过使用(在onCreateView(…)
method上)view.findViewById(…)
或getActivity().findViewById(…)
或getView().findViewById(…)
但没有任何效果
希望有人能帮忙!!提前谢谢你
编辑:
custom_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
(...)
<ImageView
android:id="@+id/deleteItemImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/icon"/>
</RelativeLayout>
您需要为适配器项声明侦听器接口。
并在Tab1_类中实现TouchListener
我希望下面的代码将有助于
修改适配器类和ViewHolder,如
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnTouchListener{
private ImageView iv_cardImage;
public MyViewHolder(View itemView) {
super(itemView);
iv_cardImage = (ImageView) itemView.findViewById(R.id.iv_cardImage);
iv_cardImage.setOnTouchListener(this);
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
view.getParent().requestDisallowInterceptTouchEvent(true);
switch(motionEvent.getAction()){
case MotionEvent.ACTION_UP:
//Toast.makeText(context, "from adapter", Toast.LENGTH_LONG).show();
touchListener.itemTouched(view, getAdapterPosition());
return true;
}
return false;
}
}
public void setTouchListener(TouchListener touchListener){
this.touchListener = touchListener;
}
创建一个在单击item imageview时调用的接口
public interface TouchListener{
void itemTouched(View v, int position);
}
请将xml和java文件发布到您试图查找视图的位置,代码在哪里?编辑:上面的代码。您可以膨胀tab1_list.xml
,但尝试查找custom_row.xml
中无法工作的元素。行在哪里充气?@Altoyyr我没有查看行视图=充气器。充气(R.layout.custom_行,容器,false)代码>并且它没有给出错误(谢谢!),但是当我单击它时,什么也没有发生,setOnClickListener()
是否存在。。。
public interface TouchListener{
void itemTouched(View v, int position);
}