CertificationCourseRecAdapter.java 2.5 KB
package com.hh.xuetubao.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hh.xuetubao.R;

import java.util.List;

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

    private List<String> list;
    private Context context;
    private int clickPosition = 0;

    public CertificationCourseRecAdapter(List<String> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.rv_certificationcourse, null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        if (list.get(position) != null) {
            holder.title.setText(list.get(position));
        }


        if (position == clickPosition) {
            holder.title.setTextColor(context.getResources().getColor(R.color.color_ff5400));
            holder.title.setBackgroundColor(context.getResources().getColor(R.color.color_ffffff));
        } else {
            holder.title.setTextColor(context.getResources().getColor(R.color.color_333333));
            holder.title.setBackgroundColor(context.getResources().getColor(R.color.color_e4e4e4));
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickItem != null) {
                    clickItem.ClickItem(position);
                }
            }
        });


    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView title;

        public ViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.title);
        }
    }

    private ClickItem clickItem;

    public void setClickItem(ClickItem clickItem) {
        this.clickItem = clickItem;
    }

    public interface ClickItem {
        void ClickItem(int position);
    }

    public void setClickPosition(int clickPosition) {
        this.clickPosition = clickPosition;
    }
}