科技美学

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Adapter就是适配器,而设计模式里也有Adapter Pattern。

而BaseAdapter就是设计模式里的思维,把一些不相关的东西放进去,经过适配器,最终都会出产同一样的东西。

就像BaseAdapter,就算放不同的XML和Data进去,最终出现都是ListView的Cell。

 

1. BaseAdaper代码,完全没有特别的地方,不解述了。

package com.paradise.electronic.eparadise2.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.paradise.electronic.eparadise2.R;
import com.paradise.electronic.eparadise2.dto.PostDTO;

import java.util.List;

/**
 * Created by Administrator on 17-11-12.
 */
public class PostAdapter extends BaseAdapter {

    // data
    private Context context;
    private LayoutInflater inflter;
    private List<PostDTO> postDTOs;
    private List<Bitmap> bitMaps;

    // UI
    private TextView tvPostTitle;
    private TextView tvPostDate;
    private ImageView imgAbstractImg;

    public PostAdapter(Context context, List<PostDTO> postDTOs, List<Bitmap> bitMaps) {
        super();
        this.postDTOs = postDTOs;
        this.bitMaps = bitMaps;
        this.context = context;
        inflter = (LayoutInflater.from(context));
    }

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

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        // 初始化convertView
        convertView = inflter.inflate(R.layout.lv_home_post, null);

        // 初始化UI
        tvPostTitle = (TextView) convertView.findViewById(R.id.tvPostTitle);
        tvPostDate = (TextView) convertView.findViewById(R.id.tvPostDate);
        imgAbstractImg = (ImageView) convertView.findViewById(R.id.imgAbstractImg);

        // Set date into tv
        tvPostTitle.setText(postDTOs.get(position).getTitle());
        tvPostDate.setText(postDTOs.get(position).getDate());
        imgAbstractImg.setImageBitmap(bitMaps.get(position));

        return convertView;
    }

}

2. 初始化BaseAdaper,然后让ListView调用BaseAdaper就可以。

    private String strJsPost;
    private void initData() {
        // set view unclickable first
        rlFragmentHome.setClickable(false);

        // Network thread
        new Thread(new Runnable() {
            @Override
            public void run() {
                // get <JSON>post from network
                strJsPost = okhttp.getResponse(Configuration.getHotPost);
                postHotDTOs = gsonTools.getPost(strJsPost);

                // get post image URL from network, than convect the file to bitmap
                bitHotMaps = new ArrayList<Bitmap>();
                for (int i = 0; i < postHotDTOs.size(); i++ ) {
                    Bitmap temp = bitMap.returnBitMap(postHotDTOs.get(i).getAbstractImg());
                    bitHotMaps.add(temp);
                }

                // UI thread
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // init adapter
                        postAdapter = new PostAdapter(getActivity(), postHotDTOs, bitHotMaps);
                        lvHomePostItem.setAdapter(postAdapter);

                        // hide loading textview
                        tvHomeLoading.setText("");
                        tvHomeLoading.getLayoutParams().height = 0;

                        // set view unclickable while finishing
                        rlFragmentHome.setClickable(true);
                    }
                });
            }
        }).start();
    }

 

posted on 2017-12-27 21:21  chankuang  阅读(1938)  评论(0编辑  收藏  举报