1. 程式人生 > >安卓開發中使用ListView實現類似QQ聊天框(總結)

安卓開發中使用ListView實現類似QQ聊天框(總結)

效果如下:

(現在有點醜,至於每個item的背景,可以自己處理圖片)

思路:看見上圖的效果,既然是兩種不同的效果,肯定是要用資料介面卡的。

MutiLayoutAdapter.java的程式碼如下:

package com.deepreality.mutilayoutadapterdemo;

import android.content.Context;
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 java.util.ArrayList;

public class MutiLayoutAdapter extends BaseAdapter {

    private final static int TYPE_BOOK = 0;
    private final static int TYPE_APP = 1;
    private Context mContext;
    private ArrayList<Object> mData = null;

    public MutiLayoutAdapter() {
        super();
    }

    public MutiLayoutAdapter(Context context, ArrayList<Object> mData) {
        this.mContext = context;
        this.mData = mData;
    }

    public void Add(Object object) {
        if (mData == null) {
            mData = new ArrayList<>();
        }
        mData.add(object);
        notifyDataSetChanged();
    }

    public void Clear() {
        if (mData != null) {
            mData.clear();
        }
        notifyDataSetChanged();
    }

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

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

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

    @Override
    public int getItemViewType(int position) {
        if (mData.get(position) instanceof App) {
            return TYPE_APP;
        } else if (mData.get(position) instanceof Book) {
            return TYPE_BOOK;
        }
        return super.getItemViewType(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int type = getItemViewType(position);
        ViewHolder1 viewHolder1 = null;
        ViewHolder2 viewHolder2 = null;
        if (convertView == null) {
            switch (type) {
                case TYPE_BOOK:{
                    convertView = LayoutInflater.from(mContext).inflate(R.layout.item_two, parent, false);
                    viewHolder1 = new ViewHolder1(convertView);
                    convertView.setTag(viewHolder1);
                    break;
                }
                case TYPE_APP:{
                    convertView = LayoutInflater.from(mContext).inflate(R.layout.item_one, parent, false);
                    viewHolder2 = new ViewHolder2(convertView);
                    convertView.setTag(viewHolder2);
                    break;
                }
                default:break;
            }

        } else {
            switch (type) {
                case TYPE_BOOK:{
                    viewHolder1 = (ViewHolder1) convertView.getTag();
                    break;
                }
                case TYPE_APP:{
                    viewHolder2 = (ViewHolder2) convertView.getTag();
                    break;
                }
                default:break;
            }
        }

        Object object = mData.get(position);
        switch (type) {
            case TYPE_BOOK:{
                Book book = (Book) object;
                viewHolder1.txt_bname.setText(book.get_txt_bname());
                viewHolder1.txt_bauthor.setText(book.get_txt_bauthor());
                break;
            }
            case TYPE_APP:{
                App app = (App) object;
                viewHolder2.img_icon.setImageResource(app.get_img_icon());
                viewHolder2.txt_aname.setText(app.get_txt_aname());
                break;
            }
            default:break;
        }

        return convertView;
    }

    //兩個不同的ViewHolder
    private static class ViewHolder2{
        ImageView img_icon;
        TextView txt_aname;
        public ViewHolder2(View convertView) {
            img_icon = convertView.findViewById(R.id.img_icon);
            txt_aname = convertView.findViewById(R.id.txt_aname);
        }
    }

    private static class ViewHolder1{
        TextView txt_bname;
        TextView txt_bauthor;
        public ViewHolder1(View convertView) {
            txt_bname = convertView.findViewById(R.id.txt_bname);
            txt_bauthor = convertView.findViewById(R.id.txt_bauthor);
        }
    }
}

MainActivity.java中程式碼如下:

package com.deepreality.mutilayoutadapterdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ListView lvDatas;
    private Button btnAdd_A, btnAdd_B, btnClear;

    private ArrayList<Object> mData = null;
    private MutiLayoutAdapter myAdapter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        componentInit();
        componentAddOnClickListener();
        mData = new ArrayList<>();
        myAdapter = new MutiLayoutAdapter(MainActivity.this, mData);
        lvDatas.setAdapter(myAdapter);
    }

    private void componentInit() {
        btnAdd_A = findViewById(R.id.main_btnAdd_A);
        btnAdd_B = findViewById(R.id.main_btnAdd_B);
        btnClear = findViewById(R.id.main_btnClear);
        lvDatas = findViewById(R.id.main_lvDatas);
    }

    private void componentAddOnClickListener() {
        btnAdd_A.setOnClickListener(this);
        btnAdd_B.setOnClickListener(this);
        btnClear.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.main_btnAdd_A:{
                myAdapter.Add(new Book("AA", "這是A說的話"));
                break;
            }
            case R.id.main_btnAdd_B:{
                myAdapter.Add(new App(R.mipmap.ic_launcher, "這是B說的話"));
                break;
            }
            case R.id.main_btnClear:{
                myAdapter.Clear();
                break;
            }
            default:break;
        }
    }
}
說白了就是在介面卡中做了當前物件的判斷處理。