我正在尝试检测何时触发RecyclerView行中的某个按钮(btn_removeApp
)。我希望单击RecyclerView中任意行中的按钮时也会调用rvOnClick_btn_AdapterAppsalreadySelected(View view, int position)
函数,但不会发生。
下面是我对代码的尝试:
我的界面,用于从活动类中检测onClick
public interface RV_AppsalreadySelected_BtnClickListener {
public void rvOnClick_btn_AdapterAppsalreadySelected(View view, int position);
}
我的Adapter_AppsalreadySelected.class:
public class Adapter_AppsalreadySelected
extends RecyclerView.Adapter<Adapter_AppsalreadySelected.MyViewHolder> {
private List<String> selAppsList;
private MyAppLogger mLog = new MyAppLogger();
private Context context;
private RV_AppsalreadySelected_BtnClickListener btn_clickListener;
public class MyViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
public TextView tv_selappname;
public ImageView iv_selappicon;
public Button btn_removeApp;
private String mItem;
public MyViewHolder(View view) {
super(view);
tv_selappname = (TextView) view.findViewById(R.id.tv_selappname);
btn_removeApp = (Button) view.findViewById(R.id.btn_removeApp);
btn_removeApp = (Button) view.findViewById(R.id.btn_removeApp);
btn_removeApp.setOnClickListener(this);
}
public void setItem(String item) {
mItem = item;
tv_selappname.setText(item);
}
@Override
public void onClick(View v) {
if(btn_clickListener != null) {
btn_clickListener.rvOnClick_btn_AdapterAppsalreadySelected(v, getAdapterPosition());
}
}
}
public Adapter_AppsalreadySelected(Context context,
List<String> selAppsList) {
this.context = context;
this.selAppsList = selAppsList;
}
@Override
public Adapter_AppsalreadySelected.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.row_appsalreadyselected, parent, false);
return new Adapter_AppsalreadySelected.MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(Adapter_AppsalreadySelected.MyViewHolder holder, int position) {
String app = selAppsList.get(position);
holder.tv_selappname.setText(app);
}
@Override
public int getItemCount() {
return selAppsList.size();
}
}
row_appsalreadyselected.xml
<ImageView
android:id="@+id/iv_selappicon"
android:layout_width="0dp"
android:layout_weight="0.2"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/>
<TextView
android:id="@+id/tv_selappname"
android:layout_width="0dp"
android:layout_weight="0.6"
android:layout_height="wrap_content"
android:text="Apps Name"
android:layout_gravity="center_vertical"
/>
<Button
android:id="@+id/btn_removeApp"
android:layout_width="0dp"
android:layout_weight="0.2"
android:layout_height="wrap_content"
android:clickable="false"
/>
</LinearLayout>
MyActivity类:这是我要获取onClick侦听器的位置:
public class SetupBlocksActivity extends AppCompatActivity
implements
RV_AppsalreadySelected_BtnClickListener {
@Override
public void rvOnClick_btn_AdapterAppsalreadySelected(View view, int position) {
//THIS FUNCTION NEVER GETS TRIGGERED EVEN WHEN I CLICK ON btn_removeApp on ANY OF THE ROWS IN THE RECYCLERVIEW
}
}
最佳答案
尝试添加此行
if (context instanceof RV_AppsalreadySelected_BtnClickListener)
btn_clickListener = (RV_AppsalreadySelected_BtnClickListener) context;
关于java - 单击RecylerView中某行中的某个按钮时,如何从Activity类中进行检测?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39562733/