recycleview click listener,subView click,view.getId()= -1

时间:2016-04-12 02:47:14

标签: android android-recyclerview android-view onclicklistener

我使用RecycleView来显示数据,每个ListItem都有3个按钮:stat_btncomment_btnforward_btn。我想知道用户点击哪个按钮来执行不同的功能,因此在onClick方法中,我尝试使用view.getId()来显示单击了哪个按钮,但view.getId()始终返回 - 1;

04-12 10:32:44.273 29382-29382/com.smartspace.magicmirror I/QUESTIONNAIREADAPTER: ---->>view.getid:-1
04-12 10:32:44.273 29382-29382/com.smartspace.magicmirror I/QUESTIONNAIREFRAGMENT: ---->>onItemClick: -1

这是我的代码: 适配器

public class QuestionnaireAdapter extends RecyclerView.Adapter<QuestionnaireAdapter.QuestionnaireViewHolder> implements OnClickListener{

    private static final String TAG = "QUESTIONNAIREADAPTER";

    public Context context;
    public Uri picTestUrl = Uri.parse("https://raw.githubusercontent.com/facebook/fresco/gh-pages/static/fresco-logo.png");
    public Uri picTestUrl2 = Uri.parse("http://magicwang.oss-cn-qingdao.aliyuncs.com/test.jpg");


    private OnRecycleViewItemClickListener mOnItemClickListener = null;

    public  interface OnRecycleViewItemClickListener {
        void onItemClick(View view , String data);
    }

    public void setOnItemClickListener(OnRecycleViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public QuestionnaireAdapter(Context context){
        this.context = context;
    }

    @Override
    public QuestionnaireViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_questionnaire_item, parent, false);

        QuestionnaireViewHolder questionnaireViewHolder =
                new QuestionnaireViewHolder(view);

        view.setOnClickListener(this);

        return questionnaireViewHolder;
    }

    @Override
    public void onBindViewHolder(final QuestionnaireViewHolder holder, int position) {
        //holder.tv.setText("22");
        holder.dressImageView.setImageURI(picTestUrl2);
        holder.itemView.setTag("tag");

    }



    @Override
    public int getItemCount() {
        return 10;
    }

    @Override
    public void onClick(View v) {
        Log.i(TAG,"---->>view.getid:" + v.getId());
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick(v,(String)v.getTag());
        }
    }

ViewHolder

    class QuestionnaireViewHolder extends RecyclerView.ViewHolder {

        ImageView userIcon;
        TextView userName;
        TextView questionnaireTime;
        SimpleDraweeView dressImageView;

        //底部收藏、评论和转发按钮
        ImageView starBtn;
        ImageView commmentBtn;
        ImageView forwardBtn;


        public QuestionnaireViewHolder(View itemView) {
            super(itemView);
            userIcon = (ImageView) itemView.findViewById(R.id.user_icon);
            userName = (TextView) itemView.findViewById(R.id.user_name);
            questionnaireTime = (TextView) itemView.findViewById(R.id.questionnaire_time);
            dressImageView = (SimpleDraweeView) itemView.findViewById(R.id.dress_imageview);

            starBtn = (ImageView) itemView.findViewById(R.id.star_btn);
            commmentBtn = (ImageView) itemView.findViewById(R.id.comment_btn);
            forwardBtn = (ImageView) itemView.findViewById(R.id.forward_btn);

        }


    }
}

和使用适配器和recyclerView的片段

public class QuestionnaireFragment extends Fragment {

    public static final String TAG = "QUESTIONNAIREFRAGMENT";

    // TODO: Customize parameter argument names
    private static final String ARG_COLUMN_COUNT = "column-count";
    // TODO: Customize parameters
    private int mColumnCount = 1;
   // private OnListFragmentInteractionListener mListener;

    private RecyclerView qusetionnaireRecyclerView;
    private QuestionnaireAdapter mQuestionnaireAdapter;


    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public QuestionnaireFragment() {
    }

    // TODO: Customize parameter initialization
    @SuppressWarnings("unused")
    public static QuestionnaireFragment newInstance(int columnCount) {
        QuestionnaireFragment fragment = new QuestionnaireFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_COLUMN_COUNT, columnCount);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        if (getArguments() != null) {
            mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
        }

       mQuestionnaireAdapter = new QuestionnaireAdapter(AppApplication.getContext());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_questionnaireitem_list, container, false);

        qusetionnaireRecyclerView = (RecyclerView) view.findViewById(R.id.questionnaire_list);
        qusetionnaireRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        qusetionnaireRecyclerView.setAdapter(mQuestionnaireAdapter);
        mQuestionnaireAdapter.setOnItemClickListener(new QuestionnaireAdapter.OnRecycleViewItemClickListener() {
            @Override
            public void onItemClick(View view, String data) {
                Log.i(TAG,"---->>onItemClick: " + view.getId());
                switch (view.getId()) {
                    case R.id.star_btn:
                        //do something
                        break;

                    case R.id.comment_btn:
                        //do something
                        break;

                    case R.id.forward_btn:
                        //do something
                        break;
                }
            }
        });


        return view;
    }



    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

    }

    @Override
    public void onDetach() {
        super.onDetach();
            //mListener = null;
        }
    }

2 个答案:

答案 0 :(得分:0)

我就是这样做的。

在onCreate中声明按钮及其ID:

nchar(d * 1000)
# [1] 13 13 13 13 12 13 13 13 13 12 13 13 13 13 12 13 13 13 13 11 13 13 13 13 12 13 13 13 13 12 13

然后在onClick方法中定义一个用于侦听按钮ID

的开关
stat = (Button)findViewById(R.id.stat_btn);
cmnt = (Button)findViewById(R.id.comment_btn);
fwd = (Button)findViewById(R.id.forward_btn):

答案 1 :(得分:0)

将OnClickListeners添加到onBindViewHolder()

中的按钮
 @Override
    public void onBindViewHolder(final QuestionnaireViewHolder holder, int position) {
        //holder.tv.setText("22");
        holder.dressImageView.setImageURI(picTestUrl2);
        holder.itemView.setTag("tag");

        holder.starBtn.setOnClickListener(new OnClickListener()....);
        holder.commmentBtn.setOnClickListener(new OnClickListener()....);
        holder.forwardBtn.setOnClickListener(new OnClickListener()....);

    }

如果您只想听取儿童观点而不是整个项目,则无需实施OnItemClickListener