继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

抽屉 式菜单 ,点击菜单中的第一个会转到画廊用到SlidingMenuImageSwitcher

哈哈哈_3
关注TA
已关注
手记 14
粉丝 4
获赞 412

项目名称为Am222

因为初学者,正在学习,所以从网上找资料,然后更改等,只是简单的分享而已。参考网站:http://blog.csdn.net/yangyu20121224/article/details/9258275 和http://blog.chinaunix.net/uid-25799257-id-3971442.html;
ImageSwitcherDemo的类:
package com.example.am222;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

public class ImageSwitcherDemo extends Activity implements
OnItemSelectedListener, ViewFactory {
private ImageSwitcher is;
private Gallery gallery;

private Integer[] mThumbIds = { R.drawable.aa, R.drawable.ab,
        R.drawable.ac, R.drawable.ad, R.drawable.ae,
        R.drawable.af, R.drawable.ag,
        R.drawable.ah, R.drawable.as, R.drawable.aw,
        };

private Integer[] mImageIds = { R.drawable.aa, R.drawable.ab,
        R.drawable.ac, R.drawable.ad, R.drawable.ae,
        R.drawable.af, R.drawable.ag,
        R.drawable.ah, R.drawable.as, R.drawable.aw, };

/*
 * (non-Javadoc)
 * 
 * @see android.app.Activity#onCreate(android.os.Bundle)
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.imageswitcherpage);

    is = (ImageSwitcher) findViewById(R.id.switcher);
    is.setFactory(this);

    is.setInAnimation(AnimationUtils.loadAnimation(this,
            android.R.anim.fade_in));
    is.setOutAnimation(AnimationUtils.loadAnimation(this,
            android.R.anim.fade_out));

    gallery = (Gallery) findViewById(R.id.gallery);

    gallery.setAdapter(new ImageAdapter(this));
    gallery.setOnItemSelectedListener(this);
}

@Override
public View makeView() {
    ImageView i = new ImageView(this);
    i.setBackgroundColor(0xFF000000);
    i.setScaleType(ImageView.ScaleType.FIT_CENTER);
    i.setLayoutParams(new ImageSwitcher.LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    return i;
}

public class ImageAdapter extends BaseAdapter {
    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(mContext);

        i.setImageResource(mThumbIds[position]);
        i.setAdjustViewBounds(true);
        i.setLayoutParams(new Gallery.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        i.setBackgroundResource(R.drawable.e);
        return i;
    }

    private Context mContext;

}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
        long id) {
    is.setImageResource(mImageIds[position]);

}

@Override
public void onNothingSelected(AdapterView<?> parent) {
    // TODO Auto-generated method stub

}

}

SampleListFragment类:这个类要先下载SlidingMenu的一个库,网上有的,可以下载;
package com.example.am222;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class SampleListFragment extends ListFragment {

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.list, null);
}

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    SampleAdapter adapter = new SampleAdapter(getActivity());
    for (int i = 0; i < 20; i++) {
        adapter.add(new SampleItem("Sample List", android.R.drawable.ic_menu_search));
        //adapter.add(new SampleItem("十一", R.drawable.ic_launcher));
    }
    setListAdapter(adapter);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);

    switch (position) {
    case 0:
        Toast.makeText(getActivity(), "点击了第0个", Toast.LENGTH_SHORT).show();
        Intent intent=new Intent();
        intent.setClass(getActivity(), ImageSwitcherDemo.class);
        //getActivity().finish();
        startActivity(intent);

        break;

    case 1:
        Toast.makeText(getActivity(), "点击了第1个", Toast.LENGTH_SHORT).show();

        break;

    case 2:
        Toast.makeText(getActivity(), "点击了第2个", Toast.LENGTH_SHORT).show();

        break;

    case 3:
        Toast.makeText(getActivity(), "点击了第3个", Toast.LENGTH_SHORT).show();

        break;

    default:
        break;
    }
}

private class SampleItem {
    public String tag;
    public int iconRes;
    public SampleItem(String tag, int iconRes) {
        this.tag = tag; 
        this.iconRes = iconRes;
    }
}

public class SampleAdapter extends ArrayAdapter<SampleItem> {

    public SampleAdapter(Context context) {
        super(context, 0);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.row, null);
        }
        ImageView icon = (ImageView) convertView.findViewById(R.id.row_icon);
        icon.setImageResource(getItem(position).iconRes);
        TextView title = (TextView) convertView.findViewById(R.id.row_title);
        title.setText(getItem(position).tag);

        return convertView;
    }

}

}

然后就是主函数 MainActivity:
package com.example.am222;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.MenuItem;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;

public class MainActivity extends SlidingFragmentActivity{

@SuppressLint("NewApi") @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // 设置标题
    setTitle("Left and Right"); 

    // 初始化滑动菜单
    initSlidingMenu(savedInstanceState);
/*  

    setHomeButtonEnabled这个小于4.0版本的默认值为true的。
    但是在4.0及其以上是false,该方法的作用:决定左上角的图标是否可以点击。
    没有向左的小图标。 true 图标可以点击  false 不可以点击。
    actionBar.setDisplayHomeAsUpEnabled(true)   
     // 给左上角图标的左边加上一个返回的图标 。对应ActionBar.DISPLAY_HOME_AS_UP
    actionBar.setDisplayShowHomeEnabled(true)   
    //使左上角图标是否显示,如果设成false,则没有程序图标,仅仅就个标题,否则,显示应用程序图标,
     * 对应id为Android.R.id.home,对应ActionBar.DISPLAY_SHOW_HOME
    actionBar.setDisplayShowCustomEnabled(true)  
    // 使自定义的普通View能在title栏显示,即actionBar.setCustomView能起作用,
     * 对应ActionBar.DISPLAY_SHOW_CUSTOM
    actionBar.setDisplayShowTitleEnabled(true)   
    //对应ActionBar.DISPLAY_SHOW_TITLE。
    其中setHomeButtonEnabled和setDisplayShowHomeEnabled共同起作用,
    如果setHomeButtonEnabled设成false,即使setDisplayShowHomeEnabled设成true,
    图标也不能点击

    */
    getActionBar().setDisplayHomeAsUpEnabled(true);
}

