欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

ListView点击Item展开菜单实现代码详解

程序员文章站 2024-03-04 20:57:48
一、概述 listview点击item显示菜单是要实现这样的效果: 需要实现的逻辑如下: 1)点击一个普通item,展开当前菜单,同时关闭其他菜单...

一、概述

listview点击item显示菜单是要实现这样的效果:

ListView点击Item展开菜单实现代码详解

需要实现的逻辑如下:

1)点击一个普通item,展开当前菜单,同时关闭其他菜单

2)点击一个已展开的菜单,隐藏当前菜单

3)将展开菜单滑到listview之外,再滑动回来,展开菜单状态不变

4)点击菜单中的按钮,能够根据不同item进行不同的处理

二、实现思路

1、ui布局上,对于这种每个listitem都包含动态显示菜单的场景,可以直接在listitem的xml布局里就包含两部分元素:item本身以及展开菜单

点击item的时候,动态控制展开菜单这部分元素的visibility就可以了

2、逻辑控制上,需要额外记录当前展开菜单的item是谁,这样可以方便高效的实现概述中描述的逻辑

三、开始干活

实现的代码结构不复杂,这里直接贴代码了:

listview_menu.xml:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eeeeee"
android:orientation="vertical"
tools:context="${relativepackage}.${activityclass}" >
<listview
android:id="@+id/listview_menu_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null" />
</linearlayout>

listview_menu_item.xml:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/background_light"
android:orientation="vertical"
android:descendantfocusability="blocksdescendants" >
<textview
android:id="@+id/listview_menu_item_txt"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textstyle="bold"
android:textsize="20sp"
android:textcolor="@android:color/black"
android:text="123" />
<linearlayout
android:id="@+id/listview_menu_item_menu"
android:layout_width="match_parent"
android:layout_height="50dp"
android:visibility="visible"
android:orientation="horizontal" >
<button
android:id="@+id/listview_menu_item_menu_toast"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#8080ff"
android:textcolor="@android:color/black"
android:textsize="15sp"
android:text="提示" />
<button
android:id="@+id/listview_menu_item_menu_collapse"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#80ff80"
android:textcolor="@android:color/black"
android:textsize="15sp"
android:text="收起" />
</linearlayout>
</linearlayout>

listviewmenuactivity.java:

public class listviewmenuactivity extends activity {
private int mexpandedmenupos = -1;
private listviewadapter madapter;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.listview_menu);
arraylist<integer> data = new arraylist<integer>();
for (int index = 0; index != 40; ++index) {
data.add(index);
}
listview list = (listview)findviewbyid(r.id.listview_menu_list);
list.setadapter(madapter = new listviewadapter(this, data));
list.setonitemclicklistener(new onlistitemclicklistenser());
}
private class listviewadapter extends baseadapter {
private layoutinflater mlayoutinflater;
private arraylist<integer> mlistdata;
private onmenuclicklistenser monmenuclicklistenser = new onmenuclicklistenser();
private class viewholder {
public viewholder (view viewroot) {
root = viewroot;
txt = (textview)viewroot.findviewbyid(r.id.listview_menu_item_txt);
menu = viewroot.findviewbyid(r.id.listview_menu_item_menu);
btntoast = (button)viewroot.findviewbyid(r.id.listview_menu_item_menu_toast);
btncollapse = (button)viewroot.findviewbyid(r.id.listview_menu_item_menu_collapse);
}
public view root;
public textview txt;
public view menu;
public button btntoast;
public button btncollapse;
}
public listviewadapter(context context, arraylist<integer> data) {
mlayoutinflater = layoutinflater.from(context);
mlistdata = data;
}
@override
public int getcount() {
return mlistdata == null ? 0 : mlistdata.size();
}
@override
public object getitem(int position) {
return mlistdata == null ? 0 : mlistdata.get(position);
}
@override
public long getitemid(int position) {
return position;
}
@override
public view getview(final int position, view convertview, viewgroup parent) {
if (convertview == null) {
convertview = mlayoutinflater.inflate(r.layout.listview_menu_item, parent, false);
convertview.settag(new viewholder(convertview));
}
if (convertview != null && convertview.gettag() instanceof viewholder) {
final viewholder holder = (viewholder)convertview.gettag();
holder.txt.settext(string.valueof(getitem(position)));
if (position % 2 == 0) {
holder.root.setbackgroundcolor(0xffc9eefe);
} else {
holder.root.setbackgroundcolor(0xffffffff);
}
holder.menu.setvisibility(position == mexpandedmenupos ? view.visible : view.gone);
holder.btntoast.settext("提示" + position);
holder.btncollapse.settext("收起" + position);
holder.btntoast.setonclicklistener(monmenuclicklistenser);
holder.btncollapse.setonclicklistener(monmenuclicklistenser);
}
return convertview;
}
private class onmenuclicklistenser implements view.onclicklistener {
@override
public void onclick(view v) {
final int id = v.getid();
if (id == r.id.listview_menu_item_menu_toast) {
toast.maketext(listviewmenuactivity.this, "提示" + mexpandedmenupos, toast.length_short).show();
} else if (id == r.id.listview_menu_item_menu_collapse) {
mexpandedmenupos = -1;
notifydatasetchanged();
}
}
}
}
private class onlistitemclicklistenser implements onitemclicklistener {
@override
public void onitemclick(adapterview<?> parent, view view, int position, long id) {
if (position == mexpandedmenupos) {
mexpandedmenupos = -1;
} else {
mexpandedmenupos = position;
}
madapter.notifydatasetchanged();
}
}
}

四、demo视频

ListView点击Item展开菜单实现代码详解

[转载请保留本文地址:

五、demo工程

保存下面的图片,扩展名改成 .zip 即可

ListView点击Item展开菜单实现代码详解

以上所述是小编给大家介绍的listview点击item展开菜单实现代码详解的相关知识,希望对大家有所帮助