好长时间没有记录了,平时都记在Notion里面了。这个Adapter还是自己记下吧。
SimpleAdapter,MainActivity里面的代码,SimpleAdapter需要对应好参数。
其中第四个参数是对应Map里的String对象,然后第五个参数是对应的单个的布局文件的id。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<Map<String,Object>> listItem = new ArrayList<>();
for (int i = 0; i < name.length; i++) {
HashMap<String, Object> objectObjectHashMap = new HashMap<>();
objectObjectHashMap.put("person",name[i]);
objectObjectHashMap.put("desc",descs[i]);
listItem.add(objectObjectHashMap);
}
SimpleAdapter simpleAdapter = new SimpleAdapter(this,listItem, R.layout.simple_item, new String[]{"person", "desc"},new int[]{R.id.name,R.id.desc}) ;
ListView listView = findViewById(R.id.list);
listView.setAdapter(simpleAdapter);
}
单个的组件应该怎么放的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<ImageView
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
主布局文件很简单只有一个ListView
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#f00"
android:headerDividersEnabled="false"/>
</androidx.constraintlayout.widget.ConstraintLayout>
RecyclerView
RecyclerView很容易出错,我一开始的时候显示不出来发现好多人都有这个问题。集中在3点。
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
public PersonHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View inflate = LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null);
return new PersonHolder(inflate,this);
}
@Override
public int getItemCount() {
return personList.size();
}
Adapter像是连接viewholder和recycler的桥梁。
需要构建的xml文件是在Adapter里的onCreat时加载的。然后返回自己创建的Viewholder
RecyclerView.Adapter<PersonHolder> personHolderAdapter = new RecyclerView.Adapter<PersonHolder>() {
@NonNull
@Override
public PersonHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View inflate = LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null);
return new PersonHolder(inflate,this);
}
@Override
public void onBindViewHolder(@NonNull PersonHolder holder, int position) {
holder.nameTv.setText(personList.get(position).name);
holder.descTv.setText(personList.get(position).desc);
holder.headerIv.setImageResource(personList.get(position).header);
}
@Override
public int getItemCount() {
return personList.size();
}
};
viewHolder集中在对应关系上
class PersonHolder extends RecyclerView.ViewHolder{
TextView nameTv;
TextView descTv;
ImageView headerIv;
private RecyclerView.Adapter adapter;
public PersonHolder(@NonNull View itemView, RecyclerView.Adapter adapter) {
super(itemView);
this.headerIv = itemView.findViewById(R.id.header);
this.nameTv = itemView.findViewById(R.id.name);
this.descTv = itemView.findViewById(R.id.desc);
this.adapter = adapter;
}
接下来就可以在MainActivity里构造了
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recycler);
recyclerView.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
//省略Adapter
recyclerView.setAdapter(personHolderAdapter);