在recyclerView中实现ItemClickListener

时间:2017-01-03 19:46:05

标签: android

我已经尝试了2天,在recyclerView中实现了ItemClickListener,它不起作用。请帮忙

如何在此.class中实现ItemClickListener?

public class Platos_Adapter extends
    RecyclerView.Adapter<Platos_Adapter.ViewHolder> {

            private ImageLoader imageLoader;
            private Context context;

            List<Estadisticas> estadisticas;

            public Platos_Adapter(List<Estadisticas> comida,Context context){
                super();
                //Getting all the comida
                this.estadisticas = estadisticas;
                this.context = context;
            }

            @Override
            public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.platos_row,
    parent, false);
                ViewHolder viewHolder = new ViewHolder(v);
                return viewHolder;
            }

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

                holder.setOnClickListener((View.OnClickListener) this);

                Estadisticas superHero =  estadisticas.get(position);

                imageLoader = DecoracionLineaDivisoria.CustomVolleyRequest.getInstance(context).getImageLoader();

                imageLoader.get(superHero.getEscudo_Local(), ImageLoader.getImageListener(holder.escudo_local,
    R.mipmap.ic_launcher, android.R.drawable.ic_dialog_alert));

                holder.escudo_local.setImageUrl(superHero.getEscudo_Local(),
    imageLoader);
                holder.textViewEquipo_Local.setText(superHero.getEquipo_Local());
            }

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

            class ViewHolder extends RecyclerView.ViewHolder{
                public NetworkImageView escudo_local;
                public TextView textViewEquipo_Local;

                public ViewHolder(View itemView) {
                    super(itemView);

                    escudo_local = (NetworkImageView) itemView.findViewById(R.id.tv_esc_local);
                    textViewEquipo_Local= (TextView) itemView.findViewById(R.id.tv_ek_local);
        ;        }


                public void setOnItemClickListener(ViewHolder.OnItemClickListener
    onClickListener) {

                    public void onClick(View view) {

                        Toast.makeText(Platos_Adapter.this, "clicked" ,Toast.LENGTH_SHORT).show();

                        Estadisticas comida = estadisticas.get(getAdapterPosition());
                    }
                }
            }
        }

3 个答案:

答案 0 :(得分:2)

在RecyclerView中,没有像onItemClickListener那样的直接支持,但是,你必须在你的适配器类上这样做,这可能对你有帮助。

 public class ViewHolder extends RecyclerView.ViewHolder{
    View view;

    public ViewHolder(View itemView) {
       super(itemView);
       view=itemView;
 }
}

你必须在onBindViewHolder上触发onclick方法,

 holder.view.setOnClickListener(new View.OnClickListener() {
        @Override
      public void onClick(View view) {
         Toast.makeText(Platos_Adapter.this, "clicked" ,Toast.LENGTH_SHORT).show();

         Estadisticas comida = estadisticas.get(getAdapterPosition());
  }
 });

我认为,这是我们可以点击recyclerview上的视图的方式。

答案 1 :(得分:0)

您可以使用此代码

public class Platos_Adapter extends RecyclerView.Adapter {
private ImageLoader imageLoader;
private Context context;
List<Estadisticas> estadisticas;

public Platos_Adapter(List<Estadisticas> comida,Context context){
    super();
    //Getting all the comida
    this.estadisticas = estadisticas;
    this.context = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.platos_row, parent, false);
    ViewHolder viewHolder = new ViewHolder(v);
    return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    holder.root.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(context, "clicked" ,Toast.LENGTH_SHORT).show();
            Estadisticas comida = estadisticas.get(getAdapterPosition());
        }
    });
    Estadisticas superHero =  estadisticas.get(position);
    imageLoader = DecoracionLineaDivisoria.CustomVolleyRequest.getInstance(context).getImageLoader();
    imageLoader.get(superHero.getEscudo_Local(), ImageLoader.getImageListener(holder.escudo_local, R.mipmap.ic_launcher, android.R.drawable.ic_dialog_alert));
    holder.escudo_local.setImageUrl(superHero.getEscudo_Local(), imageLoader);
    holder.textViewEquipo_Local.setText(superHero.getEquipo_Local());
}

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

class ViewHolder extends RecyclerView.ViewHolder{
    public NetworkImageView escudo_local;
    public TextView textViewEquipo_Local;
    public View root;

    public ViewHolder(View itemView) {
        super(itemView);
        root=itemView;
        escudo_local = (NetworkImageView) itemView.findViewById(R.id.tv_esc_local);
        textViewEquipo_Local= (TextView) itemView.findViewById(R.id.tv_ek_local);
    }
}

答案 2 :(得分:0)

我要感谢@Dharma Kshetri和@Rahul Pareta的回复。谢谢 这是解决方案:

public class Platos_Adapter extends RecyclerView.Adapter<Platos_Adapter.ViewHolder> {
    private ImageLoader imageLoader;
    private Context context;
    List<Estadisticas> estadisticas;

    public Platos_Adapter(List<Estadisticas> estadisticas, Context context) {
        super();
        this.estadisticas = estadisticas;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from( parent.getContext() ).inflate( R.layout.platos_row, parent, false );
        ViewHolder viewHolder = new ViewHolder( v );
        return viewHolder;
    }

    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.root.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Toast.makeText(context, estadisticas.get(position).getEquipo_Local(), Toast.LENGTH_SHORT).show();

