安卓之高级控件Listview的优化

时间:2021-08-19 22:43:33

效果图如下:

安卓之高级控件Listview的优化


XML代码(activity_main)如下:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="match_parent"  
  4.     android:layout_height="match_parent">  
  5.   
  6.     <ListView  
  7.         android:layout_width="match_parent"  
  8.         android:layout_height="wrap_content"  
  9.         android:id="@+id/lv_main_list"></ListView>  
  10.   
  11. </LinearLayout>  


XML代码(itemtag)如下(用来优化的):

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="horizontal"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:descendantFocusability="blocksDescendants">  
  7.   
  8.   
  9.     <ImageView  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:id="@+id/iv_main_image"/>  
  13.   
  14.     <TextView  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:layout_weight="1"  
  18.         android:id="@+id/tv_main_word"/>  
  19.   
  20.     <Button  
  21.         android:layout_width="wrap_content"  
  22.         android:layout_height="wrap_content"  
  23.         android:text="下载"  
  24.         android:id="@+id/bu_main_bu"/>  
  25.   
  26. </LinearLayout>  

JAVA代码如下(ItemTag):

可以把它想象成一个实体类

[html] view plain copy
  1. public class ItemTag {  
  2.     public ImageView imageView;  
  3.     public TextView textView;  
  4.     public Button button;  
  5. }  

Java代码如下(MainActivity):

[html] view plain copy
  1. public class MainActivity extends AppCompatActivity {  
  2.   
  3.     private ListView lv_main_list;  
  4.     private int images[]={R.drawable.bird,R.drawable.cat,R.drawable.chicken,R.drawable.duck,R.drawable.elephant};  
  5.     private String titles[]={"小鸟","猫咪","小鸡","鸭子","大象"};  
  6.   
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.activity_main);  
  11.         lv_main_list = (ListView) findViewById(R.id.lv_main_list);  
  12.         lv_main_list.setAdapter(new MyAdapter());  
  13.   
  14.         lv_main_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
  15.             @Override  
  16.             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {  
  17.                 Toast.makeText(MainActivity.this, "跳转到"+titles[position]+"页面", Toast.LENGTH_SHORT).show();  
  18.             }  
  19.         });  
  20.     }  
  21.   
  22.     class MyAdapter extends BaseAdapter{  
  23.   
  24.         @Override  
  25.         public int getCount() {  
  26.             return titles.length;  
  27.         }  
  28.   
  29.         @Override  
  30.         public Object getItem(int position) {  
  31.             return titles[position];  
  32.         }  
  33.   
  34.         @Override  
  35.         public long getItemId(int position) {  
  36.             return position;  
  37.         }  
  38.   
  39.         @Override  
  40.         public View getView(final int position, View convertView, ViewGroup parent) {  
  41.             if(convertView==null){  
  42.                 convertView=getLayoutInflater().inflate(R.layout.itemtag,null);  
  43.                 ItemTag itemTag=new ItemTag();  
  44.                 itemTag.imageView= (ImageView) convertView.findViewById(R.id.iv_main_image);  
  45.                 itemTag.textView= (TextView) convertView.findViewById(R.id.tv_main_word);  
  46.                 itemTag.button = (Button) convertView.findViewById(R.id.bu_main_bu);  
  47.                 convertView.setTag(itemTag);  
  48.             }  
  49.   
  50.             ItemTag itemTag= (ItemTag) convertView.getTag();  
  51.             itemTag.imageView.setImageResource(images[position]);  
  52.             itemTag.textView.setText(titles[position]);  
  53.             itemTag.button.setOnClickListener(new View.OnClickListener() {  
  54.                 @Override  
  55.                 public void onClick(View v) {  
  56.                     Toast.makeText(MainActivity.this, "正在下载"+titles[position], Toast.LENGTH_SHORT).show();  
  57.                 }  
  58.             });  
  59.   
  60.             return convertView;  
  61.         }  
  62.     }