I. Overview
ListView Click Item to display the menu to achieve this effect:
The logic to be implemented is as follows:
1 Click on a normal item, expand the current menu, and close other menus
2 Click on an expanded menu to hide the current menu
3 will expand the menu to slide to ListView, and then slide back, expand the menu status unchanged
4 Click on the button in the menu, can be different according to different item processing
Second, the realization of ideas
1, the UI layout, for this each ListItem contains dynamic display menu scene, you can directly in the ListItem XML layout contains two parts: item itself and expand the menu
Click on the item, dynamic control to expand the menu part of the elements of the visibility on it
2, logic control, the need for additional records of the current expanded menu of who is the item, so that you can easily and efficiently implement the logic described in the overview
Three, get to work.
The implementation of the code structure is not complex, where the code is directly attached:
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=" prompts "/> <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= "closed"/> </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!=; ++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<intege
R> 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; The 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) {RET
Urn position; @Override Public View getview (final int position, View Convertview, ViewGroup parent) {if (Convertview = null) {Conve
Rtview = 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)
));
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 ("hint" + position);
Holder.btnCollapse.setText ("Close up" + 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 i
D = V.getid (); if (id = = r.id.listview_menu_item_menu_toast) {toast.maketext (listviewmenuactivity.this, "hint" + 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.notifydataset
Changed (); }
}
}
Four, demo video
[Reprint please keep this article address: http://www.cnblogs.com/snser/p/5539746.html
Five, demo project
Save the image below and change the extension to. zip
The above is a small series to introduce the ListView click on the item to expand the menu to achieve the relevant knowledge of code, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!