【Android 界面效果33】二级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个,下面看一下运行效果:
代码下载地址:点击打开链接
【Android 界面效果33】二级listview列表的更多相关文章
- 【Android 界面效果26】listview android:cacheColorHint,android:listSelector属性作用
ListView是常用的显示控件,默认背景是和系统窗口一样的透明色,如果给ListView加上背景图片,或者背景颜色时,滚动时listView会黑掉, 原因是,滚动时,列表里面的view重绘时,用的依 ...
- android二级listview列表
今天来实现以下大众点评客户端的横向listview二级列表,先看一下样式. 这种横向的listview二级列表在手机软件上还不太常见,但是使用过平板的都应该知道,在平板上市比较常见的.可能是因为平板屏 ...
- 我的Android进阶之旅------>Android二级ListView列表的实现
实现如下图所示的二级列表效果 首先是在布局文件中,布局两个ListView,代码如下: <LinearLayout xmlns:android="http://schemas.andr ...
- 【Android 界面效果21】Android ViewPager使用详解
这是谷歌官方给我们提供的一个兼容低版本安卓设备的软件包,里面包囊了只有在安卓3.0以上可以使用的api.而viewpager就是其中之一利用它,我们可以做很多事情,从最简单的导航,到页面菜单等等.那如 ...
- 【Android 界面效果17】Android手机平板两不误,使用Fragment实现兼容手机和平板的程序
记得我之前参与开发过一个华为的项目,要求程序可以支持好几种终端设备,其中就包括Android手机和Android Pad.然后为了节省人力,公司无节操地让Android手机和Android Pad都由 ...
- 【Android 界面效果47】RecyclerView详解
RecylerView作为 support-library发布出来,这对开发者来说绝对是个好消息.因为可以在更低的Android版本上使用这个新视图.下面我们看如何获取 RecylerView.首先打 ...
- 【Android 界面效果31】Android--侧滑菜单应用的实现
侧滑菜单应用现在非常多,而且实现方式也多种多样.通过在网上的多方查找,我找到郭霖少侠的这篇文章:http://blog.csdn.net/guolin_blog/article/details/874 ...
- 【Android 界面效果25】android中include标签的使用
在一个项目中我们可能会需要用到相同的布局设计,如果都写在一个xml文件中,代码显得很冗余,并且可读性也很差,所以我们可以把相同布局的代码单独写成一个模块,然后用到的时候可以通过<include ...
- 【Android 界面效果18】Android软件开发之常用系统控件界面整理
[java] view plaincopyprint? <span style="font-size:18px">1.文本框TextView TextView的作用 ...
随机推荐
- DSp寄存器“是怎么和板子上的”具体地址“一一对应起来的
转自:http://hzcjustfly.blog.163.com/blog/static/18319712920117191123928/ 最近在学习DSP,今天在开发板TMS320F2808学习例 ...
- 【全面完美方案】iPhone 4S WiFi变灰 DIY修复方式
这是我在一位台湾网友usaretama发表的一篇帖子中看到的,原帖我发表在维维网 如果你有WiFi开关变灰不能切换.WiFi遇到搜不到AP或搜到了连不上,那您就要注意这篇了. 家人的 iPhone 4 ...
- 技术文集:万能WINDOWS XP封装
这里将系统封装分为3步:做系统.封装.部署 一.做系统 平台不限,但不建议在虚拟机上制作.CPU及主板芯片没有限制,关于intelide和intelppm的不兼容问题,深度白金3in1并没有删除这些注 ...
- ML 徒手系列 拉格朗日乘子法
拉格朗日乘子法是解决极值问题的方法. 本方法是计算多元函数在约束条件下的极值问题的方法. 1.多元函数与约束问题 如下图所示,f(x,y)为多元函数,g(x,y)=c为约束条件.目的是计算在约束条件下 ...
- Android PHP 通过JSON进行数据交互
一.首先是Android客户端解析PHP返回的JSON数据 1.PHP代码(这里用到了数据库,如果没有准备数据库的话,可以自定义字符串) <?php $link=mysql_connect(SA ...
- Lotus 迁移到Exchange POC 之 新建2007 服务器!
我们登录到Exchange 2007 服务器,由于需要对AD进行扩展,我们首先必须完成架构扩展,由于默认没有ldifde工具,所以我们需要执行servermanagercmd –I rsat-adds ...
- redis的发布订阅模式
概要 redis的每个server实例都维护着一个保存服务器状态的redisServer结构 struct redisServer { /* Pubsub */ // 字典,键为频道, ...
- [前端引用] 利用ajax实现类似php include require 等命令的功能
利用ajax实现类似php中的include.require等命令的功能 最新文件下载: https://github.com/myfancy/ajaxInclude 建议去这里阅读readme-2. ...
- android wifi总结
大致可以分为四个主要的类ScanResult wifiConfiguration WifiInfo WifiManager (1)ScanResult,主要是通过wifi 硬件的扫描来获取一些周边的w ...
- SAE J1708 DS36277 MAX3444, DS75176B
http://en.wikipedia.org/wiki/J1708 J1708 SAE J1708 is a standard used for serial communications betw ...