快速實現一個滑動顯示隱藏面板的ListView
基本用法:
在你的item布局文件中需要有ID為expandable_toggle_button的把手,和ID為expandable的面板容器
典型的像下面這樣:
如果你嫌上面的做法麻煩:
還有簡單的,使用ActionSlideExpandableListView控件,無需指定具體的把手ID和面板ID;
但是我通常不這樣做,因為畢竟使用的是ActionSlideExpandableListView,而不是普通的ListView,擴展性可能會受限制。
附件使用的是ActionSlideExpandableListView控件

基本用法:
listView = (ListView) view.findViewById(R.id.listView); protected void notifyDataSetChanged() { if (adapter == null) { adapter = new CommonAdapter<T>(context, beans, layoutId) { @Override public 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 { @Override public 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 it ActionSlideExpandableListView list = (ActionSlideExpandableListView)this.findViewById(R.id.list); // fill the list with data list.setAdapter(buildDummyData()); // listen for events in the two buttons for every list item. // the 'position' var will tell which list item is clicked list.setItemActionListener(new ActionSlideExpandableListView.OnActionClickListener() { @Override public 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 ); }
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
