RecruitmentAdapter.java 2.6 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 RecruitmentAdapter extends RecyclerView.Adapter<RecruitmentAdapter.ViewHolder> {
    private List<String> list;
    private Context context;

    public RecruitmentAdapter(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_recruitment, null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

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


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getListener != null) {
                    getListener.onClick(position);
                    notifyDataSetChanged();
                }
            }
        });
        if (position == clickPosition) {
            holder.itemView.setBackground(context.getResources().getDrawable(R.drawable.stoke_1dp_orange));
            holder.content.setTextColor(context.getResources().getColor(R.color.color_ffffff));

        } else {
            holder.itemView.setBackground(context.getResources().getDrawable(R.drawable.stoke_1dp));
            holder.content.setTextColor(context.getResources().getColor(R.color.color_464646));
        }
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView content;

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

        }
    }

    public interface GetListener {
        void onClick(int position);
    }

    private GetListener getListener;

    public void setGetListener(GetListener getListener) {
        this.getListener = getListener;
    }

    private int clickPosition;

    public int getClickPosition() {
        return clickPosition;
    }

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