SlideExpandableListView滑动显示隐藏面板

系统 1564 0
快速实现一个滑动显示隐藏面板的ListView
SlideExpandableListView滑动显示隐藏面板


基本用法:
    
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
		);
	}

  

SlideExpandableListView滑动显示隐藏面板


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论