Android - 初心者 - ListView、ViewHolder、AarrayAdapter

Android - 初心者 - ListView、ViewHolder、AarrayAdapter

這次要來講,ListView,

常常看到列表,需要一直不斷的滑,

通常ListView會搭配ViewHolder,才能夠不必一直不斷的重複產生新的View,而造成效能變差,

再搭配客製化的ArrayAdapter也比較好撰寫與重複利用、好維護。

要使用這種方式撰寫ListView,

先準備一個Model,(就是類別、物件)

假設是Member好了。

public class Member {
    public String id = "";
    public String name = "";
}

接著就要繼承ArrayAdapter<T>撰寫MemberListAdapter,此時就會在裡面包ViewHolder


public class MemberListAdapter extends ArrayAdapter<Member> {
    private Context mContext;

    static class ViewHolder{
        TextView mId;
        TextView mName;
    }

    public MemberListAdapter(Context ctx) {
        super(ctx, R.layout.item_member);
        mContext = ctx;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater =  (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ViewHolder holder;

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_member, null);
            holder.mId = (TextView) convertView.findViewById(R.id.tv_id);
            holder.mName = (TextView) convertView.findViewById(R.id.tv_name);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Member item = this.getItem(position);

        holder.mId.setText(item.id);
        holder.mName.setText(item.name);
        return convertView;
    }
}

並且還要準備Item 所對映的View Layout,item_member

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="50dp">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:padding="5dp"
        android:layout_gravity="center_vertical|center_horizontal"
        android:gravity="center_vertical|center_horizontal">
        <TextView
            android:id="@+id/tv_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1"
            android:textSize="30sp"/>
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="100dp"
        android:padding="5dp"
        android:layout_gravity="center_vertical|left"
        android:gravity="center_vertical|left">
        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1"
            android:textSize="30sp"/>
    </LinearLayout>
</LinearLayout>

準備好資料、資料所對映的畫面,再來就是要設定到ListView裡。

所以Activity就會如下撰寫


public class MemberListActivity extends Activity  {
    private ListView mListView;
    private MemberListAdapter mAdapter ;

    @Override
    protected void onCreate(Bundle savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_member_list);

        this.findViews();
        this.setAdapter();
        this.loadData();
    }

    public void findViews() {
        mListView = (ListView) this.findViewById(R.id.list_view);
    }

    public void setAdapter(){
        mAdapter = new MemberListAdapter(this);
        mListView.setAdapter(mAdapter);
    }

    public void loadData(){
        MemberDao dao = MemberDao.getInstance(this);
        List<MemberItem> list = dao.getAll();
        mAdapter.addAll(list);
        mAdapter.notifyDataSetChanged();
    }
}

MemberDao則去參考我的SQLite 初心者(1)、(2)

https://dotblogs.com.tw/newmonkey48/2018/07/12/112453

https://dotblogs.com.tw/newmonkey48/2018/07/12/144938

這樣大概,就可以完成最簡單的列表頁呈現了。