ListView 及适配器的应用
ListView的应用【包含Adapter的应用】
-
父容器和子view
【main_activity.xml】
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:fitsSystemWindows="true">
<ListView
android:id="@+id/lv_hobby"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>【hobby_item.xml】
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/hobby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
</LinearLayout> -
item封装-->Hobby.java
package com.neostra.test;
public class Hobby {
private String hobby;
public Hobby(String hobby) {
this.hobby = hobby;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
-
子view(hobby_item)适配器--->HobbyAdapter.java
package com.neostra.test;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import java.util.ArrayList;
public class HobbyAdapter extends BaseAdapter {
private ArrayList<Hobby> dataList;//适配器数据源
private Context mContext;
public HobbyAdapter(ArrayList<Hobby> dataList, Context mContext) {
this.dataList = dataList;
this.mContext = mContext;
}
//item长度