SlideExpandableListView滑动显示隐藏面板

来源:互联网 发布:云镜安卓限制安装软件 编辑:程序博客网 时间:2024/05/01 02:21
快速实现一个滑动显示隐藏面板的ListView



基本用法:
listView = (ListView) view.findViewById(R.id.listView);protected void notifyDataSetChanged() {if (adapter == null) {adapter = new CommonAdapter<T>(context, beans, layoutId) {@Overridepublic void setValues(ViewHolder helper, T item, int position) {createItem(helper, item, position);}};listView.setAdapter(new SlideExpandableListAdapter(adapter,R.id.expandable_toggle_button, R.id.expandable));} else {adapter.notifyDataSetChanged();}}


在你的item布局文件中需要有ID为expandable_toggle_button的把手,和ID为expandable的面板容器
典型的像下面这样:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:orientation="vertical" >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:paddingLeft="8dp"        android:paddingRight="8dp"        android:orientation="vertical" >        <TextView            android:id="@+id/item_0"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center_vertical|start"            android:gravity="center"            android:singleLine="true"            android:text="订单编号"            android:textColor="@color/base_black"            android:textSize="@dimen/font_middle" />        <TextView            android:id="@+id/item_1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center_vertical|end"            android:gravity="center"            android:paddingBottom="8dp"            android:paddingTop="8dp"            android:singleLine="true"            android:text="进场时间"            android:textColor="@color/base_black"            android:textSize="@dimen/font_middle" />    </LinearLayout>    <FrameLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:paddingLeft="8dp"        android:paddingRight="8dp"        android:orientation="horizontal" >        <TextView            android:id="@+id/item_2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center_vertical|start"            android:gravity="center"            android:singleLine="true"            android:text="停车场名称"            android:textColor="@color/base_black"            android:textSize="@dimen/font_middle" />        <ImageView            android:id="@+id/expandable_toggle_button"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center_vertical|end"            android:layout_marginRight="16dp"            android:src="@drawable/bg_btn_more" />    </FrameLayout>    <LinearLayout        android:id="@+id/expandable"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="@color/base_gray"        android:orientation="horizontal"         >        <TextView            android:id="@+id/btn_0"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_weight="1"            android:drawableTop="@drawable/bg_btn_0"            android:gravity="center"            android:singleLine="true"            android:text="取消订单"            android:textColor="@android:color/white"            android:textSize="@dimen/font_middle" />                <TextView            android:id="@+id/btn_1"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_weight="1"            android:drawableTop="@drawable/bg_btn_0"            android:gravity="center"            android:singleLine="true"            android:text="联系对方"            android:textColor="@android:color/white"            android:textSize="@dimen/font_middle" />        <TextView            android:id="@+id/btn_2"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_weight="1"            android:drawableTop="@drawable/bg_btn_0"            android:gravity="center"            android:singleLine="true"            android:text="退订"            android:textColor="@android:color/white"            android:textSize="@dimen/font_middle" />        <TextView            android:id="@+id/btn_3"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_weight="1"            android:drawableTop="@drawable/bg_btn_0"            android:gravity="center"            android:singleLine="true"            android:text="进场"            android:textColor="@android:color/white"            android:textSize="@dimen/font_middle" />    </LinearLayout></LinearLayout>



如果你嫌上面的做法麻烦:
还有简单的,使用ActionSlideExpandableListView控件,无需指定具体的把手ID和面板ID;
但是我通常不这样做,因为毕竟使用的是ActionSlideExpandableListView,而不是普通的ListView,扩展性可能会受限制。
附件使用的是ActionSlideExpandableListView控件
public class MainActivity extends Activity {@Overridepublic void onCreate(Bundle savedData) {super.onCreate(savedData);// set the content view for this activity, check the content view xml file// to see how it refers to the ActionSlideExpandableListView view.this.setContentView(R.layout.single_expandable_list);// get a reference to the listview, needed in order// to call setItemActionListener on itActionSlideExpandableListView list = (ActionSlideExpandableListView)this.findViewById(R.id.list);// fill the list with datalist.setAdapter(buildDummyData());// listen for events in the two buttons for every list item.// the 'position' var will tell which list item is clickedlist.setItemActionListener(new ActionSlideExpandableListView.OnActionClickListener() {@Overridepublic void onClick(View listView, View buttonview, int position) {/** * Normally you would put a switch * statement here, and depending on * view.getId() you would perform a * different action. */String actionName = "";if(buttonview.getId()==R.id.buttonA) {actionName = "buttonA";} else {actionName = "ButtonB";}/** * For testing sake we just show a toast */Toast.makeText(MainActivity.this,"Clicked Action: "+actionName+" in list item "+position,Toast.LENGTH_SHORT).show();}// note that we also add 1 or more ids to the setItemActionListener// this is needed in order for the listview to discover the buttons}, R.id.buttonA, R.id.buttonB);}/** * Builds dummy data for the test. * In a real app this would be an adapter * for your data. For example a CursorAdapter */public ListAdapter buildDummyData() {final int SIZE = 40;String[] values = new String[SIZE];for(int i=0;i<SIZE;i++) {values[i] = "Item "+i;}return new ArrayAdapter<String>(this,R.layout.expandable_list_item,R.id.text,values);}



ExpandableLayout可扩展布局,各种动画效果
http://www.jcodecraeer.com/a/opensource/2015/0909/3431.html