Android上实现各种风格的隐藏菜单,比如左右滑动菜单、上下滑动显示隐藏菜单

Android上的菜单展示风格目前是各式各样的,但用的最多且体验最好的莫过于左右滑动来显示隐藏的菜单
本示例实现了各种方式的菜单展示效果,只有你想不到的
源码:https://github.com/SimonVT/android-menudrawer.git

main.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="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <Button
        android:id="@+id/windowSample"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Window sample" />

    <Button
        android:id="@+id/overlayWindowSample"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Action Bar overlay window sample" />

    <Button
        android:id="@+id/contentSample"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Content sample" />

    <Button
        android:id="@+id/listActivitySample"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ListActivity sample" />

</LinearLayout>

SimplesActivity.java

package net.simonvt.menudrawer.samples;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

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

public class SamplesActivity extends ListActivity {

    private SamplesAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAdapter = new SamplesAdapter();

        mAdapter.addSample("Content sample", "Only the content area is dragged.", ContentSample.class);
        mAdapter.addSample("ListActivity sample", "Shows how to use the drawer with a ListActivity.",
                ListActivitySample.class);
        mAdapter.addSample("Window sample", "The entire window is dragged.", WindowSample.class);
        mAdapter.addSample("ActionBar overlay sample", "A window sample, where the ActionBar is an overlay",
                ActionBarOverlaySample.class);
        mAdapter.addSample("Right menu", "The menu is positioned to the right of the content", RightMenuSample.class);
        mAdapter.addSample("Top menu", "The menu is positioned above the content", TopMenuSample.class);
        mAdapter.addSample("Bottom menu", "The menu is positioned below the content", BottomMenuSample.class);
        mAdapter.addSample("Touch Mode", "The menu touch behavior change according to different"
                + " content view state (Ex: View Pager)", ViewPagerSample.class);
        mAdapter.addSample("Layout xml", "The drawer and its menu and content is defined in XML", LayoutSample.class);
        mAdapter.addSample("Static drawer", "The drawer is always visible", StaticDrawerSample.class);

        setListAdapter(mAdapter);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        SampleItem sample = (SampleItem) mAdapter.getItem(position);
        Intent i = new Intent(this, sample.mClazz);
        startActivity(i);
    }

    private static class SampleItem {

        String mTitle;
        String mSummary;
        Class mClazz;

        public SampleItem(String title, String summary, Class clazz) {
            mTitle = title;
            mSummary = summary;
            mClazz = clazz;
        }
    }

    public class SamplesAdapter extends BaseAdapter {

        private List<SampleItem> mSamples = new ArrayList<SampleItem>();

        public void addSample(String title, String summary, Class clazz) {
            mSamples.add(new SampleItem(title, summary, clazz));
        }

        @Override
        public int getCount() {
            return mSamples.size();
        }

        @Override
        public Object getItem(int position) {
            return mSamples.get(position);
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            SampleItem sample = (SampleItem) getItem(position);

            View v = convertView;
            if (v == null) {
                v = getLayoutInflater().inflate(R.layout.list_row_sample, parent, false);
            }

            ((TextView) v.findViewById(R.id.title)).setText(sample.mTitle);
            ((TextView) v.findViewById(R.id.summary)).setText(sample.mSummary);

            return v;
        }
    }
}

转:http://code.eoe.cn/215.html

 

posted @ 2013-07-10 17:09  还是你最好  阅读(24793)  评论(0编辑  收藏  举报