android二级listview列表实现代码

android二级listview列表实现代码,第1张

概述今天来实现以下大众点评客户端的横向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) || (!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,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[] {"全",
new String[] {"全部",
new String[] {"全部休",
new String[] {"全部休闲",
new String[] {"全部休闲娱","桌面游戏"},
new String[] {"全部休闲aaa",
};
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_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,
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(),0);
subListVIEw.setAdapter(subAdapter);
subListVIEw.setonItemClickListener(new OnItemClickListener() {
@OverrIDe
public voID onItemClick(AdapterVIEw<?> arg0,Toast.LENGTH_SHORT).show();
}
});
}
}

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

总结

以上是内存溢出为你收集整理的android二级listview列表实现代码全部内容,希望文章能够帮你解决android二级listview列表实现代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1142595.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存