android之SlidingDrawer 抽屉效果

先看这张效果图吧
android之SlidingDrawer 抽屉效果

转载时请注明来自http://fuchangle.iteye.com/admin/blogs/1533167

先贴出main.xml的代码,代码中需要的图片自己解决
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
	<SlidingDrawer 
		android:id="@+id/slidingDrawer"
		android:layout_width="fill_parent"
		android:layout_height="fill_parent"
		android:orientation="vertical"
		android:handle="@+id/handle"
		android:content="@+id/content">
		<ImageButton 
			android:id="@+id/handle"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:src="@drawable/button_up"/>
			<LinearLayout 
				android:id="@+id/content"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"
				android:background="#ffffff">
				
				<TextView android:text="ceshi"
					android:id="@+id/textView"
					android:layout_width="wrap_content"
					android:layout_height="wrap_content"/>
			</LinearLayout>
	</SlidingDrawer>
</LinearLayout>



然后是java代码
package com.dapp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.SlidingDrawer;
import android.widget.TextView;

public class ChoutiActivity extends Activity {
	private SlidingDrawer mdrawer;
	private ImageButton mButton;
	private TextView mText;
	private boolean flag;
	private HashMap<String, Object> map;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mdrawer = (SlidingDrawer)findViewById(R.id.slidingDrawer);
        mButton = (ImageButton)findViewById(R.id.handle);
        mText = (TextView)findViewById(R.id.textView);
        drawerlistener();
        getdata();
    }
	
	private void getdata(){
		List<HashMap<String, Object>> listdata = new ArrayList<HashMap<String, Object>>();
		
			for(int i=0; i<10; i++){
				map = new HashMap<String, Object>();
				map.put("text", "ceshi");
				listdata.add(map);
			}
	}
	
	private void drawerlistener(){
		mdrawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() {
			
			@Override
			public void onDrawerOpened() {
				flag = true;
				mButton.setImageResource(R.drawable.button_down);
			}
		});
		
		mdrawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {
			
			@Override
			public void onDrawerClosed() {
				flag = false;
				mButton.setImageResource(R.drawable.button_up);
			}
		});
		
		mdrawer.setOnDrawerScrollListener(new SlidingDrawer.OnDrawerScrollListener() {
			
			@Override
			public void onScrollStarted() {
				mText.setText("结束拖动");
			}
			
			@Override
			public void onScrollEnded() {
				mText.setText("开始拖动");
			}
		});
	}
	
}

class DataAdapter extends BaseAdapter{
	private Context context;
	private List<HashMap<String, Object>> data;
	
	public DataAdapter(Context context,  List<HashMap<String, Object>> data) {
		super();
		this.context = context;
		this.data = data;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		return null;
	}
	
}

你可能感兴趣的:(android,slidingdrawer)