在我的RecycleView

时间:2018-02-24 16:37:09

标签: android recycler-adapter

我正在尝试在我的RecycleView适配器中设置一个 OnItemClickListener 或类似内容,以覆盖我的项目名称。

我尝试了很多东西,但我从不使用适配器,对我来说解决所有冲突试图使用互联网例子有点困难,

有人可以帮我实现吗? 提前致谢

在这里我的适配器

package com.example.anonymous.mh4;

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

import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Anonymous on 22/02/2018.
 */

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.CustomViewHolder> {

    private List<Monster> feedItemList;
    private Context mContext;

    public MyRecyclerViewAdapter(Context context, List<Monster> feedItemList) {
        this.feedItemList = feedItemList;
        this.mContext = context;
    }




    @Override
    public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i)  {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.listviewitemmonster, null);
        CustomViewHolder viewHolder = new CustomViewHolder(view);

        return viewHolder;

    }

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

        //Render image using Picasso library
            Picasso.with(mContext).load(feedItem.getIcon())
                    .error(R.drawable.unknown)
                    .placeholder(R.drawable.unknown)
                    .into(customViewHolder.imageView);

        //Setting text view title
        customViewHolder.textView.setText(Html.fromHtml(feedItem.getName()));

    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);

    }

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

    class CustomViewHolder extends RecyclerView.ViewHolder {
        protected ImageView imageView;
        protected TextView textView;


        public CustomViewHolder(View view) {
            super(view);
            this.imageView = (ImageView) view.findViewById(R.id.imglayout);
            this.textView = (TextView) view.findViewById(R.id.namelayout);
            view.setOnClickListener((View.OnClickListener) this);
        }
    }


}

1 个答案:

答案 0 :(得分:0)

快速回答:

public CustomViewHolder(View view) {
    super(view);
    this.imageView = (ImageView) view.findViewById(R.id.imglayout);
    this.textView = (TextView) view.findViewById(R.id.namelayout);
    customViewHolder.textView.setOnClickListener(new OnClickListener(){
        public void onClick(View view){
            // Make your toast here, and for the text, use ((TextView) view).getText().toString()
        }
    })
}

但我建议您查看bind() ViewHolderonBindViewHolder调用的方法 <f:FormElement id="label17" label="Parte Objeto"> <f:fields> <Select enabled="true" id="objeto_grupo" items="{path: '/CatalogosComboSet', filters : [ { path : 'IKatalogart', operator : 'EQ', value1 : 'B'}, { path : 'IStep', operator : 'EQ', value1 : '1'} ] }" name="" placeholder="Tecnico ejecutor" selectedKey="{Codegruppe}" change="detalleSelect"> <items> <core:ListItem id="__item_objeto_1" key="{Codegruppe}" text="{Kurztext}"/> </items> </Select> <Select enabled="true" id="objeto_detalle" name="" placeholder="Objeto" selectedKey="0"> <items> </items> </Select> </f:fields> </f:FormElement>