ListView是Android非常重要的控件之一,它有三个要素,分别是listview控件,适配器类,数据源。
ListView优化:
- 适配器adapter中重写getView方法时,利用convertView是否为空进行view的重用。若convertView为空,则用过inflate()方法加载布局,如果不为空,则直接对convertView进行重用,这样listview在滚动时可以表现更好的性能。
- 无论inflate()加载布局还是重用convertView都会调用findviewById()方法,避免重复调用,利用ViewHolder类。
- listview的点击事件和属性优化: setOnItemClickListener, setOnItemLongClickListener
下面通过一个具体的例子来演示上面的几点优化:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"
android:divider="@color/colorPrimary"
android:dividerHeight="40dp"
android:scrollbars="none"/>
</RelativeLayout>
item_country.xml
<?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"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv_flag"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv_name"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"/>
</LinearLayout>
Country.java
package com.nxyuntui.testproject;
public class Country {
private String name;
private int imageId;
public String getName(){
return name;
}
public int getImageId(){
return imageId;
}
public Country(String name ,int imageId){
this.name = name;
this.imageId = imageId;
}
}
CountryAdapter.java
public class CountryAdapter extends ArrayAdapter<Country> {
private int resourceId;
public CountryAdapter(Context context, int resource, List<Country> objects) {
super(context, resource ,objects);
resourceId = resource;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Country country = getItem(position); //获取当前位置条目的数据实例
View view;
ViewHolder viewHolder = null;
if (convertView == null){
viewHolder = new ViewHolder();
//将xml转换成view对象
view = View.inflate(getContext(),resourceId,null);
viewHolder.imageView = view.findViewById(R.id.iv_flag);
viewHolder.textView = view.findViewById(R.id.iv_name);
view.setTag(viewHolder); //将viewHolder保存到view对象中
}else{
view = convertView;
viewHolder = (ViewHolder) view.getTag();
}
//设置数据
viewHolder.imageView.setImageResource(country.getImageId());
viewHolder.textView.setText(country.getName());
return view;
}
static class ViewHolder{
ImageView imageView;
TextView textView;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
private List <Country> countryList = new ArrayList<>();
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listView);
initDatas();
final CountryAdapter adapter = new CountryAdapter(this,R.layout.item_country,countryList);
listView.setAdapter(adapter);
//条目点击事件
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getApplicationContext(),countryList.get(i).getName(),Toast.LENGTH_SHORT)
.show();
}
});
//条目长按事件
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
countryList.remove(i);
adapter.notifyDataSetChanged();
return true;
}
});
}
public void initDatas(){
countryList.add(new Country("美国",R.drawable.america));
countryList.add(new Country("比利时",R.drawable.belgium));
countryList.add(new Country("巴西",R.drawable.brazil));
countryList.add(new Country("意大利",R.drawable.italy));
countryList.add(new Country("葡萄牙",R.drawable.portugal));
countryList.add(new Country("西班牙",R.drawable.spain));
countryList.add(new Country("克罗地亚",R.drawable.croatian));
}
}