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;
}
@Override
public String toString() {
return "Hobby{" +
"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长度
@Override
public int getCount() {
return dataList.size();
}
//item项
@Override
public Object getItem(int i) {
return dataList.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
//获取item,放回匹配的视图
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
MyViewHolder myViewHolder=null;//什么内部类Holder对象
//判断xml视图是否已存在view当中
if (view == null){
view = View.inflate(mContext,R.layout.hobby_item,null);
TextView hobby=view.findViewById(R.id.hobby);
CheckBox checkedBox=view.findViewById(R.id.checkbox);
myViewHolder = new MyViewHolder(hobby,checkedBox);
view.setTag(myViewHolder);//将myViewHolder存储到view当中,从而不用getView时都新建子view中的控件
}else {
myViewHolder=(MyViewHolder) view.getTag();//若view不为空,则获取到子viewHolder类
}
myViewHolder.hobby.setText(dataList.get(i).getHobby());//动态设置子view显示效果
return view;
}
//视图holder类
static class MyViewHolder{
private TextView hobby;
private CheckBox checkBox;
public MyViewHolder(TextView hobby, CheckBox checkBox) {
this.hobby = hobby;
this.checkBox = checkBox;
}
}
}
-
MainActivity.java
package com.neostra.test;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.util.Log;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView lv_honbby;
private HobbyAdapter hobbyAdapter;
private ArrayList<Hobby> dataList;//适配数据准备
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dataList = new ArrayList<Hobby>();//数据源准备
dataList.add(new Hobby("math"));
dataList.add(new Hobby("English"));
dataList.add(new Hobby("chinese"));
dataList.add(new Hobby("chinese"));
lv_hobby = findViewById(R.id.lv_hobby);
hobbyAdapter = new HobbyAdapter(dataList, this);//适配器准备
lv_hobby.setAdapter(hobbyAdapter);//为listview视图设置适配器
}
}
标签:layout,dataList,适配器,应用,import,hobby,android,ListView,view 来源: https://www.cnblogs.com/a-n-yan/p/16220796.html