1、使用步骤
2、demo案例
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".listView.ListViewActivity">
<ListView
android:id="@+id/list_view_lv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:id="@+id/list_item_tv"/>
</LinearLayout>
- 2-3 Bean类代码,用来存放item的数据(可根据需求添加属性)
package com.example.viewexample.listView;
public class Bean {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
- 2-4 自定义Adapter ,并继承BaseAdapter
package com.example.viewexample.listView;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.example.viewexample.R;
import java.util.List;
public class MyAdapter extends BaseAdapter {
private static final String TAG = "MyAdapter";
private List<Bean> data;
private Context mContext;
public MyAdapter(List<Bean> data,Context context) {
this.data = data;
this.mContext = context;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view ==null) {
view = LayoutInflater.from(mContext).inflate(R.layout.list_item, viewGroup, false);
}
TextView item = view.findViewById(R.id.list_item_tv);
item.setText(data.get(i).getName());
Log.d(TAG, "getView: "+i);
return view;
}
}
package com.example.viewexample.listView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.viewexample.R;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class ListViewActivity extends AppCompatActivity {
private static final String TAG = "ListViewActivity";
private List<Bean> data = new ArrayList<>();
private ListView mListView;
private MyAdapter mMyAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
long oldTime = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
Bean bean = new Bean();
bean.setName("listView"+i);
data.add(bean);
}
mListView = findViewById(R.id.list_view_lv);
mMyAdapter = new MyAdapter(data,this);
mListView.setAdapter(mMyAdapter);
long newTime = System.currentTimeMillis();
System.out.println("消耗的时间是:"+(newTime-oldTime)+"ms");
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Log.d(TAG, "onItemClick:"+i);
}
});
}
}
3、效果图
|