1、定义LinearLayout布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
2、自定义ListView控件类
public class MyListView extends LinearLayout {
private ListView listView;
private List<MyListViewModel> modelList;
private MyListViewAdapter myListViewAdapter;
public MyListView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.listview_layout, this);
myListViewAdapter = new MyListViewAdapter(context, R.layout.listview_item);
listView = findViewById(R.id.listview);
listView.setAdapter(myListViewAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
MyListViewModel model = modelList.get(i);
model.getOnClickListener().onClick(view);
}
});
}
public void setModelList(List<MyListViewModel> modelList) {
this.modelList = modelList;
myListViewAdapter.setModelList(modelList);
}
public List<MyListViewModel> getModelList() {
return modelList;
}
public void notifyDataSetChanged() {
myListViewAdapter.notifyDataSetChanged();
}
}
3、定义ListView的Adapter
public class MyListViewAdapter extends ArrayAdapter<MyListViewModel> {
private List<MyListViewModel> modelList;
public MyListViewAdapter(@NonNull Context context, int resource) {
super(context, resource);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View itemView = convertView;
if (itemView == null) {
itemView = LayoutInflater.from(getContext()).inflate(R.layout.listview_item, parent, false);
ImageView imageView = itemView.findViewById(R.id.item_img);
TextView title = itemView.findViewById(R.id.item_title);
ViewHolder viewHolder = new ViewHolder(imageView, title);
itemView.setTag(viewHolder);
}
ViewHolder viewHolder = (ViewHolder) itemView.getTag();
MyListViewModel model = modelList.get(position);
viewHolder.imageView.setImageResource(model.getImageResource());
viewHolder.title.setText(model.getTitle());
return itemView;
}
@Override
public void addAll(@NonNull Collection<? extends MyListViewModel> collection) {
super.clear();
super.addAll(collection);
}
public void setModelList(List<MyListViewModel> modelList) {
this.modelList = modelList;
addAll(modelList);
}
private class ViewHolder {
private ImageView imageView;
private TextView title;
public ViewHolder(ImageView imageView, TextView title) {
this.imageView = imageView;
this.title = title;
}
}
}
4、定义ListView中每个item的数据模型
public class MyListViewModel {
private int imageResource;
private String title;
private View.OnClickListener onClickListener;
public MyListViewModel(int imageResource, String title, View.OnClickListener onClickListener) {
this.imageResource = imageResource;
this.title = title;
this.onClickListener = onClickListener;
}
public int getImageResource() {
return imageResource;
}
public void setImageResource(int imageResource) {
this.imageResource = imageResource;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public View.OnClickListener getOnClickListener() {
return onClickListener;
}
public void setOnClickListener(View.OnClickListener onClickListener) {
this.onClickListener = onClickListener;
}
}
5、定义ListView中每个item的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/item_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/item_title"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="@string/title"/>
</LinearLayout>
6、在Activity中的布局中引入自定义ListView
<?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="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".three.ListViewActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="ListView Test"/>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000"/>
<Button
android:id="@+id/listviewshow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="show"/>
<com.example.firstlinecode.three.MyListView
android:id="@+id/mylistview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
7、在Activity类中初始化ListView中的数据,并调用自定义ListView的notifyDataSetChanged方法刷新ListView,这里为每个item中增加点击事件,跳转到其他Activity,并修改当前item的title值
package com.example.firstlinecode.three;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.example.firstlinecode.R;
import java.util.ArrayList;
import java.util.List;
public class ListViewActivity extends AppCompatActivity {
MyListView myListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
myListView = findViewById(R.id.mylistview);
Button button = findViewById(R.id.listviewshow);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
List<MyListViewModel> list = initData();
myListView.setModelList(list);
myListView.notifyDataSetChanged();
}
});
}
private List<MyListViewModel> initData() {
List<MyListViewModel> list = new ArrayList<>();
for (int i = 0;i<20;i++) {
int finalI = i;
MyListViewModel model = new MyListViewModel(R.drawable.ic_launcher_background, "title" + i, new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent;
if ( finalI % 2 == 0) {
intent = new Intent(ListViewActivity.this, NextTwoActivity.class);
} else {
intent = new Intent(ListViewActivity.this, NextOneActivity.class);
}
// startActivity(intent);
startActivityForResult(intent, finalI);
}
});
list.add(model);
}
return list;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
List<MyListViewModel> modelList = myListView.getModelList();
if (requestCode>=0 && requestCode < modelList.size()) {
MyListViewModel model = modelList.get(requestCode);
if (data == null) {
return;
}
String title = data.getStringExtra("title");
model.setTitle(title);
myListView.notifyDataSetChanged();
}
}
}