viewholder没有收到点击 - android

时间:2016-12-11 19:09:05

标签: android android-recyclerview android-arrayadapter android-viewholder

我有一个recyclerview和一个我在片段中的onCreate方法构建的适配器。

现在,我处理点击的方式是我必须接口, RecyclerViewOnClickListener RecyclerViewOnLongClickListener

当我创建适配器时,我向它提供了这些接口的两个实现。然后,当调用onBindViewHolder时,我只是将这些接口绑定到视图持有者的 itemView

问题是当我点击来自recyclerview的项目时,不会触发点击事件。我似乎无法理解为什么会这样。

适配器代码:

package com.silverfix.dgdeditor.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.silverfix.dgdeditor.R;
import com.silverfix.dgdeditor.fragments.ThirdPhaseFragment;
import com.silverfix.dgdeditor.utils.RecyclerViewClickListener;
import com.silverfix.dgdeditor.utils.RecyclerViewLongClickListener;

import java.util.List;

/**
 * Created by David on 09/12/2016.
 */

public class DangerousGoodsAdapter extends RecyclerView.Adapter<DangerousGoodsAdapter.DGViewHolder> {

    private Context context;
    private int clickedItemPos;
    private List<ThirdPhaseFragment.DGObject> dataSet;

    private RecyclerViewClickListener onClickListener;
    private RecyclerViewLongClickListener onLongClickListener;

    public DangerousGoodsAdapter(Context context, List<ThirdPhaseFragment.DGObject> dataSet) {
        this.context = context;
        this.dataSet = dataSet;

        clickedItemPos = -1;
    }

    public void setClickedItemPosition(int clickedItemPos) {
        this.clickedItemPos = clickedItemPos;
    }

    public int getClickedItemPosition() {
        return clickedItemPos;
    }

    public void setClickListener(RecyclerViewClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public void setOnLongClickListener(RecyclerViewLongClickListener onLongClickListener) {
        this.onLongClickListener = onLongClickListener;
    }

    @Override
    public DGViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.dg_list_item, parent, false);

        DGViewHolder viewHolder = new DGViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(DGViewHolder holder, int position) {
        ThirdPhaseFragment.DGObject object = dataSet.get(position);

        // Set the correct data to the text views
        holder.unTv.setText(object.un);
        holder.descTv.setText(object.description);

        // Binding click events to the view holder
        holder.bindClick(position, onClickListener);
        holder.bindLongClick(position, onLongClickListener);
    }

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

    public static class DGViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener{

        private TextView unTv, descTv;
        public DGViewHolder(View rootView) {
            super(rootView);
            rootView.setOnCreateContextMenuListener(this);

            unTv = (TextView) rootView.findViewById(R.id.unTv);
            descTv = (TextView) rootView.findViewById(R.id.descTv);
        }

        public void bindClick(final int position, final RecyclerViewClickListener onClickListener) {
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onClickListener.onClick(position);
                }
            });

        }

        public void bindLongClick(final int position, final RecyclerViewLongClickListener onLongClickListener) {
            itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    onLongClickListener.onLongClick(position);
                    return true;
                }
            });
        }

        public void detachListeners() {
            itemView.setOnClickListener(null);
            itemView.setOnLongClickListener(null);
        }

        @Override
        public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {
            contextMenu.add(Menu.NONE, R.id.action_edit, Menu.NONE, R.string.dg_list_context_menu_modify);
            contextMenu.add(Menu.NONE, R.id.action_remove, Menu.NONE, R.string.dg_list_context_menu_remove);
        }
    }

    @Override
    public void onViewRecycled(DGViewHolder holder) {
        super.onViewRecycled(holder);
        holder.detachListeners();
    }
}

片段代码:

adapter = new DangerousGoodsAdapter(getActivity(), objects);

        adapter.setClickListener(new RecyclerViewClickListener() {
            @Override
            public void onClick(int position) {
                DGObject object = objects.get(position);

                adapter.setClickedItemPosition(position);
                showDialog(createBundle(object), DG_EDIT);
            }
        });

        adapter.setOnLongClickListener(new RecyclerViewLongClickListener() {
            @Override
            public void onLongClick(int position) {
                adapter.setClickedItemPosition(position);
            }
        });

        dgList.setAdapter(adapter);

onClick和onLongClick中的代码无关紧要,因为根本没有调用它。

0 个答案:

没有答案