歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android抽屜實現

Android抽屜實現

日期:2017/3/1 10:33:09   编辑:Linux編程
在手機上實現了抽屜效果,其實很簡單,但是效果卻很酷。

首先在layout 下設置xml布局文件

[html]
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:Android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent" >
  5. <SlidingDrawer
  6. android:id="@+id/sliding"
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent"
  9. android:content="@+id/allApps"
  10. android:handle="@+id/imageViewIcon"
  11. android:orientation="vertical" >
  12. <GridView
  13. android:id="@+id/allApps"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:background="@drawable/bk"
  17. android:columnWidth="60dp"
  18. android:gravity="center"
  19. android:horizontalSpacing="10dp"
  20. android:numColumns="auto_fit"
  21. android:padding="10dp"
  22. android:stretchMode="columnWidth"
  23. android:verticalSpacing="10dp" />
  24. <ImageView
  25. android:id="@+id/imageViewIcon"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:src="@drawable/touch_handler" />
  29. </SlidingDrawer>
  30. </RelativeLayout>
SlidingDrawer就是重要的抽屜控件 ,handle是抽屜的拖動按鈕,content是抽屜中的內容。

然後建立 chouti的activity類:

[html]
  1. import android.app.Activity;
  2. import android.content.Intent;
  3. import android.content.pm.ResolveInfo;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.BaseAdapter;
  8. import android.widget.GridView;
  9. import android.widget.ImageView;
  10. import android.widget.SlidingDrawer;
  11. public class Chouti extends Activity {
  12. private GridView gv;
  13. private SlidingDrawer sd;
  14. private ImageView iv;
  15. private List<ResolveInfo> apps;
  16. /** Called when the activity is first created. */
  17. @Override
  18. public void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.slidingdrawer);
  21. loadApps();
  22. gv = (GridView) findViewById(R.id.allApps);
  23. sd = (SlidingDrawer) findViewById(R.id.sliding);
  24. iv = (ImageView) findViewById(R.id.imageViewIcon);
  25. gv.setAdapter(new GridAdapter());
  26. sd.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener()// 開抽屜
  27. {
  28. @Override
  29. public void onDrawerOpened() {
  30. iv.setImageResource(R.drawable.touch_handler);// 響應開抽屜事件
  31. // ,把圖片設為向下的
  32. }
  33. });
  34. sd.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {
  35. @Override
  36. public void onDrawerClosed() {
  37. iv.setImageResource(R.drawable.touch_handler);// 響應關抽屜事件
  38. }
  39. });
  40. }
  41. private void loadApps() {
  42. Intent intent = new Intent(Intent.ACTION_MAIN, null);
  43. intent.addCategory(Intent.CATEGORY_LAUNCHER);
  44. apps = getPackageManager().queryIntentActivities(intent, 0);
  45. }
  46. public class GridAdapter extends BaseAdapter {
  47. public GridAdapter() {
  48. }
  49. public int getCount() {
  50. // TODO Auto-generated method stub
  51. return apps.size();
  52. }
  53. public Object getItem(int position) {
  54. // TODO Auto-generated method stub
  55. return apps.get(position);
  56. }
  57. public long getItemId(int position) {
  58. // TODO Auto-generated method stub
  59. return position;
  60. }
  61. public View getView(int position, View convertView, ViewGroup parent) {
  62. // TODO Auto-generated method stub
  63. ImageView imageView = null;
  64. if (convertView == null) {
  65. imageView = new ImageView(Chouti.this);
  66. imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
  67. imageView.setLayoutParams(new GridView.LayoutParams(50, 50));
  68. } else {
  69. imageView = (ImageView) convertView;
  70. }
  71. ResolveInfo ri = apps.get(position);
  72. imageView.setImageDrawable(ri.activityInfo
  73. .loadIcon(getPackageManager()));
  74. return imageView;
  75. }
  76. }
  77. }
loadApps方法是得到主界面上的圖片和文字。

然後設置的自定義adapter中去。

看下運行後效果:


向上滑動imageview按鈕後:



為了體現更好的效果,可以用兩張滑動圖片,一張朝上的,一張朝下的。根據監聽器做相應的切換。
Copyright © Linux教程網 All Rights Reserved