gpt4 book ai didi

java - 当单击 RecyclerView 中的一行中的某个按钮时,如何从 Activity 类中检测?

转载 作者:行者123 更新时间:2023-11-30 10:38:48 25 4
gpt4 key购买 nike

我正在尝试检测 RecyclerView 一行中的某个按钮 (btn_removeApp) 何时被触发。我期待函数,也在下面,rvOnClick_btn_AdapterAppsalreadySelected(View view, int position) 在 RecyclerView 中任何行中的按钮被点击时被调用,但这并没有发生。

下面是我对代码的尝试:

我的界面检测来自 Activity 类的 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 - 当单击 RecyclerView 中的一行中的某个按钮时,如何从 Activity 类中检测?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39562733/

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