android如何检测从 Activity 类时某 button 在 RecylerView 中的某一行中单击?

标签: Java Android
发布时间: 2016/10/10 21:45:40
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我想当某检测 button ( btn_removeApp ) 的 RecyclerView 的行中获取触发。我期待的功能,下文, rvOnClick_btn_AdapterAppsalreadySelected(View view, int position) 时获取调用 button 在任何行中单击 RecyclerView,但不是这样。

下面是我尝试的代码︰

我的接口来检测从 onClick Activity

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 listener︰

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

}

}

解决方法 1:

尝试添加这条线

if (context instanceof RV_AppsalreadySelected_BtnClickListener) 
            btn_clickListener = (RV_AppsalreadySelected_BtnClickListener) context;
赞助商