ListFragment 使用ListView and 自定义Adapter

在开发过程中经常使用Tabs + ListFragment 作为表现形式。

ListFragment 中加入ListView显示方式很容易。


[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package project.animalsound;  
  2.   
  3. import android.app.ListFragment;  
  4. import android.os.Bundle;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8. import android.widget.ListView;  
  9.   
  10. public class TabFirst extends ListFragment {  
  11.   
  12.     private AnimalListAdapter adapter = null;  
  13.       
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.           
  18.         adapter = new AnimalListAdapter (getActivity());  
  19.         setListAdapter(adapter);    
  20.     }  
  21.   
  22.     @Override  
  23.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  24.             Bundle savedInstanceState) {  
  25.         View v = inflater.inflate(R.layout.tab_first, container, false);  
  26.         return v;  
  27.     }  
  28.       
  29.     @Override  
  30.     public void onListItemClick(ListView l, View v, int position, long id) {  
  31.         System.out.println("Click On List Item!!!");  
  32.         super.onListItemClick(l, v, position, id);  
  33.     }  
  34. }  
只要在onCreateView中增加
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. View v = inflater.inflate(R.layout.tab_first, container, false);  
就可以完成。

对应的R.layout.tab_first为:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <ListView  
  8.         android:id="@+id/android:list"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.          />  
  12.   
  13. </RelativeLayout>  

在这里我们对于ListView上的每个Item的布局使用下面布局

user.xml

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="horizontal" >  
  6.   
  7.     <ImageView  
  8.         android:id="@+id/animal"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="80dp"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:layout_centerVertical="true"  
  13.         android:layout_margin="1dp" />  
  14.   
  15.   <LinearLayout  
  16.         android:layout_height="fill_parent"  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_marginLeft="20dp"  
  19.         android:layout_toRightOf="@id/animal"  
  20.         android:layout_centerVertical="true"  
  21.         android:orientation="vertical" >  
  22.   
  23.         <TextView  
  24.             android:id="@+id/cn_word"  
  25.             android:layout_width="wrap_content"  
  26.             android:layout_height="wrap_content"  
  27.             android:layout_gravity="center"  
  28.             android:textColor="#191970"  
  29.             android:textSize="30sp" />  
  30.   
  31.         <TextView  
  32.             android:id="@+id/en_word"  
  33.             android:layout_width="wrap_content"  
  34.             android:layout_height="wrap_content"  
  35.             android:layout_gravity="center"  
  36.             android:textColor="#800080"   
  37.             android:textSize="30sp"/>  
  38.     </LinearLayout>  
  39.       
  40.       <ImageView  
  41.         android:id="@+id/speaker"  
  42.         android:layout_width="wrap_content"  
  43.         android:layout_height="wrap_content"  
  44.           
  45.         android:layout_alignParentRight="true"  
  46.         android:layout_margin="15dp"  
  47.         android:layout_centerVertical="true"  
  48.           
  49.           
  50.         android:src="@drawable/speaker"  
  51.         />  
  52.   
  53. </RelativeLayout>  

但是在实现的时候常会遇到一个问题,如果相对其中的一个ImageView增加OnClickListener的时候会出现问题。

我们无法在下句获得的View对象中使用findViewById(R.id.speaker)去获得对应的ID对象。

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. View v = inflater.inflate(R.layout.tab_first, container, false);  
原因是这句只是获得了R.layout.tab_first对应的View对象。

所以需要从user.xml获得对象信息。


这里可以使用Adapter轻松完成注册Listener的过程。我们继承BaseAdapter,然后在getView中实现整个初始化的过程。

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package project.animalsound;  
  2. import android.content.Context;  
  3. import android.view.LayoutInflater;  
  4. import android.view.View;  
  5. import android.view.View.OnClickListener;  
  6. import android.view.ViewGroup;  
  7. import android.widget.BaseAdapter;  
  8. import android.widget.ImageView;  
  9. import android.widget.TextView;  
  10.   
  11. class ViewHolder {    
  12.     public ImageView animal;  
  13.     public TextView cn_word;  
  14.     public TextView en_word;  
  15.     public ImageView speaker;    
  16. }    
  17.   
  18. public class AnimalListAdapter extends BaseAdapter {    
  19.     private LayoutInflater mInflater = null;  
  20.     public AnimalListAdapter(Context context){  
  21.         super();  
  22.         mInflater = (LayoutInflater) context  
  23.         .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  24.     }  
  25.   
  26.     @Override  
  27.     public int getCount() {  
  28.         // TODO Auto-generated method stub  
  29.         return 50;  
  30.     }  
  31.   
  32.     @Override  
  33.     public Object getItem(int position) {  
  34.         // TODO Auto-generated method stub  
  35.         return null;  
  36.     }  
  37.   
  38.     @Override  
  39.     public long getItemId(int position) {  
  40.         // TODO Auto-generated method stub  
  41.         return position;  
  42.     }  
  43.   
  44.     @Override  
  45.     public View getView(int position, View convertView, ViewGroup parent) {  
  46.   
  47.         ViewHolder holder = null;    
  48.         if (convertView == null) {    
  49.             holder = new ViewHolder();    
  50.             convertView = mInflater.inflate(R.layout.user, null);  
  51.             holder.animal = (ImageView) convertView.findViewById(R.id.animal);    
  52.             holder.cn_word = (TextView) convertView.findViewById(R.id.cn_word);    
  53.             holder.en_word = (TextView) convertView.findViewById(R.id.en_word);    
  54.             holder.speaker = (ImageView) convertView.findViewById(R.id.speaker);    
  55.   
  56.             convertView.setTag(holder);    
  57.         } else {    
  58.             holder = (ViewHolder) convertView.getTag();    
  59.         }  
  60.   
  61.         holder.animal.setImageResource(R.drawable.ic_launcher);  
  62.         holder.cn_word.setText("xxxxx");  
  63.         holder.en_word.setText("ssssss");  
  64.         holder.speaker.setImageResource(R.drawable.speaker);  
  65.   
  66.         holder.speaker.setOnClickListener(new OnClickListener(){  
  67.   
  68.             @Override  
  69.             public void onClick(View v) {  
  70.                 System.out.println("Click on the speaker image on ListItem ");  
  71.             }  
  72.         });  
  73.   
  74.         return convertView;  
  75.     }    
  76.   
  77. }  


Keep it simple!
作者:N3verL4nd
知识共享,欢迎转载。
原文地址:https://www.cnblogs.com/lgh1992314/p/5834741.html