Fragment中使用listview
来源:互联网 发布:人工智能带来的问题 编辑:程序博客网 时间:2024/05/17 21:51
按照步骤来就可以达到效果:
1-新建一个listview.xml
是listview中每个items的样式
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#f1e4f1" android:orientation="horizontal"> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#666872" /> <Button android:id="@+id/view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="详细" /> </LinearLayout> <TextView android:id="@+id/info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#666872" /></LinearLayout>
2-新建一个适配器类
public class ListViewAdapter extends BaseAdapter { private List<Map<String, Object>> data; private LayoutInflater layoutInflater; private Context context; public ListViewAdapter(Context context,List<Map<String, Object>> data){ this.context=context; this.data=data; this.layoutInflater=LayoutInflater.from(context); } /** * 组件集合,对应list.xml中的控件 * @author Administrator */ public final class Zujian{ public ImageView image; public TextView title; public Button view; public TextView info; } @Override public int getCount() { return data.size(); } /** * 获得某一位置的数据 */ @Override public Object getItem(int position) { return data.get(position); } /** * 获得唯一标识 */ @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { Zujian zujian=null; if(convertView==null){ zujian=new Zujian(); //获得组件,实例化组件 convertView=layoutInflater.inflate(R.layout.home_listview, null); zujian.image=(ImageView)convertView.findViewById(R.id.image); zujian.title=(TextView)convertView.findViewById(R.id.title); zujian.view=(Button)convertView.findViewById(R.id.view); zujian.info=(TextView)convertView.findViewById(R.id.info); convertView.setTag(zujian); }else{ zujian=(Zujian)convertView.getTag(); } //绑定数据 zujian.image.setBackgroundResource((Integer)data.get(position).get("image")); zujian.title.setText((String)data.get(position).get("title")); zujian.info.setText((String)data.get(position).get("info")); return convertView; }}
3-在fragment的布局文件中(如main_fragment.xml)添加Listview控件
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:materialdesign="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" > <!-- ... XML CODE --> <ListView android:id="@+id/listview" android:layout_width="fill_parent" android:layout_height="fill_parent"> </ListView></RelativeLayout>
4-在Fragment.java中调用Listview
public class FragmentIHome extends BaseFragment{ private ListView listView; public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view= inflater.inflate(R.layout.home_fragment , container, false); listView = (ListView)view.findViewById(R.id.home_listview); List<Map<String, Object>> list=getData(); listView.setAdapter(new HomeListViewAdapter(getActivity(), list)); return view; } public List<Map<String, Object>> getData(){ List<Map<String, Object>> list=new ArrayList<Map<String,Object>>(); for (int i = 0; i < 10; i++) { Map<String, Object> map=new HashMap<String, Object>(); map.put("image", R.drawable.ic_launcher); map.put("title", "这是一个标题"+i); map.put("info", "这是一个详细信息" + i); list.add(map); } return list; }}
1 0
- Fragment中使用listview
- Fragment中使用listview
- 在Fragment中使用Listview
- 使用ListView的Fragment
- 使用ListView的Fragment
- 使用ListView的Fragment
- 使用ListView的Fragment
- 使用ListView的Fragment
- Fragment中使用ListView为什么不显示出数据?
- 在Fragment中使用PullToRefreshScrollView嵌套Listview问题的解决
- Fragment中添加ListView而不使用ListFragment
- fragment中listview的onitemclicklistener
- 子fragment中加载listview
- Fragment中加载ListView问题
- Fragment中加载ListView问题
- android中使用底部菜单栏造成Viewpager+fragment中ListView数据被隐藏一条的解决
- 遇到的Fragment中使用setAdapter()设置ListView报空指针解决方案
- fragment中填充listView后退保存位置
- 关于SQLite,java.lang.IllegalStateException: attempt to re-open an already-closed object
- Unity C# 序列化 保存物体信息
- MYSQL性能优化--不停机分表,修改mysql表结构
- CMakeListx.txt 编辑语法学习
- java开发常用工具类
- Fragment中使用listview
- 【图论知识点】【学习笔记】ICM-ICPC程序设计系列-图论及应用——第一章、图【未更新完毕】
- 读书笔记,不定期更新,复习一下基础概念
- 使用libevent编写高并发HTTP server
- sql 优化
- Android动画——Tween动画之Translate
- 彭博士谈伤精与养精 -精有多么重要?
- 【vi】与【vim】编辑器详解
- C++第三次实验—静态成员应用