多条目适配器--pizifusheng



public class MyAdapter extends BaseAdapter {
    private List<Result.DataBean> list;
    private Context context;
    private final DisplayImageOptions options;
    private final int IMAGE_LEFT = 0;
    private final int IMAGE_RIGHT = 1;

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

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

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

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

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

    @Override
    public int getItemViewType(int position) {
         if (position%2==0){
             return IMAGE_RIGHT;
         }else {
             return IMAGE_LEFT;
         }
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        int itemViewType = getItemViewType(i);
        if (itemViewType==0) {
            ViewHolder holder;
            if (view == null) {
                view = View.inflate(context, R.layout.items, null);
                holder = new ViewHolder();
                holder.title = view.findViewById(R.id.titles);
                holder.time = view.findViewById(R.id.times);
                holder.img = view.findViewById(R.id.img);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }
            holder.time.setText(list.get(i).getSHOWTIME());
            holder.title.setText(list.get(i).getTITLE());
            ImageLoader.getInstance().displayImage(list.get(i).getIMAGEURL(), holder.img, ImageloaderUtil.getImageOptions());
            return view;
          }


else {
            ViewHolder holder;
            if (view==null){
                view = View.inflate(context, R.layout.item,null);
                holder = new ViewHolder();
                holder.title = view.findViewById(R.id.titles);
                holder.time = view.findViewById(R.id.times);
                holder.img =view.findViewById(R.id.img);
                view.setTag(holder);
            }else {
                holder = (ViewHolder) view.getTag();
            }
            holder.time.setText(list.get(i).getSHOWTIME());
            holder.title.setText(list.get(i).getTITLE());

//ImageLoader工具类对图片做处理
            ImageLoader.getInstance().displayImage(list.get(i).getIMAGEURL(),holder.img,ImageloaderUtil.getImageOptions());
            return view;
        }
        }





    class ViewHolder{
        private ImageView img;
        private TextView title;
        private TextView time;
    }

}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页