Android 程式开发:(十三)特殊碎片 —— 13.1 ListFragment

http://tech.ddvip.com/2012-09/1348930516182922.html

ListFramgent就是一个包含ListView的Fragment,它可以通过数据源(数组或游标)显示一系列的信息。ListFragment是非常有用处的,就像RSS,可能左边显示一个列表,右边显示被选中的列表所对应的内容。

可以通过继承ListFragment创建一个ListFragment对象。下面将展示如何使用ListFragment。

1. 创建一个工程:ListFragmentExample。

2. main.xml中的代码。

<?xml version="1.0" encoding="utf-8"?>      
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" >      
           
<fragment  
    android:name="net.manoel.ListFragmentExample.Fragment1" 
    android:id="@+id/fragment1" 
    android:layout_weight="0.5" 
    android:layout_width="0dp" 
    android:layout_height="200dp" />      
           
<fragment  
    android:name="net.manoel.ListFragmentExample.Fragment1" 
    android:id="@+id/fragment2" 
    android:layout_weight="0.5" 
    android:layout_width="0dp" 
    android:layout_height="300dp" />      
           
</LinearLayout>

3、在re/layout下面,新建一个文件:fragment1.xml。

<?xml version="1.0" encoding="utf-8"?>      
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">      
               
    <ListView  
        android:id="@id/android:list" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"                
        android:layout_weight="1" 
        android:drawSelectorOnTop="false"/>      
                                          
</LinearLayout>

4、在包路径下面新建一个类:Fragment1.java。

public class Fragment1 extends ListFragment {      
    String[] presidents = {      
        "Dwight D. Eisenhower",      
        "John F. Kennedy",      
        "Lyndon B. Johnson",      
        "Richard Nixon",      
        "Gerald Ford",      
        "Jimmy Carter",      
        "Ronald Reagan",      
        "George H. W. Bush",      
        "Bill Clinton",      
        "George W. Bush",      
        "Barack Obama" 
    };      
           
    @Override 
    public View onCreateView(LayoutInflater inflater,       
    ViewGroup container, Bundle savedInstanceState) {              
        return inflater.inflate(R.layout.fragment1, container, false);      
    }      
           
    @Override 
    public void onCreate(Bundle savedInstanceState) {      
        super.onCreate(savedInstanceState);      
        setListAdapter(new ArrayAdapter<String>(getActivity(),      
            android.R.layout.simple_list_item_1, presidents));      
    }      
               
    public void onListItemClick(ListView parent, View v,       
    int position, long id)       
    {                
        Toast.makeText(getActivity(),       
            "You have selected " + presidents[position],       
            Toast.LENGTH_SHORT).show();      
    }        
           
}

5、按F11在模拟器上调试。会看见有两个信息列表。

6、随便点击一行,就会有一个消息弹出。

由于在main.xml中分别设置了两个ListFragment的android:layout_height属性,所以这两个列表的高度不一样。

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值