当前位置:  编程技术>移动开发

android二级listview列表实现代码

    来源: 互联网  发布时间:2014-10-12

    本文导语:  今天来实现以下大众点评客户端的横向listview二级列表,先看一下样式。  这种横向的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)) {
scrollTo(0, 0);
mDistance = 0;
mLastDownY = 0f;
return;
}
mStep += 1;
this.postDelayed(this, 10);
}
}

然后看一下xml的布局:
代码如下:










两个自定义的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布局:
代码如下:








然后是子适配器代码:
代码如下:

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布局:
代码如下:






最后看下主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 Horizontal ListView
  • 申请Android Map 的API Key(v2)的最新申请方式(SHA1密钥) iis7站长之家
  • android ListView自动滚动方法
  • android listview 水平滚动和垂直滚动的小例子
  • 滑动刷新的ListView Android PullToRefresh
  • android中如果为listview的item添加listener,也能为item中的button添加listener
  • android中ListView多次刷新重复执行getView的解决方法
  • Android笔记之:在ScrollView中嵌套ListView的方法
  • android ListView 一些重要属性详解
  • Android用ListView显示SDCard文件列表的小例子
  • Android入门之ListView应用解析(一)
  • android表格效果之ListView隔行变色实现代码
  • android基础教程之android的listview与edittext冲突解决方法
  • Android ListView 扩展 DragSortListView
  • android开发之横向滚动/竖向滚动的ListView(固定列头)
  • Android入门之ListView应用解析(二)
  • Android之ScrollView嵌套ListView和GridView冲突的解决方法
  • android开发教程之listview显示sqlite数据
  • android开发教程之listview使用方法
  • Android在listview添加checkbox实现原理与代码
  • android 自定义Android菜单背景的代码
  • android panellistview 圆角实现代码
  • android 简单图片动画播放的实例代码
  • android网络编程之android连接网络的简单示例代码
  • 用ubuntu下载的android源代码不知下到哪去了
  • Android发送短信功能代码
  • Android Bitmap和Drawable相互转换的简单代码
  • android WakeLock使用方法代码实例
  • android自动安装apk代码实例(不使用apk安装器安装)
  • 控制Android LED灯颜色的代码实例
  • android通过bitmap生成新图片关键性代码
  •  
    本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
    本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • 申请Android Map 的API Key(v2)的最新申请方式(SHA1密钥)
  • Android瀑布流实例 android_waterfall
  • Android开发需要的几点注意事项总结
  • Android系统自带样式 (android:theme)
  • android 4.0 托管进程介绍及优先级和回收机制
  • Android网络共享软件 Android Wifi Tether
  • Android访问与手机通讯相关类的介绍
  • Android 图标库 Android GraphView
  • Android及andriod无线网络Wifi开发的几点注意事项
  • 轻量级Android开发工具 Android Tools
  • Android 2.3 下StrictMode介绍
  • Android 开发环境 Android Studio
  • IDEA的Android开发插件 idea-android
  • Android手机事件提醒 Android Notifier
  • XBMC的Android客户端 android-xbmcremote
  • Android小游戏 Android Shapes
  • Android电池监控 Android Battery Dog
  • android开发:“android:WindowTitle”没有对应项no resource
  • Android 上类似IOS 的开关控件。 Android ToggleButton
  • Android 将 android view 的位置设为右下角的解决方法
  • Android 2D游戏引擎 Android Angle


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3