Android控件之ListView

来源:互联网 发布:intent 获取数据 编辑:程序博客网 时间:2024/04/17 02:27

实现代码:

xml:

[html] view plain copy
  1. <span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>    
  2. <LinearLayout    
  3.     xmlns:android="http://schemas.android.com/apk/res/android"    
  4.     xmlns:app="http://schemas.android.com/apk/res-auto"    
  5.     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"    
  6.     android:layout_height="match_parent" tools:context="com.zking.laci.android08.MainActivity">    
  7.     
  8.     <ListView    
  9.         android:layout_width="match_parent"    
  10.         android:layout_height="wrap_content"    
  11.         android:id="@+id/lv_list"    
  12.         android:layout_weight="1"    
  13.         ></ListView>    
  14.     
  15.     
  16. </LinearLayout>  </span>  


注意:如果不写Android:descendantFocusability="blocksDescendants",ListView点击事件就会和Button的点击事件冲突,只出现Button的点击效果

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

Java:

[html] view plain copy
  1. <span style="font-size:18px;">    
  2. import android.widget.Button;    
  3. import android.widget.ImageView;    
  4. import android.widget.TextView;    
  5.     
  6. /**   
  7.  * Created by Laci on 2017/6/9.   
  8.  */    
  9.     
  10. public class ItemTag {    
  11.     public ImageView iv;    
  12.     public TextView tv;    
  13.     public Button bt;    
  14. }  </span>  



[html] view plain copy
  1. <span style="font-size:18px;">import android.support.v7.app.AppCompatActivity;    
  2. import android.os.Bundle;    
  3. import android.view.View;    
  4. import android.view.ViewGroup;    
  5. import android.widget.AdapterView;    
  6. import android.widget.BaseAdapter;    
  7. import android.widget.Button;    
  8. import android.widget.ImageView;    
  9. import android.widget.ListView;    
  10. import android.widget.SimpleAdapter;    
  11. import android.widget.TextView;    
  12. import android.widget.Toast;    
  13.     
  14. import java.util.ArrayList;    
  15. import java.util.HashMap;    
  16. import java.util.List;    
  17. import java.util.Map;    
  18.     
  19. public class MainActivity extends AppCompatActivity {    
  20.     private ListView lv;    
  21.     private int images[]={R.drawable.bird,R.drawable.cat,R.drawable.chicken};    
  22.     private String titles[]={"小鸟","猫猫","火鸡"};    
  23.     private Button bt;    
  24.     
  25.     @Override    
  26.     protected void onCreate(Bundle savedInstanceState) {    
  27.         super.onCreate(savedInstanceState);    
  28.         setContentView(R.layout.activity_main);    
  29.     
  30.         lv= (ListView) findViewById(R.id.lv_list);    
  31.         lv.setAdapter(new MyAdapter());    
  32.         lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {    
  33.             @Override    
  34.             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {    
  35.                 Toast.makeText(MainActivity.this, "跳转到"+titles[position%titles.length], Toast.LENGTH_SHORT).show();    
  36.             }    
  37.         });    
  38.     
  39.     }    
  40.     class MyAdapter extends BaseAdapter{    
  41.     
  42.         //指定的view有多少个    
  43.         @Override    
  44.         public int getCount() {    
  45.             return 1000;    
  46.         }    
  47.     
  48.         //内容    
  49.         @Override    
  50.         public Object getItem(int position) {    
  51.             return titles[position%titles.length];    
  52.         }    
  53.     
  54.         //每一行的下标    
  55.         @Override    
  56.         public long getItemId(int position) {    
  57.             return position;    
  58.         }    
  59.     
  60.         //拿到每一行的view    
  61.         @Override    
  62.         public View getView(final int position, View convertView, ViewGroup parent) {    
  63.             //把布局文件转成view    
  64.             if(convertView==null){    
  65.                 convertView=getLayoutInflater().inflate(R.layout.res,null);    
  66.                 //实例化自己写的实体类    
  67.                 ItemTag it=new ItemTag();    
  68.                 it.iv= (ImageView) convertView.findViewById(R.id.res_iv);    
  69.                 it.tv= (TextView) convertView.findViewById(R.id.res_tv);    
  70.                 it.bt= (Button) convertView.findViewById(R.id.res_bt);    
  71.                 convertView.setTag(it);    
  72.             }    
  73.             ItemTag itg= (ItemTag) convertView.getTag();    
  74.     
  75.             //设置    
  76.             itg.iv.setImageResource(images[position%titles.length]);    
  77.             itg.tv.setText(titles[position%titles.length]);    
  78.             itg.bt.setOnClickListener(new View.OnClickListener() {    
  79.                 @Override    
  80.                 public void onClick(View v) {    
  81.                     Toast.makeText(MainActivity.this, "状态下载"+titles[position%titles.length], Toast.LENGTH_SHORT).show();    
  82.                 }    
  83.             });    
  84.             return convertView;    
  85.         }    
  86.     }    
  87. }  </span>