java – 如何在回收站视图中使用OnClickListener?

前端之家收集整理的这篇文章主要介绍了java – 如何在回收站视图中使用OnClickListener?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > RecyclerView onClick34
我基本上试图做的是使回收者视图中显示的对象可以点击某个TextView id,因为我正在制作一个在列表中显示相册封面及其标题的程序.我需要能够点击回收商视图所产生的每个框,并且在其点击的同时,将TextView与其他信息(作者,发布日期,热门歌曲等)弹出,然后再回到按钮(如果可能)返回到专辑列表.我一直在等待几个小时,不能弄清楚如何使OnclickListener工作.如果你知道如何或有任何建议,请高兴听到他们.谢谢!
package com.albumlist.albumlist;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private AlbumData[] itemsData;

    public MyAdapter(AlbumData[] itemsData){
        this.itemsData = itemsData;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private TextView txtViewTitle;
        private ImageView imgViewIcon;

        public ViewHolder(View itemLayoutView) {
            super(itemLayoutView);
            itemLayoutView.setOnClickListener(this);
            txtViewTitle = (TextView) itemLayoutView.findViewById(R.id.album_title);
            imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.album_icon);
        }

        @Override
        public void onClick(View v) {

        }
    }

    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {

        View itemLayoutView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.data_layout,null);

        ViewHolder viewHolder = new ViewHolder(itemLayoutView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder,int position) {

        viewHolder.txtViewTitle.setText(itemsData[position].getTitle());
        viewHolder.imgViewIcon.setImageResource(itemsData[position].getImageUrl());
    }

    @Override
    public int getItemCount() {
        return itemsData.length;
    }

}

解决方法

这个概念在Xaver Kapeller的评论中很好地总结了出来.如果您正在寻找一种简单的方法来管理RecyclerView交互,就像ListView的传统交互一样,请查看GitHub上的 BigNerdRanch’s recyclerview-multiselect库.他们有一个可以探索的示例应用程序,它实现了一系列具有多选功能的OnClickListener和OnLongClickListeners.

以下是BigNerdRanch如何通过适配器和ViewHolder在片段中实现侦听器的代码段,在这种情况下实际上是库自己的SwappingHolder的扩展.

public CrimeHolder(View itemView) {
        super(itemView,mMultiSelector);

        mTitleTextView = (TextView) itemView.findViewById(R.id.crime_list_item_titleTextView);
        mDateTextView = (TextView) itemView.findViewById(R.id.crime_list_item_dateTextView);
        mSolvedCheckBox = (CheckBox) itemView.findViewById(R.id.crime_list_item_solvedCheckBox);
        itemView.setOnClickListener(this);
        itemView.setLongClickable(true);
        itemView.setOnLongClickListener(this);
    }

    public void bindCrime(Crime crime) {
        mCrime = crime;
        mTitleTextView.setText(crime.getTitle());
        mDateTextView.setText(crime.getDate().toString());
        mSolvedCheckBox.setChecked(crime.isSolved());
    }

    @Override
    public void onClick(View v) {

        if (mCrime == null) {
            return;
        }
        if (!mMultiSelector.tapSelection(this)) {
            selectCrime(mCrime);
        }

    }


    @Override
    public boolean onLongClick(View v) {

        ((AppCompatActivity) getActivity()).startSupportActionMode(mDeleteMode);
        mMultiSelector.setSelected(this,true);
        return true;
    }


}


private class CrimeAdapter extends RecyclerView.Adapter<CrimeHolder> {
    @Override
    public CrimeHolder onCreateViewHolder(ViewGroup parent,int pos) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_item_crime,parent,false);
        return new CrimeHolder(view);
    }

    @Override
    public void onBindViewHolder(CrimeHolder holder,int pos) {
        Crime crime = mCrimes.get(pos);
        holder.bindCrime(crime);
        Log.d(TAG,"binding crime" + crime + "at position" + pos);
    }

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

猜你在找的Java相关文章