上一篇文章介绍了ListFragment,当中的ListView并没有自己定义适配器,实际上在实际开发中常会用到自己定义适配器,是实现更复杂的列表数据展示。

所以这篇文章添加了自己定义适配器。来进行ListView数据的展示。

实现效果图:

左边是Activity中的一个button。点击button会出现右边的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;
@Override
protected 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() { @Override
public 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>(); @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); for (int i = 0; i < 30; i++) {
list.add("item" + i);
}
adapter = new MyAdapter(getActivity(), list);
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.article, null);
setListAdapter(adapter);
return view;
}
@Override
public 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;
} @Override
public int getCount() {
return list.size();
} @Override
public Object getItem(int position) {
return list.get(position);
} @Override
public long getItemId(int position) {
return position;
} @Override
public 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;
}
}

源码下载:

点击下载源代码

Android ListFragment实例Demo(自己定义适配器)的更多相关文章

  1. Android ExpandableListView实例Demo

    前几篇文章介绍了Listview.但在实际开发中也常常会用到多层的Listview来展示数据,比方qq中的好友展示,所以这张来了解一下ExpandableListview.基本思想与Listview大 ...

  2. Android BroadcastReceiver实例Demo(有序广播的发送)

    上一篇简介了广播的发送,这篇主要介绍下,有序广播的发送. 设置完相关属性的时候,广播就会依照有序的方式进行发送: 发送顺序: 先发送第二条广播: 再发送第一条广播: 最后发送第三条广播. 代码例如以下 ...

  3. Android之SlideMenu实例Demo

    年末加班基本上一周都没什么时候回家写代码,回到家就想睡觉,周末难得有时间写个博客,上次写了一篇关于SlideMenu开源项目的导入问题,这次主要讲讲使用的问题,SlideMenu应用的广泛程度就不用说 ...

  4. Android微信分享功能实例+demo

    Android微信分享功能实例 1 微信开放平台注册 2 获得appId,添加到程序中,并运行程序 3 使用应用签名apk生成签名,添加到微信开放平台应用签名,完成注册 4 测试分享功能. 有问题请留 ...

  5. Android开发工程师文集-Fragment,适配器,轮播图,ScrollView,Gallery 图片浏览器,Android常用布局样式

    Android开发工程师文集-Fragment,适配器,轮播图,ScrollView,Gallery 图片浏览器,Android常用布局样式 Fragment FragmentManager frag ...

  6. JMeter学习-014-JMeter 配置元件实例之 - 用户定义的变量 参数化配置

    前文讲述了通过 CSV Data Set Config 实现参数化配置(详情敬请参阅:JMeter学习-010-JMeter 配置元件实例之 - CSV Data Set Config 参数化配置), ...

  7. Android 举例说明自己的定义Camera图片和预览,以及前后摄像头切换

    如何调用本地图片,并调用系统拍摄的图像上一博文解释(http://blog.csdn.net/a123demi/article/details/40003695)的功能. 而本博文将通过实例实现自己定 ...

  8. Android学习小Demo(19)利用Loader来实时接收短信

    之前写过一篇文章<Android学习小Demo(13)Android中关于ContentObserver的使用>,在里面利用ContentOberver去监測短信URI内容的变化.我们先来 ...

  9. android JNI 简单demo(2)它JNI demo 写

    android JNI 简单demo(2)它JNI demo 写 一.搭建Cygwin 环境:http://blog.csdn.net/androidolblog/article/details/25 ...

随机推荐

  1. POJ 3264 Balanced Lineup(RMQ)

    点我看题目 题意 :N头奶牛,Q次询问,然后给你每一头奶牛的身高,每一次询问都给你两个数,x y,代表着从x位置上的奶牛到y位置上的奶牛身高最高的和最矮的相差多少. 思路 : 刚好符合RMQ的那个求区 ...

  2. PYTHON多进程编码结束之进程池POOL

    结束昨晚开始的测试. 最后一个POOL. A,使用POOL的返回结果 #coding: utf-8 import multiprocessing import time def func(msg): ...

  3. [wikioi]装箱问题

    http://wikioi.com/problem/1014/ 01背包问题是最经典的动态规划之一,这道题目甚至是这其中还简单的一种,因为价值就是本身的重量了.本来比如,w是总重量限制,v[]是每个的 ...

  4. simplemodal — jquery弹出窗体插件

    方式一:使用jquery-1.7.1.min.js(1.9.1的版本我试过了,不行) + jquery_modal.js的方式 文件:        testModel.css: /* Overlay ...

  5. 如何将Springside4项目转成Eclipse项目

    1)下载springside4 官网地址 http://www.springside.org.cn/download.html 2)运行CMD,进入 C:\Documents and Settings ...

  6. RecyclerView一个奇怪的npe异常

    java.lang.NullPointerException at android.support.v7.widget.RecyclerView.computeVerticalScrollOffset ...

  7. MySQL 内存监控

    上一篇blog介绍了因为sql查询information_schema表而导致内存暴涨的case. 今天顺便做了一个thd内存的监控: 先来介绍下MySQL的内存: 1. 线程内内存:thd-> ...

  8. BZOJ3585: mex

    3585: mex Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 322  Solved: 169[Submit][Status] Descripti ...

  9. oracle rac scan ip 用途 原理

    Oracle 11G R2 RAC增加了scan ip功能,在11.2之前,client链接数据库的时候要用vip,假如你的cluster有4个节点,那么客户端的tnsnames.ora中就对应有四个 ...

  10. .net,sessionState的Session共享问题解决方案

    最近项目因为要负载均衡所以就使用了sessionState的Session共享,但是却发现多台服务器中有个别服务器的Session没有共享,于是就有了这篇文章,下面开始说说. 这个基本上就分两种情况: ...