1. 程式人生 > >頻道管理-----adapter

頻道管理-----adapter

MyChanAdapter

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.pindao.R;
import com.example.pindao.bean.Chan;
import java.util.List;

public class MyChanAdapter extends BaseAdapter {
    private List<Chan> list;
    private Context context;
    private boolean isEditable;

    //藉口
    public interface OnItemDeleteClickListener{
        void onDeleteClick(int position);
    }
    private OnItemDeleteClickListener listener;

    public void setOnItemDeleteClickListener(OnItemDeleteClickListener listener){
        this.listener = listener;
    }

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

    public void setEditable(boolean editable) {
        isEditable = editable;
        notifyDataSetChanged();
    }

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

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null){
            convertView = View.inflate(context, R.layout.item_my_chan,null);
            holder = new ViewHolder();
            holder.textName = convertView.findViewById(R.id.text_name);
            holder.imgDelete = convertView.findViewById(R.id.img_delete_chan);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.textName.setText(list.get(position).getName());
        if (isEditable){
            holder.imgDelete.setVisibility(View.VISIBLE);
        }else {
            holder.imgDelete.setVisibility(View.GONE);
        }
        holder.imgDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onDeleteClick(position);
            }
        });
        return convertView;
    }
    class ViewHolder{
        TextView textName;
        ImageView imgDelete;
    }
}

NewsAdapter

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.pindao.R;
import com.example.pindao.bean.News;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.List;

public class NewsAdapter extends BaseAdapter {
    public static final int TYPE_ONE = 0;
    public static final int TYPE_TWO = 1;
    public static final int TYPE_THREE = 2;

    private List<News.DataBean> list;
    private Context context;

    public NewsAdapter(List<News.DataBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

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

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public int getItemViewType(int position) {
        News.DataBean bean = list.get(position);
        if (!TextUtils.isEmpty(bean.getThumbnails03())){
            return TYPE_THREE;
        }else if (!TextUtils.isEmpty(bean.getThumbnail02())){
            return TYPE_TWO;
        }else {
            return TYPE_ONE;
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder1 holder1 = null;
        ViewHolder2 holder2 = null;
        ViewHolder3 holder3 = null;

        int type = getItemViewType(position);
        switch (type){
            case TYPE_ONE:
                if (convertView == null){
                    convertView = View.inflate(context , R.layout.item_one_news , null);
                    holder1 = new ViewHolder1();
                    holder1.imgLogo = convertView.findViewById(R.id.img_logo);
                    holder1.textTitle = convertView.findViewById(R.id.text_title);
                    convertView.setTag(holder1);
                }else {
                    holder1 = (ViewHolder1) convertView.getTag();
                }
                holder1.textTitle.setText(list.get(position).getTitle());
                ImageLoader.getInstance().displayImage(list.get(position).getThumbnail01(),holder1.imgLogo);
                break;
            case TYPE_TWO:
                if (convertView == null){
                    convertView = View.inflate(context , R.layout.item_two_news , null);
                    holder2 = new ViewHolder2();
                    holder2.imgLogo1 = convertView.findViewById(R.id.img_logo1);
                    holder2.imgLogo2 = convertView.findViewById(R.id.img_logo2);
                    holder2.textTitle = convertView.findViewById(R.id.text_title);
                    convertView.setTag(holder2);
                }else {
                    holder2 = (ViewHolder2) convertView.getTag();
                }
                holder2.textTitle.setText(list.get(position).getTitle());
                ImageLoader.getInstance().displayImage(list.get(position).getThumbnail01(),holder2.imgLogo1);
                ImageLoader.getInstance().displayImage(list.get(position).getThumbnail02(),holder2.imgLogo2);
                break;
            case TYPE_THREE:
                if (convertView == null){
                    convertView = View.inflate(context , R.layout.item_three_news , null);
                    holder3 = new ViewHolder3();
                    holder3.imgLogo1 = convertView.findViewById(R.id.img_logo1);
                    holder3.imgLogo2 = convertView.findViewById(R.id.img_logo2);
                    holder3.imgLogo3 = convertView.findViewById(R.id.img_logo3);
                    holder3.textTitle = convertView.findViewById(R.id.text_title);
                    convertView.setTag(holder3);
                }else {
                    holder3 = (ViewHolder3) convertView.getTag();
                }
                holder3.textTitle.setText(list.get(position).getTitle());
                ImageLoader.getInstance().displayImage(list.get(position).getThumbnail01(),holder3.imgLogo1);
                ImageLoader.getInstance().displayImage(list.get(position).getThumbnail02(),holder3.imgLogo2);
                ImageLoader.getInstance().displayImage(list.get(position).getThumbnails03(),holder3.imgLogo3);
                break;
        }
        return convertView;
    }

    class ViewHolder1{
        ImageView imgLogo;
        TextView textTitle;
    }
    class ViewHolder2{
        ImageView imgLogo1;
        ImageView imgLogo2;
        TextView textTitle;
    }
    class ViewHolder3{
        ImageView imgLogo1;
        ImageView imgLogo2;
        ImageView imgLogo3;
        TextView textTitle;
    }
}

RecommondAdapter

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.pindao.R;
import com.example.pindao.bean.Chan;
import java.util.List;

public class RecomChanAdapter extends BaseAdapter {
    private List<Chan> list;
    private Context context;

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

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

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null){
            convertView = View.inflate(context, R.layout.item_recommond_chan,null);
            holder = new ViewHolder();
            holder.textName = convertView.findViewById(R.id.text_name);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.textName.setText(list.get(position).getName());

        return convertView;
    }

    class ViewHolder{
        TextView textName;
    }
}