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

时间:2016-09-18 21:12:51

标签: java android android-recyclerview onclicklistener

我正在尝试检测何时触发RecyclerView的某个按钮(btn_removeApp)。我希望当单击RecyclerView中的任何行中的按钮时,也会调用rvOnClick_btn_AdapterAppsalreadySelected(View view, int position)下面的函数,但这不会发生。

以下是我对代码的尝试:

用于检测活动类中的onClick的MY接口

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 个答案:

答案 0 :(得分:1)

尝试添加此行

if (context instanceof RV_AppsalreadySelected_BtnClickListener) 
            btn_clickListener = (RV_AppsalreadySelected_BtnClickListener) context;