android二级listview列表实现代码

今天来实现以下大众点评客户端的横向listview二级列表,先看一下样式。
 
这种横向的listview二级列表在手机软件上还不太常见,但是使用过平板的都应该知道,在平板上市比较常见的。可能是因为平板屏幕比较大,而且也能展现更多的内容。
下面来看一下我的实现步骤。
首先自定义一个listview,代码如下:


代码如下:

public class MyListView extends ListView implements Runnable {
private float mLastDownY = 0f;
private int mDistance = 0;
private int mStep = 10;
private boolean mPositive = false;
public MyListView (Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyListView (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyListView (Context context) {
super(context);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (mLastDownY == 0f && mDistance == 0) {
mLastDownY = event.getY();
return true;
}
break;
case MotionEvent.ACTION_CANCEL:
break;
case MotionEvent.ACTION_UP:
if (mDistance != 0) {
mStep = 1;
mPositive = (mDistance >= 0);
this.post(this);
return true;
}
mLastDownY = 0f;
mDistance = 0;
break;
case MotionEvent.ACTION_MOVE:
if (mLastDownY != 0f) {
mDistance = (int) (mLastDownY - event.getY());
if ((mDistance < 0 && getFirstVisiblePosition() == 0 && getChildAt(0).getTop() == 0) || (mDistance > 0 && getLastVisiblePosition() == getCount() - 1)) {
mDistance /= 2;
scrollTo(0, mDistance);
return true;
}
}
mDistance = 0;
break;
}
return super.onTouchEvent(event);
}
public void run() {
mDistance += mDistance > 0 ? -mStep : mStep;
scrollTo(0, mDistance);
if ((mPositive && mDistance <= 0) || (!mPositive && mDistance >= 0)) {
scrollTo(0, 0);
mDistance = 0;
mLastDownY = 0f;
return;
}
mStep += 1;
this.postDelayed(this, 10);
}
}

然后看一下xml的布局:


代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.example.multilistview.MyListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:choiceMode="singleChoice"
android:scrollbars="none"
android:divider="@drawable/listitem_divide"
android:listSelector="#00000000"
android:background="#e4e3de"
>
</com.example.multilistview.MyListView>

<com.example.multilistview.MyListView
android:id="@+id/subListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#e4e3de"
>

</com.example.multilistview.MyListView>
</LinearLayout>

两个自定义的listview 横向布局,然后是父listview的适配器


代码如下:

public class MyAdapter extends BaseAdapter {
Context context;
LayoutInflater inflater;
String [] foods;
int last_item;
int [] images;
private int selectedPosition = -1;
public MyAdapter(Context context,String [] foods,int[] images){
this.context = context;
this.foods = foods;
this.images = images;
inflater=LayoutInflater.from(context);
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return foods.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder = null;
if(convertView==null){
convertView = inflater.inflate(R.layout.mylist_item, null);
holder = new ViewHolder();
holder.textView =(TextView)convertView.findViewById(R.id.textview);
holder.imageView =(ImageView)convertView.findViewById(R.id.imageview);
holder.layout=(LinearLayout)convertView.findViewById(R.id.colorlayout);
convertView.setTag(holder);
}
else{
holder=(ViewHolder)convertView.getTag();
}
// 设置选中效果
if(selectedPosition == position)
{
holder.textView.setTextColor(Color.BLUE);

holder.layout.setBackgroundColor(Color.LTGRAY);
} else {
holder.textView.setTextColor(Color.WHITE);
holder.layout.setBackgroundColor(Color.TRANSPARENT);
}

holder.textView.setText(foods[position]);
holder.textView.setTextColor(Color.BLACK);
holder.imageView.setBackgroundResource(images[position]);

return convertView;
}
public static class ViewHolder{
public TextView textView;
public ImageView imageView;
public LinearLayout layout;
}
public void setSelectedPosition(int position) {
selectedPosition = position;
}
}

对应的 item布局:


代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/colorlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginTop="5dip"
/>
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="16dip"
android:layout_marginTop="8dip"
android:layout_marginLeft="8dip"
android:layout_marginBottom="8dip"/>
<!-- android:background="@drawable/selector" 自定义listview 样式-->
</LinearLayout>

然后是子适配器代码:


代码如下:

public class SubAdapter extends BaseAdapter {

Context context;
LayoutInflater layoutInflater;
String[][] cities;
public int foodpoition;
public SubAdapter(Context context, String[][] cities,int position) {
this.context = context;
this.cities = cities;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.foodpoition = position;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return cities.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return getItem(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder viewHolder = null;
final int location=position;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.sublist_item, null);
viewHolder = new ViewHolder();
viewHolder.textView = (TextView) convertView
.findViewById(R.id.textview1);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.textView.setText(cities[foodpoition][position]);
viewHolder.textView.setTextColor(Color.BLACK);

return convertView;
}
public static class ViewHolder {
public TextView textView;
}
}

对应的xml布局:


代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="aaaaa"
android:textSize="16dip"
android:layout_marginTop="10dip"
android:layout_marginLeft="8dip"
android:layout_marginBottom="8dip"/>
</LinearLayout>

最后看下主activity的实现代码


代码如下:

public class MainActivity extends Activity {
private MyListView listView;
private MyListView subListView;
private MyAdapter myAdapter;
private SubAdapter subAdapter;

String cities[][] = new String[][] {
new String[] {"全部美食", "本帮江浙菜", "川菜", "粤菜", "湘菜","东北菜","台湾菜","新疆/清真","素菜","火锅","自助餐","小吃快餐","日本","韩国料理",
"东南亚菜","西餐","面包甜点","其他"},
new String[] {"全部休闲娱乐","咖啡厅","酒吧","茶馆","KTV","电影院","游乐游艺","公园","景点/郊游","洗浴","足浴按摩","文化艺术",
"DIY手工坊","桌球馆","桌面游戏","更多休闲娱乐"},
new String[] {"全部购物", "综合商场", "服饰鞋包", "运动户外","珠宝饰品","化妆品","数码家电","亲子购物","家居建材"
,"书店","书店","眼镜店","特色集市","更多购物场所","食品茶酒","超市/便利店","药店"},
new String[] {"全部休闲娱乐","咖啡厅","酒吧","茶馆","KTV","电影院","游乐游艺","公园","景点/郊游","洗浴","足浴按摩","文化艺术",
"DIY手工坊","桌球馆","桌面游戏","更多休闲娱乐"},
new String[] {"全","咖啡厅","酒吧","茶馆","KTV","游乐游艺","公园","景点/郊游","洗浴","足浴按摩","文化艺术",
"DIY手工坊","桌球馆","桌面游戏","更多休闲娱乐"},
new String[] {"全部","咖啡厅","酒吧","茶馆","电影院","游乐游艺","公园","景点/郊游","洗浴","足浴按摩","文化艺术",
"DIY手工坊","桌球馆","桌面游戏","更多休闲娱乐"},
new String[] {"全部休","咖啡厅","酒吧","茶馆","KTV","电影院","游乐游艺","公园","景点/郊游","洗浴","足浴按摩","文化艺术",
"DIY手工坊","桌球馆","桌面游戏","更多休闲娱乐"},
new String[] {"全部休闲","咖啡厅","酒吧","茶馆","KTV","电影院","游乐游艺","公园","景点/郊游","洗浴","足浴按摩","文化艺术",
"DIY手工坊","桌球馆","桌面游戏","更多休闲娱乐"},
new String[] {"全部休闲娱","咖啡厅","酒吧","茶馆","KTV","电影院","游乐游艺","公园","景点/郊游","洗浴","足浴按摩","文化艺术",
"DIY手工坊","桌球馆","桌面游戏"},
new String[] {"全部休闲娱乐","咖啡厅","酒吧","茶馆","KTV","电影院","游乐游艺","公园","景点/郊游","洗浴","足浴按摩","文化艺术",
"DIY手工坊","桌球馆","桌面游戏","更多休闲娱乐"},
new String[] {"全部休闲aaa","咖啡厅","酒吧","茶馆","KTV","电影院","游乐游艺","公园","景点/郊游","洗浴","足浴按摩","文化艺术",
"DIY手工坊","桌球馆","桌面游戏"},
};
String foods[] =new String []{"全部频道","美食","休闲娱乐","购物","酒店","丽人","运动健身","结婚","亲子","爱车","生活服务"};
int images[] = new int[]{R.drawable.ic_category_0,R.drawable.ic_category_10,R.drawable.ic_category_30,R.drawable.ic_category_20
,R.drawable.ic_category_60,R.drawable.ic_category_50,R.drawable.ic_category_45,R.drawable.ic_category_50,R.drawable.ic_category_70,
R.drawable.ic_category_65,R.drawable.ic_category_80};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
myAdapter=new MyAdapter(getApplicationContext(), foods, images);
listView.setAdapter(myAdapter);
selectDefult();
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
final int location=position;
myAdapter.setSelectedPosition(position);
myAdapter.notifyDataSetInvalidated();
subAdapter=new SubAdapter(getApplicationContext(), cities, position);
subListView.setAdapter(subAdapter);
subListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), cities[location][position], Toast.LENGTH_SHORT).show();
}
});
}
});
}
private void init(){
listView=(MyListView) findViewById(R.id.listView);
subListView=(MyListView) findViewById(R.id.subListView);
}
private void selectDefult(){
final int location=0;
myAdapter.setSelectedPosition(0);
myAdapter.notifyDataSetInvalidated();
subAdapter=new SubAdapter(getApplicationContext(), cities, 0);
subListView.setAdapter(subAdapter);
subListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), cities[location][position], Toast.LENGTH_SHORT).show();
}
});
}
}

