每日日报

把复杂界面(通过xml文件实现)显示到ListView上

  1. public View getView(int position, View convertView, ViewGroup parent) {
  2. //把xml文件转化为 view对象的第一种方式 通过View的inflate方法
  3. //第一个参数 上下文
  4. //第二个参数 要转化成view对象 对应的布局id
  5. //第三个参数 viewGroup 是一个特殊的View对象 它可以加入子view 比如 LinearLayout RelativeLayout都是ViewGroup
  6. //如果这个参数传了值 那么创建出来的view 就是这个view的子view 我们在getview方法中 主要目的是把xml文件转化成view对象 显示在listview中
  7. //不需要加入到其他viewgroup中 所以这个参数传null就可以了
  8. View view = View.inflate(MainActivity.this, R.layout.item, null);
  9. //LayoutInflater 也有 inflate方法
  10. LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
  11. View view2 = inflater.inflate(R.layout.item, null);
  12. //ArrayAdapter源码 采用的这种方式获取的打气筒 通过打气筒把xml布局文件转化为view对象
  13. LayoutInflater inflater2 = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
  14. View view3 = inflater2.inflate(R.layout.item, null);
  15. return view3;
  16. }

把一个xml文件转化为View对象

    ① View的静态方法 View.inflate  (这种方式比较方便)
    ② 获取LayoutInflater对象  通过LayoutInflater的inflate方法实现
            获取LayoutInflater对象方式一 LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
           获取LayoutInflater对象方式二 LayoutInflater inflater2 = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
      (谷歌源码中采用的这种方式)
 SimpleAdapter & ArrayAdapter的使用

        12.1 ArrayAdapter

  1. public class MainActivity2 extends Activity {
  2. private String datas[] = new String[]{"张小军","张欣","张丹丹"};
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. ListView lv_list = (ListView) findViewById(R.id.lv_list);
  8. //第二个参数 用来显示数据的布局文件ID
  9. //第三个参数 布局文件中用来展示数据的具体的textview的id
  10. //第三个参数 String数组 用来显示的数据源
  11. ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.item,R.id.tv_test, datas);
  12. lv_list.setAdapter(adapter);
  13. }
  14. }
条目布局文件
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context=".MainActivity" >
  6. <TextView
  7. android:id="@+id/tv_test"
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. android:text="@string/hello_world" />
  11. </RelativeLayout>

        SimpleAdapter    

  1. public class MainActivity extends Activity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. ListView lv_list = (ListView) findViewById(R.id.lv_list);
  7. //List<map> 用来填充数据的
  8. List<Map<String, String>> data = new ArrayList<Map<String,String>>();
  9. Map<String, String> map1 = new HashMap<String, String>();
  10. map1.put("title", "中国足球又输了");
  11. map1.put("content", "2016冲击失败");
  12. data.add(map1);
  13. Map<String, String> map2 = new HashMap<String, String>();
  14. map2.put("title", "沪android12期高薪就业");
  15. map2.put("content", "平均薪水20K");
  16. data.add(map2);
  17. String[] from ={"title","content"};
  18. int[] to = {R.id.tv_title,R.id.tv_content};
  19. //第二个参数 数据map的List
  20. //第三个参数 用来显示数据的布局文件
  21. //第四个参数 String数组 数据List中 每一条数据的 key
  22. //第五个参数 数据List中 的文字内容 对应的 要显示此内容的 textView的id
  23. SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item2, from, to);
  24. lv_list.setAdapter(adapter);
  25. }
  26. }
条目的布局文件
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context=".MainActivity" >
  7. <ImageView
  8. android:id="@+id/iv_icon"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:src="@drawable/ic_launcher"
  12. android:layout_margin="5dp"/>
  13. <TextView
  14. android:id="@+id/tv_title"
  15. android:layout_toRightOf="@id/iv_icon"
  16. android:layout_height="wrap_content"
  17. android:layout_width="wrap_content"
  18. android:layout_margin="7dp"
  19. android:text="北京今年装修要多少钱"
  20. android:textSize="18sp"
  21. />
  22. <TextView
  23. android:id="@+id/tv_content"
  24. android:layout_toRightOf="@id/iv_icon"
  25. android:layout_below="@id/tv_title"
  26. android:layout_height="wrap_content"
  27. android:layout_width="wrap_content"
  28. android:text="获得报价"
  29. android:textColor="#999999"
  30. android:layout_marginLeft="8dp"
  31. android:textSize="16sp"
  32. />
  33. </RelativeLayout>
posted @ 2021-02-04 11:47  durtime  阅读(75)  评论(0)    收藏  举报