Android 在slidingMenu中的ListFrament中使用自定义的ListView
2014-05-07 17:58
507 查看
以前就有用过自定义的ListView,不过很久没用过了,所以决定自己写一篇博客,以记录再次学习如何使用自定义的ListView,防止以后在此使用又忘了。本文中将实现一个在ListView中存放最多三个需要监听的ListView。
发现有个东西写 蛮不错的,引用一下,思路会清晰些:
这里其实是在一个ListFrament里面,使用了自定义的Listview,摸索了很久终于实现了自己想要的效果
第一步:首先主布局文件:
<?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" >
<ListView
android:layout_marginBottom="20dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:id="@android:id/list"
android:listSelector="#0000"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView>
</LinearLayout>然后组件布局文件:
<?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" >
<TextView
android:id="@+id/talent_analytics_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="人力结构分析"
android:textStyle="bold"
android:textSize="18dp"
/>
<!-- 第二排 3个Textview -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="15dp"
>
<TextView
android:id="@+id/talent_analytics_cen1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="职等结构"
android:textColor="#14D7F1"
android:textSize="14dp"
/>
<TextView
android:id="@+id/talent_analytics_cen1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:text="年龄结构"
android:textColor="#14D7F1"
android:textSize="14dp"
/>
<TextView
android:id="@+id/talent_analytics_cen1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:text="工作年限结构"
android:textColor="#14D7F1"
android:textSize="14dp"
/>
</LinearLayout>
</LinearLayout>
第二步:1、新建一个适配器类,继承于BaseAdapter
package com.example.dayhr1_3;
import java.util.List;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
/**
* 人力分析适配器
* @author wxm
*
*/
public class APPAdapter extends BaseAdapter {
//列表数据
public List<ApplictionInfo> appListViewData = null;
public LayoutInflater layoutInflater = null;
public Context ct;
//构造函数
public APPAdapter(List<ApplictionInfo> app,
Context ctx)
{
this.ct=ctx;
appListViewData = app;
this.layoutInflater=LayoutInflater.from(ct);
}
//列表需要显示多少行
@Override
public int getCount() {
// TODO Auto-generated method stub
return appListViewData.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return appListViewData.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
//列表每一行需要显示的视图
View view = null;
//列表中组件
ViewHolder viewGroup = null;
if(arg1==null||arg1.getTag()==null)
{
view = layoutInflater.inflate(R.layout.main_talent_analytics_list, null);
TextView text0=(TextView)view.findViewById(R.id.talent_analytics_top);
TextView text1=(TextView)view.findViewById(R.id.talent_analytics_cen1);
TextView text2=(TextView)view.findViewById(R.id.talent_analytics_cen2);
TextView text3=(TextView)view.findViewById(R.id.talent_analytics_cen3);
text1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//
if(arg0==0){//添加一个判断,判断是点击了哪一列的,以做出对应的跳转下面的都雷同
Intent intent=new Intent();
intent.setClass(ct, OrgNavigationActivity_2.class);
ct.startActivity(intent);
}
}
});
text2.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//
Intent intent=new Intent();
intent.setClass(ct, OrgNavigationActivity_2.class);
ct.startActivity(intent);
}
});
text3.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//
Intent intent=new Intent();
intent.setClass(ct, OrgNavigationActivity_2.class);
ct.startActivity(intent);
}
});
viewGroup = new ViewHolder(view);
view.setTag(viewGroup);
}else
{
view = arg1;
viewGroup = (ViewHolder) arg1.getTag();
}
//把列表中数据添加到列表视图中
ApplictionInfo app = new ApplictionInfo();
app = (ApplictionInfo) getItem(arg0);
viewGroup.text0.setText(app.gettext0());
viewGroup.text1.setText(app.gettext1());
viewGroup.text2.setText(app.gettext2());
viewGroup.text3.setText(app.gettext3());
return view;
}
//实例化列表中每一行的组件
class ViewHolder{
TextView text0; //程序图标
TextView text1; //程序图标
TextView text2; //程序名称
TextView text3; //程序包名
//构造方法
ViewHolder(View v){
this.text0 = (TextView)v.findViewById(R.id.talent_analytics_top);
this.text1 = (TextView)v.findViewById(R.id.talent_analytics_cen1);
this.text2 = (TextView)v.findViewById(R.id.talent_analytics_cen2);
this.text3 = (TextView)v.findViewById(R.id.talent_analytics_cen3);
}
}
}
第二步:2,新建一个操作类,applicationInfo
package com.example.dayhr1_3;
import android.graphics.drawable.Drawable;
public class ApplictionInfo {
/*
* 用来保存应用程序的类
*/
public String text0;//1排1号位
public String text1;//1排1号位
public String text2;//1排2号位
public String text3;//1排3号位
public void settext0(String label) {
this.text0 = label;
}
public String gettext0() {
return text0;
}
/*
* 设置1排1号位名称
*/
public void settext1(String label)
{
this.text1=label;
}
/*
* 获取1排1号位名称
*/
public String gettext1()
{
return text1;
}
/*
* 设置1排2号位名称
*/
public void settext2(String label)
{
this.text2=label;
}
/*
* 获取1排2号位名称
*/
public String gettext2()
{
return text2;
}
/**
* 设置1排2号位名称
* @param label
*/
public void settext3(String label)
{
this.text3 = label;
}
/**
* 获取1排3号位名称
* @return
*/
public String gettext3()
{
return text3;
}
}
第三步:在ListFrament中
public APPAdapter appAdapter=null;
然后在新替换的layout下面添加一些语句
case 1:
gv1=R.layout.main_talent_analytics;
getActivity().setTitle("人力分析");
//由于这里是用的LIstFrament ,ListView的id的设置是 android:id="@android:id/list",不需要去findViewById,直接使用就好了
//创建适配器;
appAdapter = new APPAdapter(getlist(), getActivity());
//.添加适配器
setListAdapter(appAdapter);
break;
这里的getlist()代码如下:
private List<ApplictionInfo> getlist() {
// TODO Auto-generated method stub
List<ApplictionInfo> list = new ArrayList<ApplictionInfo>();
ArrayList<HashMap<String, Object>> list1=new ArrayList<HashMap<String,Object>>();
String text0[]={"人力结构分析","人工成本结构分析", "员工流动分析","超缺编分析"
};
String text1[]={"职等结构","成本结构", "流入流出","超缺编分析"
};
String text2[]={"年龄结构","组织分布", "入职结构",null
};
String text3[]={"工作年限结构","职位体系分布", "离职结构",null
};
for(int i=0;i<text1.length;i++){
ApplictionInfo info = new ApplictionInfo();
info.settext0(text0[i]);
info.settext1(text1[i]);
info.settext2(text2[i]);
info.settext3(text3[i]);
list.add(info);
}
return list;
}
![](https://img-blog.csdn.net/20140507181259312?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd3htMTIyNTkyOTY5MA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
发现有个东西写 蛮不错的,引用一下,思路会清晰些:
这里其实是在一个ListFrament里面,使用了自定义的Listview,摸索了很久终于实现了自己想要的效果
第一步:首先主布局文件:
<?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" >
<ListView
android:layout_marginBottom="20dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:id="@android:id/list"
android:listSelector="#0000"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView>
</LinearLayout>然后组件布局文件:
<?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" >
<TextView
android:id="@+id/talent_analytics_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="人力结构分析"
android:textStyle="bold"
android:textSize="18dp"
/>
<!-- 第二排 3个Textview -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="15dp"
>
<TextView
android:id="@+id/talent_analytics_cen1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="职等结构"
android:textColor="#14D7F1"
android:textSize="14dp"
/>
<TextView
android:id="@+id/talent_analytics_cen1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:text="年龄结构"
android:textColor="#14D7F1"
android:textSize="14dp"
/>
<TextView
android:id="@+id/talent_analytics_cen1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:text="工作年限结构"
android:textColor="#14D7F1"
android:textSize="14dp"
/>
</LinearLayout>
</LinearLayout>
第二步:1、新建一个适配器类,继承于BaseAdapter
package com.example.dayhr1_3;
import java.util.List;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
/**
* 人力分析适配器
* @author wxm
*
*/
public class APPAdapter extends BaseAdapter {
//列表数据
public List<ApplictionInfo> appListViewData = null;
public LayoutInflater layoutInflater = null;
public Context ct;
//构造函数
public APPAdapter(List<ApplictionInfo> app,
Context ctx)
{
this.ct=ctx;
appListViewData = app;
this.layoutInflater=LayoutInflater.from(ct);
}
//列表需要显示多少行
@Override
public int getCount() {
// TODO Auto-generated method stub
return appListViewData.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return appListViewData.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
//列表每一行需要显示的视图
View view = null;
//列表中组件
ViewHolder viewGroup = null;
if(arg1==null||arg1.getTag()==null)
{
view = layoutInflater.inflate(R.layout.main_talent_analytics_list, null);
TextView text0=(TextView)view.findViewById(R.id.talent_analytics_top);
TextView text1=(TextView)view.findViewById(R.id.talent_analytics_cen1);
TextView text2=(TextView)view.findViewById(R.id.talent_analytics_cen2);
TextView text3=(TextView)view.findViewById(R.id.talent_analytics_cen3);
text1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//
if(arg0==0){//添加一个判断,判断是点击了哪一列的,以做出对应的跳转下面的都雷同
Intent intent=new Intent();
intent.setClass(ct, OrgNavigationActivity_2.class);
ct.startActivity(intent);
}
}
});
text2.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//
Intent intent=new Intent();
intent.setClass(ct, OrgNavigationActivity_2.class);
ct.startActivity(intent);
}
});
text3.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//
Intent intent=new Intent();
intent.setClass(ct, OrgNavigationActivity_2.class);
ct.startActivity(intent);
}
});
viewGroup = new ViewHolder(view);
view.setTag(viewGroup);
}else
{
view = arg1;
viewGroup = (ViewHolder) arg1.getTag();
}
//把列表中数据添加到列表视图中
ApplictionInfo app = new ApplictionInfo();
app = (ApplictionInfo) getItem(arg0);
viewGroup.text0.setText(app.gettext0());
viewGroup.text1.setText(app.gettext1());
viewGroup.text2.setText(app.gettext2());
viewGroup.text3.setText(app.gettext3());
return view;
}
//实例化列表中每一行的组件
class ViewHolder{
TextView text0; //程序图标
TextView text1; //程序图标
TextView text2; //程序名称
TextView text3; //程序包名
//构造方法
ViewHolder(View v){
this.text0 = (TextView)v.findViewById(R.id.talent_analytics_top);
this.text1 = (TextView)v.findViewById(R.id.talent_analytics_cen1);
this.text2 = (TextView)v.findViewById(R.id.talent_analytics_cen2);
this.text3 = (TextView)v.findViewById(R.id.talent_analytics_cen3);
}
}
}
第二步:2,新建一个操作类,applicationInfo
package com.example.dayhr1_3;
import android.graphics.drawable.Drawable;
public class ApplictionInfo {
/*
* 用来保存应用程序的类
*/
public String text0;//1排1号位
public String text1;//1排1号位
public String text2;//1排2号位
public String text3;//1排3号位
public void settext0(String label) {
this.text0 = label;
}
public String gettext0() {
return text0;
}
/*
* 设置1排1号位名称
*/
public void settext1(String label)
{
this.text1=label;
}
/*
* 获取1排1号位名称
*/
public String gettext1()
{
return text1;
}
/*
* 设置1排2号位名称
*/
public void settext2(String label)
{
this.text2=label;
}
/*
* 获取1排2号位名称
*/
public String gettext2()
{
return text2;
}
/**
* 设置1排2号位名称
* @param label
*/
public void settext3(String label)
{
this.text3 = label;
}
/**
* 获取1排3号位名称
* @return
*/
public String gettext3()
{
return text3;
}
}
第三步:在ListFrament中
public APPAdapter appAdapter=null;
然后在新替换的layout下面添加一些语句
case 1:
gv1=R.layout.main_talent_analytics;
getActivity().setTitle("人力分析");
//由于这里是用的LIstFrament ,ListView的id的设置是 android:id="@android:id/list",不需要去findViewById,直接使用就好了
//创建适配器;
appAdapter = new APPAdapter(getlist(), getActivity());
//.添加适配器
setListAdapter(appAdapter);
break;
这里的getlist()代码如下:
private List<ApplictionInfo> getlist() {
// TODO Auto-generated method stub
List<ApplictionInfo> list = new ArrayList<ApplictionInfo>();
ArrayList<HashMap<String, Object>> list1=new ArrayList<HashMap<String,Object>>();
String text0[]={"人力结构分析","人工成本结构分析", "员工流动分析","超缺编分析"
};
String text1[]={"职等结构","成本结构", "流入流出","超缺编分析"
};
String text2[]={"年龄结构","组织分布", "入职结构",null
};
String text3[]={"工作年限结构","职位体系分布", "离职结构",null
};
for(int i=0;i<text1.length;i++){
ApplictionInfo info = new ApplictionInfo();
info.settext0(text0[i]);
info.settext1(text1[i]);
info.settext2(text2[i]);
info.settext3(text3[i]);
list.add(info);
}
return list;
}
ok 到这里就完成了,为了让每个item 点击不变颜色,给Listview添加了一个属性android:listSelector="#0000", 然后每个it之前的黑线是写了一个LinearLayout,宽为父空间宽度高为0.5dp,背景色为#D5DCEE
相关文章推荐
- Android 之自定义CheckBox结合ListView使用
- Android 使用CheckBox实现ListView自定义单选
- 【Android基础】listview控件的使用(4)-----自定义布局的listview的使用
- Android定制ListView的界面(使用继承自ArrayAdapter的自定义适配器)--《第一行代码Android》学习笔记
- Android---快速拨话程序(涵盖appWidget、Tab、联系人、多线程、自定义ListView使用)
- Android 对Layout_weight属性完全解析以及使用ListView来实现表格(自定义适配器)
- Android定制ListView的界面(使用继承自ArrayAdapter的自定义适配器)--《第一行代码Android》学习笔记
- Android得到系统已安装应用程序包列表方法 自定义ListView显示 PackageManager的使用
- Android网络框架-Volley实践 使用Volley打造自定义ListView
- android开发(34) 自定义 listView的分割线( 使用xml drawable画多条线)
- Android初试--自定义ListView的使用
- Android中使用ListView绘制自定义表格(2)
- Android开发使用自定义view实现ListView下拉的视差特效功能
- Android 在ListView中使用自定义的ViewGroup来取代ViewHolde
- 【Android】使用自定义Adapter优化ListView、修改数据及控件内部布局
- Android ListView自定义Adapter使用误区
- Android使用ScrollView和自定义的ListView实现两列的ListView
- Android自定义适配器和ListView的点击事件相结合的使用
- Android中使用ListView绘制自定义表格技巧分享
- Android---快速拨话程序(涵盖appWidget、Tab、联系人、多线程、自定义ListView使用)