在Fragment里面实现listView布局

来源:互联网 发布:数据分析建模方法 编辑:程序博客网 时间:2024/06/01 16:37
这是布局文件fragment_haoyou.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:orientation="vertical"              android:layout_width="match_parent"              android:layout_height="match_parent">    <!--首页-->    <RelativeLayout        android:id="@+id/shouye_shouyelayout"        android:layout_width="match_parent"        android:layout_height="35dip"        android:background="@color/zhujiemianbeijing"        >        <Button            android:layout_centerVertical="true"            android:layout_width="25dip"            android:layout_height="20dip"            android:background="@mipmap/fanhuijiantou"            />        <ImageView            android:id="@+id/haoyou_guanzhufensi"            android:layout_centerInParent="true"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@drawable/haoyou_select"            />        <Button            android:layout_width="25dip"            android:layout_height="25dip"            android:background="@mipmap/cirun"            android:layout_centerVertical="true"            android:layout_alignParentRight="true"            android:layout_alignParentEnd="true"            android:layout_marginRight="10dp"/>    </RelativeLayout>    <ListView        android:id="@+id/haoyou_list"        android:layout_width="fill_parent"        android:layout_height="fill_parent">    </ListView></LinearLayout>
以及item_haoyou.xml文件
<?xml version="1.0" encoding="utf-8"?><RelativeLayout    android:id="@+id/item"    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >    <TextView        android:id="@+id/tv_name"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:text="asdasdasd"        />    <TextView        android:id="@+id/tv_age"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:text="asdasdasdssssssssssssssss"        /></RelativeLayout>
然后再是JAVA文件,这里我用的
public class Fragmenthaoyou extends Fragment implements View.OnClickListener,AdapterView.OnItemClickListener,AbsListView.OnScrollListener{    private ImageView image;    private boolean temp=true;  //    View view2;    private ListView haoyouListview;    private List<Map<String, Object>> dataList;    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.fragment_haoyou,container,false);        view2 = inflater.inflate(R.layout.item_haoyou, null);        initlist(view,inflater, container);        init(view);        return view;    }    private void init(View view) {        image = (ImageView) view.findViewById(R.id.haoyou_guanzhufensi);        image.setOnClickListener(this);    }    private void initlist(View view,LayoutInflater inflater,ViewGroup container) {        haoyouListview = (ListView) view.findViewById(R.id.haoyou_list);        SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity(), getData(), R.layout.item_haoyou, new String[]{"pic", "text"}, new int[]{R.id.tv_name, R.id.tv_age});        haoyouListview.setAdapter(simpleAdapter);        haoyouListview.setOnItemClickListener(this);        haoyouListview.setOnScrollListener(this);    }    private List<? extends Map<String, ?>> getData() {        dataList = new ArrayList<Map<String, Object>>();        Map<String, Object> map1 = new HashMap<String, Object>();        map1.put("pic", R.mipmap.ic_launcher);        map1.put("text", "个人中心");        dataList.add(map1);        Map<String, Object> map2 = new HashMap<String, Object>();        map2.put("pic", R.mipmap.ic_launcher);        map2.put("text", "宝贝中心");        dataList.add(map2);        for (int i = 0; i < 20; i++) {            Map<String, Object> map = new HashMap<String, Object>();            map.put("pic", R.mipmap.ic_launcher);            map.put("text", "爱你全家a ");            dataList.add(map);        }        return dataList;    }    @Override    public void onClick(View view) {        switch (view.getId()){            case R.id.haoyou_guanzhufensi:                Data();        }    }    @Override    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {        switch (position) {            case 0:                Toast.makeText(getActivity(), "进入个人中心", Toast.LENGTH_SHORT).show();                break;            case 1:                Toast.makeText(getActivity(), "进行宝贝中心", Toast.LENGTH_SHORT).show();                break;        }    }    @Override    public void onScrollStateChanged(AbsListView absListView, int i) {    }    @Override    public void onScroll(AbsListView absListView, int i, int i1, int i2) {    }    private void Data() {        if(temp) {            image.setSelected(temp);            temp =false;        }        else{            image.setSelected(temp);            temp = true;        }    }}


0 0