Android ListFragment实例Demo(自定义适配器)
来源:互联网 发布:8051单片机基础教程 编辑:程序博客网 时间:2024/05/15 23:50
上一篇文章介绍了ListFragment,其中的ListView并没有自定义适配器,实际上在实际开发中常会用到自定义适配器,是实现更复杂的列表数据展示。所以这篇文章增加了自定义适配器,来进行ListView数据的展示。
实现效果图:
左边是Activity中的一个按钮,点击按钮会出现右边的Fragment相应的数据列表。
代码展示:
布局文件:
activity_main:
<LinearLayout 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" tools:context=".MainActivity" > <LinearLayout android:id="@+id/left" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical" android:background="#DDCCFF"> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="显示列表" /> </LinearLayout> <LinearLayout android:id="@+id/right" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="2" android:orientation="vertical" android:background="#DDFFCC"> </LinearLayout></LinearLayout>
article.xml:
<LinearLayout 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" tools:context=".MainActivity" > <ListView android:id="@id/android:list" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView></LinearLayout>
item.xml:
<LinearLayout 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" tools:context=".MainActivity" > <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp"/></LinearLayout>代码文件:
package com.fragmentdemo9_listfragment2;import android.app.Activity;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.os.Bundle;import android.view.View;import android.widget.Button;/** *ListFragment的实例Demo优化版一。 * */public class MainActivity extends Activity {private Button button;private FragmentManager manager;private FragmentTransaction transaction;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);manager = getFragmentManager();button = (Button) findViewById(R.id.button1);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {transaction = manager.beginTransaction();ArticleListFragment articleListFragment = new ArticleListFragment();transaction.replace(R.id.right, articleListFragment, "right");transaction.commit();}});}}
ArticleListFragment.java:
package com.fragmentdemo9_listfragment2;import java.util.ArrayList;import android.app.ListFragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ListView;import android.widget.Toast;/** * 本例中的唯一一个Fragment * @author Administrator * */public class ArticleListFragment extends ListFragment {private MyAdapter adapter;private ArrayList<String> list = new ArrayList<String>();@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);for (int i = 0; i < 30; i++) {list.add("item" + i);}adapter = new MyAdapter(getActivity(), list);}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View view = inflater.inflate(R.layout.article, null);setListAdapter(adapter);return view;}@Overridepublic void onListItemClick(ListView l, View v, int position, long id) {super.onListItemClick(l, v, position, id);Toast.makeText(getActivity(), list.get(position), Toast.LENGTH_SHORT).show();}}
MyAdapter:
package com.fragmentdemo9_listfragment2;import java.util.ArrayList;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.TextView;/** * 自定义适配器MyAdapter * */public class MyAdapter extends BaseAdapter {private ArrayList<String> list;private Context context;public MyAdapter(Context context, ArrayList<String> list) {this.list = list;this.context = context;}@Overridepublic int getCount() {return list.size();}@Overridepublic Object getItem(int position) {return list.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder holder = new ViewHolder();;if (convertView == null) {convertView = LayoutInflater.from(context).inflate(R.layout.item,null);holder.textView = (TextView) convertView.findViewById(R.id.textView);convertView.setTag(holder);} else {holder = (ViewHolder) convertView.getTag();}holder.textView.setText(list.get(position).toString());return convertView;}class ViewHolder {TextView textView;}}
源代码下载:
点击下载源码
2 0
- Android ListFragment实例Demo(自定义适配器)
- <Android学习笔记四>Android ListFragment实例Demo(自定义适配器)
- Android ListFragment实例Demo
- Android自定义ListFragment
- Android自定义ListFragment 显示
- Fragment学习之ListFragment及自定义适配器
- ListFragment实例
- Android ListFragment自定义view报错解决方案
- android listView 适配器demo
- Android ListFragment
- android Listfragment
- Android ListFragment
- android分组数据适配器demo
- Android自定义适配器 (BaseAdapter)及优化
- Android基础-自定义适配器
- Android自定义适配器BaseAdapter
- Android自定义Adapter适配器
- Android自定义适配器
- C++ HUSTR与两个版本的HUMAP实现
- 电信三雄4G实测! 中华电北市覆盖率差
- 苹果App Store点燃世界盃热潮!
- C语言图形模式编程,个性鼠标。
- 全台第一场Beacon Hackathon登场!
- Android ListFragment实例Demo(自定义适配器)
- 开启零售新革命 阿里推「码上淘」
- 传 iOS 8 将具备分割画面多工模式,还有各种比例...
- DIV的上下左右均居中 jboss7.1.1Final的jsp热部署解决方案。
- 解决Runtime.exec()命令行重定向保存文件
- Java NIO使用及原理分析(三)
- 关于java中多线程的一点理解
- IEEE EMC 办电磁产学研讨
- Hibernate @Temporal的使用