web-dev-qa-db-ja.com

RecyclerViewOnClickが機能しない

フラグメント内に水平リサイクラービューを作成しました。これで、アイテムをクリックしても、クリックリスナーが機能していません。 Adapterクラスのコードは次のとおりです。

public class FeaturedProductsAdapter  extends RecyclerView.Adapter<FeaturedProductsAdapter.CustomViewHolder> {
private List<FeaturedProductInfo> feedItemList;
private Context mContext;

public FeaturedProductsAdapter(Context context, List<FeaturedProductInfo> feedItemList) {
    this.feedItemList = feedItemList;
    this.mContext = context;
}
public class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    protected ImageView imageView;
    protected TextView textView,priceView;
    private Context context;


    public CustomViewHolder(View view,Context context) {

        super(view);
        this.context=context;
        this.imageView = (ImageView) view.findViewById(R.id.thumbnail);
        this.textView = (TextView) view.findViewById(R.id.prodTitle);
        this.priceView = (TextView) view.findViewById(R.id.prodPrice);
        view.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {

        int position = getLayoutPosition(); // gets item position
        Log.e("Check", position + "");
        FeaturedProductInfo user = feedItemList.get(position);//[position];
        // We can access the data within the views
        Intent intent = new Intent(context, ProductDescription.class);
        intent.putExtra("id", user.getId());
        mContext.startActivity(intent);


    }

}

@Override
public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(mContext).inflate(R.layout.featured_product_list_item_card, null);
    Context context = viewGroup.getContext();

    CustomViewHolder viewHolder = new CustomViewHolder(view,context);

    return viewHolder;
}

@Override
public void onBindViewHolder(CustomViewHolder customViewHolder, int i) {
    FeaturedProductInfo feedItem = feedItemList.get(i);

    //Download image using picasso library
    if(!feedItem.getUrl().contains("."))
    {
        feedItem.setUrl("nothing");
    }
    Picasso.with(mContext).load(feedItem.getUrl())
            .error(R.drawable.unavailable)
            .placeholder(R.drawable.unavailable)
            .resize(110,110)
            .into(customViewHolder.imageView);

    //Setting text view title
    customViewHolder.textView.setText(feedItem.getTitle());
    customViewHolder.priceView.setText(feedItem.getPrice());
    //Log.e("Featured: ","SET");
}

@Override
public int getItemCount() {
    return (null != feedItemList ? feedItemList.size() : 0);
}

}

ビューホルダーの使い方がよくわからないと思います。別のアクティビティでrecyclerViewに同じコードを使用しましたが、それは魅力のように機能します。

7
Akshay Bhasin

1. ViewHolder内のシンプルなクリックハンドラー

RecyclerViewには、メソッドsetOnItemClickListener()を持つListViewとは異なり、クリックハンドラーをアイテムにアタッチするための特別な規定はありません。同様の効果を実現するために、アダプタ内のViewHolder内にクリックイベントをアタッチできます。

public class ContactsAdapter extends RecyclerView.Adapter<ContactsAdapter.ViewHolder> {
    // ...

    // Used to cache the views within the item layout for fast access
    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public TextView tvName;
        public TextView tvHometown;
        private Context context;

        public ViewHolder(Context context, View itemView) {
            super(itemView);
            this.tvName = (TextView) itemView.findViewById(R.id.tvName);
            this.tvHometown = (TextView) itemView.findViewById(R.id.tvHometown);
            // Store the context
            this.context = context;
            // Attach a click listener to the entire row view
            itemView.setOnClickListener(this);
        }

        // Handles the row being being clicked
        @Override
        public void onClick(View view) {
            int position = getLayoutPosition(); // gets item position
            User user = users.get(position);
            // We can access the data within the views
            Toast.makeText(context, tvName.getText(), Toast.LENGTH_SHORT).show();
        }
    }

    // ...
}

別の方法が私の好みの方法です..しかし、これはそれを回避するための良い方法でもあります。

私のonBindViewHolder

@Override
    public void onBindViewHolder(CategoryViewHolder holder, int position) {
        Category category = mCategories.get(position);

        holder.tvTitle.setText(category.getTitle());
        holder.tvDescription.setText(category.getDescription());

        holder.rlContainer.setOnClickListener(mClickListener);
        holder.rlContainer.setTag(holder);
    }

私のクラスレベル(View.OnClickListnerのアダプタオブジェクト)

View.OnClickListener mClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            CategoryViewHolder holder = (CategoryViewHolder) view.getTag();
            int position = holder.getAdapterPosition();

            startAppointmentBookingFor(mCategories.get(position));
        }
    };

したがって、基本的にリスナーをホルダー内の任意のビューにアタッチし(私はそれをコンテナーにのみ配置しようとします)、それをonclickで抽出し、位置などを処理します。

14
yUdoDis

アダプタに次の変更を加えます。

public class FeaturedProductsAdapter  extends RecyclerView.Adapter<FeaturedProductsAdapter.CustomViewHolder> {
private List<FeaturedProductInfo> feedItemList;
private Context mContext;
private OnItemClickListener onItemClickListener;

public FeaturedProductsAdapter(Context context, List<FeaturedProductInfo,OnItemClickListener onItemClickListener> feedItemList) {
    this.feedItemList = feedItemList;
    this.mContext = context;
    this.onItemClickListener = onItemClickListener;

}
public class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    protected ImageView imageView;
    protected TextView textView,priceView;
    private Context context;


    public CustomViewHolder(View view,Context context) {

        super(view);
        this.context=context;
        this.imageView = (ImageView) view.findViewById(R.id.thumbnail);
        this.textView = (TextView) view.findViewById(R.id.prodTitle);
        this.priceView = (TextView) view.findViewById(R.id.prodPrice);
        view.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {

        onItemClickListener.onItemClick(getLayoutPosition());
        Log.e("Check", position + "");
        FeaturedProductInfo user = feedItemList.get(position);//[position];
        // We can access the data within the views
        Intent intent = new Intent(context, ProductDescription.class);
        intent.putExtra("id", user.getId());
        mContext.startActivity(intent);


    }

}

public interface OnItemClickListener{
    void onItemClick(int position);
}

@Override
public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(mContext).inflate(R.layout.featured_product_list_item_card, null);
    Context context = viewGroup.getContext();

    CustomViewHolder viewHolder = new CustomViewHolder(view,context);

    return viewHolder;
}

@Override
public void onBindViewHolder(CustomViewHolder customViewHolder, int i) {
    FeaturedProductInfo feedItem = feedItemList.get(i);

    //Download image using picasso library
    if(!feedItem.getUrl().contains("."))
    {
        feedItem.setUrl("nothing");
    }
    Picasso.with(mContext).load(feedItem.getUrl())
            .error(R.drawable.unavailable)
            .placeholder(R.drawable.unavailable)
            .resize(110,110)
            .into(customViewHolder.imageView);

    //Setting text view title
    customViewHolder.textView.setText(feedItem.getTitle());
    customViewHolder.priceView.setText(feedItem.getPrice());
    //Log.e("Featured: ","SET");
}

@Override
public int getItemCount() {
    return (null != feedItemList ? feedItemList.size() : 0);
}

「super(view)」の下の「CustomViewHolder」にview.setOnClickListener(this)を追加します

完了しました。動作するはずです。