在ListFragment中使用base-adapter

在ListFragment中使用base-adapter

base-adapter引用的鸿洋大神的博客–
Android 快速开发系列 打造万能的ListView GridView 适配器

使用onCreateView初始化布局

public View onCreateView(LayoutInflater inflater, ViewGroup container,  
            Bundle savedInstanceState) {  
        View view = inflater.inflate(R.layout.listview, container,false); 
        list = (ListView) view.findViewById(android.R.id.list); 

        return view; 
    } 

需要注意的是:R.layout.listview中listview Id的写法

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" >  

    <ListView android:id="@id/android:list" android:layout_width="fill_parent" android:layout_height="wrap_con ></ListView> </RelativeLayout> 

Oncreate

数据源

 List<Bean> mDatas = new ArrayList<Bean>();
            Bean bean=new Bean("美女1只","见到妹子一直","2015.12.23","17865662722");
            mDatas.add(bean);
            bean=new Bean("美女2只","见到妹子一直","2015.12.23","17865662722");
            mDatas.add(bean);
            bean=new Bean("美女3只","见到妹子一直","2015.12.23","17865662722");
            mDatas.add(bean);
            bean=new Bean("美女4只","见到妹子一直","2015.12.23","17865662722");
            mDatas.add(bean);
            bean=new Bean("美女5只","见到妹子一直","2015.12.23","17865662722");
            mDatas.add(bean);
            bean=new Bean("美女5只","见到妹子一直","2015.12.23","17865662722");
            mDatas.add(bean);

Bean

public class Bean {

    private String title;
    private String desc;
    private String time;
    private String phone;

    public Bean(String title,String desc,String time,String phone){
        this.title=title;
        this.desc=desc;
        this.time=time;
        this.phone=phone;
    }

    public String title() {
        return title;
    }

    public String desc() {
        return title;
    }

    public String time() {
        return title;
    }

    public String phone() {
        return phone;
    }


}

适配器

mAdapter=new CommonAdapter<Bean>(getActivity().getApplicationContext(), mDatas, R.layout.item_list) {

                public void convert(ViewHolder helper, Bean item) {
                    // TODO Auto-generated method stub
                    helper.setText(R.id.tv_title,item.title());
                    helper.setText(R.id.tv_describe,item.desc()); 
                    helper.setText(R.id.tv_phone,item.phone()); 
                    helper.setText(R.id.tv_time,item.time()); 
                    }
            };

            setListAdapter(mAdapter);

需要注意的是listfragment中设置适配器要使用setListAdapter(mAdapter);

布局代码为

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffff" android:orientation="vertical" android:padding="10dp" >

    <TextView  android:id="@+id/tv_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:singleLine="true" android:text="红色钱包" android:textSize="16sp" android:textColor="#444444" >
    </TextView>

    <TextView  android:id="@+id/tv_describe" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/tv_title" android:layout_marginTop="10dp" android:maxLines="2" android:minLines="1" android:text="周三早上丢失了红色钱包,在食堂二楼" android:textColor="#898989" android:textSize="16sp" >
    </TextView>

    <TextView  android:id="@+id/tv_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv_describe" android:layout_marginTop="10dp" android:text="20130240122" android:textColor="#898989" android:textSize="12sp" >
    </TextView>

    <TextView  android:id="@+id/tv_phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@id/tv_describe" android:layout_marginTop="10dp" android:background="#5cbe6c" android:drawablePadding="5dp" android:paddingBottom="3dp" android:paddingLeft="5dp" android:paddingRight="5dp" android:paddingTop="3dp" android:text="138024249542" android:textColor="#ffffff" android:textSize="12sp" >
    </TextView>

</RelativeLayout>

若数据源不是Bean 则

mAdapter=new CommonAdapter<String>(getApplicationContext(), mDatas, R.layout.item_list) {

            public void convert(ViewHolder helper, String item) {
                // TODO Auto-generated method stub
                helper.setText(R.id.id_tv_title, item);  
                }
        };

你可能感兴趣的:(android,ListView,Fragment)