                Estadisticas estadisticas1 = estadisticas.get( getAdapterPosition() );
            }

            private int getAdapterPosition() {
                return 0;
            }
        } );
        Estadisticas superHero = estadisticas.get( position );
        imageLoader = DecoracionLineaDivisoria.CustomVolleyRequest.getInstance( context ).getImageLoader();
        imageLoader.get( superHero.getEscudo_Local(), ImageLoader.getImageListener( holder.escudo_local, R.mipmap.ic_launcher, android.R.drawable.ic_dialog_alert ) );
        holder.escudo_local.setImageUrl( superHero.getEscudo_Local(), imageLoader );
        holder.textViewEquipo_Local.setText( superHero.getEquipo_Local() );
    }

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

    class ViewHolder extends RecyclerView.ViewHolder {
        public NetworkImageView escudo_local;
        public TextView textViewEquipo_Local;
        public View root;

        public ViewHolder(View itemView) {
            super( itemView );
            root = itemView;
            escudo_local = (NetworkImageView) itemView.findViewById( R.id.tv_esc_local );
            textViewEquipo_Local = (TextView) itemView.findViewById( R.id.tv_ek_local );
            textViewCategoria = (TextView) itemView.findViewById( R.id.tv_ek_visi );
        }
    }
}

和activity_detail.xml

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/detail_backdrop_height"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginStart="48dp"
        app:expandedTitleMarginEnd="64dp">

        <ImageView
            android:id="@+id/backdrop"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:fitsSystemWindows="true"
            app:layout_collapseMode="parallax" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_collapseMode="pin" />

    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingTop="24dp">

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/card_margin">

            <LinearLayout
                style="@style/Widget.CardContent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/tv_info_title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="yyyyyyyyyyyyyyyyyy"
                    android:textAppearance="@style/TextAppearance.AppCompat.Title" />

                <TextView
                    android:id="@+id/tv_info"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="ddddddddddddddd" />

            </LinearLayout>

        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/card_margin"
            android:layout_marginLeft="@dimen/card_margin"
            android:layout_marginRight="@dimen/card_margin">

            <LinearLayout
                style="@style/Widget.CardContent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/tv_categoria_title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="mmmmmmmmmmmmmmmmmmm"
                    android:textAppearance="@style/TextAppearance.AppCompat.Title" />

                <TextView
                    android:id="@+id/tv_categoria"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="aaaaaaaaaaaaaaaaaaaaaa" />

            </LinearLayout>

        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/card_margin"
            android:layout_marginLeft="@dimen/card_margin"
            android:layout_marginRight="@dimen/card_margin">

            <LinearLayout
                style="@style/Widget.CardContent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="xxxxxxxxxxxxxxxxx"
                    android:textAppearance="@style/TextAppearance.AppCompat.Title" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="ddddddddddd" />

            </LinearLayout>

        </android.support.v7.widget.CardView>

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

和PlatosAdapter.java

public class Platos_Adapter extends RecyclerView.Adapter<Platos_Adapter.ViewHolder> {

    private ImageLoader imageLoader;
    private Context context;

    List<Estadisticas> estadisticas;

    public Platos_Adapter(List<Estadisticas> estadisticas, Context context) {
        super();
        this.estadisticas = estadisticas;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from( parent.getContext() ).inflate( R.layout.platos_row, parent, false );
        ViewHolder viewHolder = new ViewHolder( v );
        return viewHolder;
    }

    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.root.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //Toast.makeText(context, "this is my Toast message!!! =)",
                        //Toast.LENGTH_LONG).show();

                //Toast.makeText(context, estadisticas.get(position).getEquipo_Local(), Toast.LENGTH_SHORT).show();

                Intent intent= new Intent(context, DetailActivity.class);

                intent.putExtra("Nombre", estadisticas.get(position).getEquipo_Local());
                intent.putExtra("Categoria", estadisticas.get(position).getEquipo_Visitante());
                intent.putExtra("Imagen", estadisticas.get(position).getEscudo_Local());

                context.startActivity(intent);


                Estadisticas estadisticas1 = estadisticas.get( getAdapterPosition() );
            }

            private int getAdapterPosition() {
                return 0;
            }
        } );
        Estadisticas superHero = estadisticas.get( position );
        imageLoader = DecoracionLineaDivisoria.CustomVolleyRequest.getInstance( context ).getImageLoader();
        imageLoader.get( superHero.getEscudo_Local(), ImageLoader.getImageListener( holder.escudo_local, R.mipmap.ic_launcher, android.R.drawable.ic_dialog_alert ) );
        holder.escudo_local.setImageUrl( superHero.getEscudo_Local(), imageLoader );
        holder.textViewEquipo_Local.setText( superHero.getEquipo_Local() );
        //holder.textViewCategoria.setText( superHero.getEquipo_Visitante() );
    }

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

    class ViewHolder extends RecyclerView.ViewHolder {
        public NetworkImageView escudo_local;
        public TextView textViewEquipo_Local;
        //public TextView textViewCategoria;
        public View root;

        public ViewHolder(View itemView) {
            super( itemView );
            root = itemView;
            escudo_local = (NetworkImageView) itemView.findViewById( R.id.tv_esc_local );
            textViewEquipo_Local = (TextView) itemView.findViewById( R.id.tv_ek_local );
            //textViewCategoria = (TextView) itemView.findViewById( R.id.tv_ek_visi );
        }
    }
}