在RecyclerView中的OnClick侦听器

时间:2018-04-16 19:02:19

标签: android android-recyclerview

无法使我的OnClick侦听器工作。我在另一个片段中创建了一个onClick侦听器,但该方法在此片段中不起作用。如果你查看我的代码,我之前使用onclick监听器的方式是:

holder.alertItemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

但它不适用于下面的代码。对于一些rease,alertItemView是红色的,我不知道为什么。我做的与我的其他代码完全相同,但这次它无法正常工作。关于为什么或者你有更好的方法的任何想法我可以设置一个onclick监听器来从每个位置获取信息?

public class AlertAdapter extends RecyclerView.Adapter<AlertAdapter.AlertViewHolder>  {
private List<AlertReference> AlertItem;
public Context context;

public AlertAdapter(List<AlertReference> AlertItem, Context context) {
    this.AlertItem = AlertItem;
    this.context = context;
}

@NonNull
@Override
public AlertViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.alert_list_item, parent, false);

    return new AlertViewHolder(v);

}

@Override
public void onBindViewHolder(@NonNull final AlertViewHolder holder, final int position) {
    final AlertReference alertItemHolder = AlertItem.get(position);

    holder.text_AlertCoin.setText(alertItemHolder.getCoin_asset());
    holder.text_AlertAmount.setText(alertItemHolder.getCoin_amount());

}

@Override
public int getItemCount() {
    return AlertItem.size();
}

public class AlertViewHolder extends RecyclerView.ViewHolder {
    public TextView text_AlertCoin;
    public ImageView image_AlertCoin;

    public AlertViewHolder(View alertItemView) {
        super(alertItemView);
        text_AlertCoin = (TextView) 
alertItemView.findViewById(R.id.TV_CoinAsset);            
alertItemView.findViewById(R.id.image_CoinAlert);
    }
}
} 

2 个答案:

答案 0 :(得分:0)

view和setOnClickListner中创建AlertViewHolderClass的引用,如下所示:

public class AlertAdapter extends RecyclerView.Adapter<AlertAdapter.AlertViewHolder>  {
    private List<AlertReference> AlertItem;
    public Context context;

    public AlertAdapter(List<AlertReference> AlertItem, Context context) {
        this.AlertItem = AlertItem;
        this.context = context;
    }

    @NonNull
    @Override
    public AlertViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.alert_list_item, parent, false);

        return new AlertViewHolder(v);

    }

    @Override
    public void onBindViewHolder(@NonNull final AlertViewHolder holder, final int position) {
        final AlertReference alertItemHolder = AlertItem.get(position);

        holder.text_AlertCoin.setText(alertItemHolder.getCoin_asset());
        holder.text_AlertAmount.setText(alertItemHolder.getCoin_amount());
        holder.alertItemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });

    }

    @Override
    public int getItemCount() {
        return AlertItem.size();
    }

    public class AlertViewHolder extends RecyclerView.ViewHolder {
        public TextView text_AlertCoin;
        public ImageView image_AlertCoin;
        public View alertItemView;

        public AlertViewHolder(View alertItemView) {
            super(alertItemView);
            this.alertItemView = alertItemView;
            text_AlertCoin = (TextView)
                    alertItemView.findViewById(R.id.TV_CoinAsset);
            alertItemView.findViewById(R.id.image_CoinAlert);

        }
    }
}

希望这有帮助

答案 1 :(得分:0)

使用一点DataBinding和一个自定义监听器非常简单。

以下是您的新AlertAdapter

public class AlertAdapter extends RecyclerView.Adapter<AlertAdapter.AlertViewHolder>  {

    public Context context;
    public AlertListener alertListener;

    private ArrayList<AlertReference> alertItems;

    public AlertAdapter(Context context, ArrayList<AlertReference> alertItems, AlertListener alertListener) {
        this.alertItems = alertItems;
        this.context = context;
    }

    @NonNull
    @Override
    public AlertViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new AlertViewHolder(DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.alert_list_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull final AlertViewHolder holder, final int position) {
        final AlertReference alertItem = alertItems.get(position);

        holder.binding.setAlertItem(alertItem);
        holder.binding.setHandler(alertListener);
    }

    @Override
    public int getItemCount() {
        return AlertItem.size();
    }

    public class AlertViewHolder extends RecyclerView.ViewHolder {

        public AlertBinding binding;

        public AlertViewHolder(View alertItemView) {
            super(alertItemView);

            binding = DataBindingUtil.bind(alertItemView);
        }
    }

    public interface AlertListener {
        public void onAlertTapped(AlertReference reference);
    }
}

我不知道您的布局文件是什么样的,但它需要是这样的:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data class="AlertBinding">

        <variable
            name="alertItem"
            type="package.for.your.AlertReference" />

        <variable
            name="handler"
            type="package.for.your.AlertListener" />

    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:onClick="@{ (view) -> handler.onAlertItemClicked(alertItem) }">

        <TextView
            android:id="@+id/text_alert_coin"
            android:text="@{ alertItem.getCoin_amount() }"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/text_alert_amount"
            android:text="@{ alertItem.getCoin_asset() }"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>
</layout>

顶部的<layout>标记在布局上启用DataBinding,在适配器的绑定布局上设置变量会自动设置布局中的值。这样,您可以直接从您已实例化AlertAdapter的Activity或Fragment将侦听器传递到布局。像这样:

AlertAdapter alertAdapter = new AlertAdapter(context, yourAlertItemsList, new AlertAdapter.AlertListener() {
    @Override
    public void onAlertItemClicked(AlertReference alertReference){
        // Your alertReference code goes here
    }
});

此外,请确保通过将其添加到模块级build.gradle文件来启用DataBinding:

android {
    ...

    dataBinding {
        enabled true
    }
}

如果您有任何疑问,请与我们联系!

编辑: 有关DataBinding的更多信息 - https://developer.android.com/topic/libraries/data-binding/index.html