SlidingDrawer隐藏屏外的内容,并允许用户通过handle以显示隐藏内容。它可以垂直或水平滑动,它有俩个View组成,其一是可以拖动的handle,其二是隐藏内容的View.它里面的控件必须设置布局,在布局文件中必须指定handle和content.。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/text_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:gravity="center"
android:text="@string/hello"
android:textSize="10pt" />
<SlidingDrawer
android:id="@+id/sliding_drawer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:content="@+id/mycontent"
android:handle="@+id/layout1"
android:orientation="vertical" >
<LinearLayout
android:id="@id/layout1"
android:layout_width="fill_parent"
android:layout_height="35dp"
android:background="#00000000"
android:gravity="center" >
<ImageView
android:contentDescription="@string/handle"
android:id="@+id/my_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/up1" />
</LinearLayout>
<GridView
android:id="@id/mycontent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff000000"
android:gravity="center"
android:numColumns="3"
android:paddingTop="20dip" />
</SlidingDrawer>
</RelativeLayout>
Java部分:
package com.cn.daming;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SlidingDrawer;
import android.widget.TextView;
@SuppressWarnings("deprecation")
public class SlidingDrawerMainActivity extends Activity {
private GridView gridView;
private SlidingDrawer slidingDrawer;
private ImageView imageView;
private TextView textview;
private int[] icons={
R.drawable.title1, R.drawable.title2,
R.drawable.title3, R.drawable.title4,
R.drawable.title5, R.drawable.title6
};
private String[] items={
"Phone", "Message", "AddImage", "Music", "Telephone", "SMS"
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridView = (GridView)findViewById(R.id.mycontent);
slidingDrawer = (SlidingDrawer)findViewById(R.id.sliding_drawer);
imageView = (ImageView)findViewById(R.id.my_image);
textview = (TextView)findViewById(R.id.text_view);
MyGridViewAdapter adapter = new MyGridViewAdapter(this, items, icons);
gridView.setAdapter(adapter);
slidingDrawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() {
public void onDrawerOpened() {
textview.setVisibility(View.GONE);
imageView.setImageResource(R.drawable.down1);
}
});
slidingDrawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {
public void onDrawerClosed() {
textview.setVisibility(View.VISIBLE);
imageView.setImageResource(R.drawable.up1);
}
});
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
}
package com.cn.daming;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyGridViewAdapter extends BaseAdapter {
private Context context;
private String[] items;
private int[] icons;
public MyGridViewAdapter(Context context, String[] items, int[] icons) {
this.context = context;
this.items = items;
this.icons = icons;
}
public int getCount() {
return items.length;
}
public Object getItem(int arg0) {
return items[arg0];
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
View view = (View) layoutInflater.inflate(R.layout.grid, null);
ImageView imageView = (ImageView) view.findViewById(R.id.image_view);
TextView textview = (TextView) view.findViewById(R.id.text_view);
imageView.setImageResource(icons[position]);
textview.setText(items[position]);
return view;
}
}
运行效果:
本文参考:http://blog.csdn.net/wdaming1986/article/details/6898374
转载请标明出处:http://blog.csdn.net/tpxwantpxwan/article/details/38849683
源码下载:http://download.csdn.net/detail/tpxwantpxwan/7825479#comment