private void initSlidingMenu(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    // 设置滑动菜单的属性值
            getSlidingMenu().setMode(SlidingMenu.LEFT_RIGHT);   //这里设置菜单出现在左边还是右边或者两边的都有
            getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);//设置触摸全屏左右滑动时出现菜单还是滑动菜单的边缘时出现菜单和关闭菜单
            getSlidingMenu().setShadowWidthRes(R.dimen.shadow_width);   //设置菜单的影子
            getSlidingMenu().setShadowDrawable(R.drawable.shadow);//设置菜单的影子的图片
            getSlidingMenu().setBehindOffsetRes(R.dimen.slidingmenu_offset);//设置菜单与屏幕右边的距离
            getSlidingMenu().setFadeDegree(0.35f);  //设置淡化度

            // 设置主界面的视图
            setContentView(R.layout.content_frame);
            //getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, new SampleListFragment()).commit();        

            // 设置滑动菜单的左视图界面
            setBehindContentView(R.layout.menu_frame);
            getSupportFragmentManager().beginTransaction().replace(R.id.menu_frame, new SampleListFragment()).commit();

            // 设置滑动菜单的右视图界面
            getSlidingMenu().setSecondaryMenu(R.layout.menu_frame_two);//设置布局资源中的“右”菜单内容。资源将被夸大,增加了所有的顶层视图到后面看。

            getSlidingMenu().setSecondaryShadowDrawable(R.drawable.shadowright);
            getSupportFragmentManager().beginTransaction().replace(R.id.menu_frame_two, new SampleListFragment1()).commit();
}
/**
 * 菜单按钮点击事件,通过点击ActionBar的Home图标按钮来打开滑动菜单
 */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        toggle();
        return true;    
    }       
    return super.onOptionsItemSelected(item);
}

}

在这里用到了很多XML文件

content_frame.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />

imageswicherpage.xml:
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageSwitcher android:id="@+id/switcher"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
/>

<Gallery android:id="@+id/gallery"
    android:background="#55000000"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"

    android:gravity="center_vertical"
    android:spacing="16dp"
/>

</RelativeLayout>

list.xml:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/list_padding"
android:paddingRight="@dimen/list_padding" />

menu_frame.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/menu_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />

menu_frame_two.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/menu_frame_two"
android:layout_width="match_parent"
android:layout_height="match_parent" />

row.xml:
<?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="50dp"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/row_icon"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:padding="10dp"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/row_title"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:gravity="center_vertical"
    android:padding="10dp"
    android:text="Medium Text"
    android:textAppearance="@android:style/TextAppearance.Medium" />

</LinearLayout>

在values里面,dimens.xml:
<!--
Copyright 2011 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<resources>

<dimen name="slidingmenu_offset">60dp</dimen>
<dimen name="list_padding">10dp</dimen>
<dimen name="shadow_width">15dp</dimen>

<integer name="num_cols">1</integer>

</resources>

打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP