MainActivity的事件
package com.example.slidingdrawer;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SlidingDrawer;
public class MainActivity extends Activity {
private GridView gridView;
private SlidingDrawer slidingDrawer;
private ImageView imageView;
private int[] icons={
R.drawable.widget01, R.drawable.widget02,
R.drawable.widget03, R.drawable.widget04,
R.drawable.widget05, R.drawable.widget06,
R.drawable.widget07, R.drawable.widget08,
R.drawable.widget09
};
String[] items = new String[]{"手机防盗","通讯卫视","软件管理","流量管理","进程管理","手机杀毒","缓存清理","高级工具","设置中心"};
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView)findViewById(R.id.mycontent);
slidingDrawer = (SlidingDrawer)findViewById(R.id.sliding_drawer);
imageView = (ImageView)findViewById(R.id.my_image);
MyGridViewAdapter adapter = new MyGridViewAdapter(this, items, icons);
gridView.setAdapter(adapter);
slidingDrawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() {
public void onDrawerOpened() {
imageView.setImageResource(R.drawable.right1);
}
});
slidingDrawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {
public void onDrawerClosed() {
imageView.setImageResource(R.drawable.left1);
}
});
}
}
在创建一个MyGridViewAdapter
package com.example.slidingdrawer;
import android.annotation.SuppressLint;
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;
}
@SuppressLint({ "ViewHolder"})
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;
}
}
在activity_main.xml 中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="抽屉效果"
android:gravity="center"
android:textSize="20sp"
/>
<SlidingDrawer
android:id="@+id/sliding_drawer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:handle="@+id/layout1"
android:content="@+id/mycontent"
android:orientation="horizontal"
>
<LinearLayout
android:id="@id/layout1"
android:layout_width="35px"
android:layout_height="fill_parent"
android:gravity="center"
android:background="#00000000"
>
<ImageView
android:id="@+id/my_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/left1"
/>
</LinearLayout>
<GridView
android:id="@id/mycontent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="20dip"
android:numColumns="3"
android:gravity="center"
android:background="#ff000000"
/>
</SlidingDrawer>
</RelativeLayout>
在创建一个grid.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<ImageView
android:id="@+id/image_view"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginBottom="5dp"
/>
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginLeft="27dp"
/>
</LinearLayout>
样式