默认我选中了第0个,下面看一下运行效果:

(0)

相关推荐

  • Android实现获取应用程序相关信息列表的方法

    本文所述为Androdi获取手机应用列表的方法,比如获取到Android应用的软件属性.大小和应用程序路径.应用名称等,获取所有已安装的Android应用列表,包括那些卸载了的,但没有清除数据的应用程序,同时在获取到应用信息的时候,判断是不是系统的应用程序,这是一个应用管理器所必需具备的功能. 具体实现代码如下: //AppInfoProvider.java package com.xh.ui; import java.util.ArrayList; import java.util.List;

  • Android ExpandableListView展开列表控件使用实例

    你是否觉得手机QQ上的好友列表那个控件非常棒? 不是..... 那也没关系,学多一点知识对自己也有益无害. 那么我们就开始吧. 展开型列表控件, 原名ExpandableListView 是普通的列表控件进阶版, 可以自由的把列表进行收缩, 非常的方便兼好看. 首先看看我完成的截图, 虽然界面不漂亮, 但大家可以自己去修改界面. 该控件需要一个主界面XML 一个标题界面XML及一个列表内容界面XML 首先我们来看看 mian.xml 主界面 复制代码 代码如下: //该界面非常简单, 只要一个E

  • Android用ListView显示SDCard文件列表的小例子

    复制代码 代码如下: filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ADASiteMaps/SigRecord";        File file=new File(filePath);        File[] files = file.listFiles(); 构造Adapter, 复制代码 代码如下: for(File mCurrentFile:files){       

  • android 支持的语言列表(汇总)

    Arabic, Egypt (ar_EG) -----------------------------阿拉伯语,埃及Arabic, Israel (ar_IL) -------------------------------阿拉伯语,以色列Bulgarian, Bulgaria (bg_BG) ---------------------保加利亚语,保加利亚Catalan, Spain (ca_ES) ---------------------------加泰隆语,西班牙Czech, Czech

  • ListView实现聊天列表之处理不同数据项

    通常我们用惯的ListView每一项的布局都是相同的,只是控件所绑定的数据不同.但单单只是如此并不能满足我们某些特殊需求,比如我们常见的QQ.微信的聊天列表,除了有左右之分外,内容更是有很大区别,有文字.语音.图片.视频等等,他们真的是ListView可以实现的吗?答案是肯定的,只要我们做一下类型区别即可. 实现效果如下所示: 大家不要在意布局,这里为了方便就随意了.大家可以看到,这里有两种布局,一种头像在左,一种头像在右,虽然这是一种简单的情况,但我们只需要了解其中的原理,再复杂的情况都可以迎

  • Android通过LIstView显示文件列表的两种方法介绍

    在Android中通过ListView显示SD卡中的文件列表一共有两种方法,一是:通过继承ListActivity显示;二是:利用BaseAdapter显示.BaseAdapter是一个公共基类适配器,用于对ListView和Spinner等 一些控件提供显示数据.下面是利用BaseAdapter类来实现通过LIstView显示SD卡的步骤: 1.main.xml界面设计,如下图 复制代码 代码如下: <?xml version="1.0" encoding="utf-

  • Android实现带列表的地图POI周边搜索功能

    先看效果图:(以公司附近的国贸为中心点) 上面是地图,下面是地理位置列表,有的只有地理位置列表(QQ动态的位置),这是个很常见的功能.它有个专门的叫法:POI周边搜索. 实现: 这个效果实现起来其实很简单,不过需要你先阅读下地图的API,这里使用的是高德地图的Android SDK,SDK的配置这里不作讲解,文末会放一些链接供学习. 思路: 1.利用地图的定位功能,获取用户当前的位置 2.根据获得的位置信息调用POI搜索,获取位置列表 3.ListView展示位置列表 4.用户拖动地图,获取地图

  • android开发教程之使用listview显示qq联系人列表

    首先还是xml布局文件,在其中添加ListView控件: 主布局layout_main.xml 复制代码 代码如下: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"

  • Android uses-permission权限列表中文注释版

    android同时也限定了系统资源的使用,像网络设备,SD卡,录音设备等.如果你的应用希望去使用任何系统资源,我们必须去申请Android的权限.这就是<uses-permission>元素的作用. 一个权限通常有以下格式,用一个名字为name 的字符串去指导我们希望使用的权限. 复制代码 代码如下: <uses-permission android:name="string"/> 例如:想要获得networking APIs的使用权限,我们指定如下的元素作为

  • Android实现三级联动下拉框 下拉列表spinner的实例代码

    主要实现办法:动态加载各级下拉值的适配器 在监听本级下拉框,当本级下拉框的选中值改变时,随之修改下级的适配器的绑定值              XML布局: 复制代码 代码如下: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_w

随机推荐