Android之drawlayout使用和总结

DrawLayout

                     今天发的Android之SlidingMenu博客之后,有人说out了,说用DrawLayout,我插,被叼了,然后我就到网上查drawLayout是什么?原来是侧滑菜单,好吧,赶快学习下,网上很多例子,都不怎么全面,然后找了找了呀,终于找到到了个成形的,然后自己改了下,不说了,先爆照

Android之drawlayout使用和总结_第1张图片

Android之drawlayout使用和总结_第2张图片

Android之drawlayout使用和总结_第3张图片



导航抽屉是在 屏幕左侧边缘的 应用主导航选项的面板. 它大多数时间是隐藏的, 但当用户用手指从屏幕的左侧滑动, 或者当用户点击应用顶部工具栏的应用图标的时候, 它就会显示.


创建一个抽屉布局

添加一个抽屉, 声明UI的时候需要吧DrawLayout作为你的布局文件的根视图(root view). 在DrawLayout里面, 添加一个主要内容的视图(当抽屉导航隐藏的时候你的主要的布局文件), 和另一个包含导航抽屉的视图.

举例来说, 下面的布局使用了DrawLayout, 它有2个子视图: 一个FrameLayout包含主要的内容, 一个ListView的导航抽屉.

drawlayout.xml

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <!-- The navigation drawer -->
    <ListView android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

这个布局文件示范了一些重要的布局特征.

  • 主要内容的视图(FrameLayout)必须是DrawLayout的第一个子元素, 因为导航抽屉是在主要内容视图的上面.
  • 主要内容视图设置为匹配父视图的宽度和高度, 因为它代表了整个界面导航抽屉是隐藏的.
  • 抽屉视图(ListView)必须指定其水平重力与android:layout_gravity属性。支持从右到左(RTL)语言,指定值与"start" 代替 "left"(所以抽屉里出现在布局的右侧当布局是RTL时).
  • 抽屉视图指定其宽度用dp单位和高度匹配父视图。抽屉里的宽度不能超过320 dp, 所以用户总是可以看到主要内容视图的一部分.

初始化抽屉列表

在你的Activity中, 第一件事就是初始化导航抽屉列表里面的元素, 你如何做取决于你的应用程序的内容,但一个导航抽屉通常包括一个ListView, 因此清单应该由一个Adapter填充(例如ArrayAdapter或SimpleCursorAdapter).

例如, 这里演示了如何用String array来初始化一个导航列表.


simpleitem.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ImageView
        android:id="@+id/imgsim"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        />

    <TextView
        android:id="@+id/titlesim"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:textColor="#ffaaff"
        android:textSize="20sp"

        />
    </LinearLayout>

planetfragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ImageView
        android:id="@+id/imgfrag1"
        android:background="@drawable/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        />

    <TextView
        android:id="@+id/titlefrag1"
        android:text="我是content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:textColor="#ffaaff"
        android:textSize="20sp"
        />
    <Button
        android:id="@+id/buttonfragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点我"/>
    </LinearLayout>
这个 代码调用setOnItemClickListener()去接收导航抽屉列表的点击事件. 下一节将展示如何实现这个接口,当用户选择一个Item时改变内容视图.

处理导航点击事件

当用户选择了抽屉列表里面的一个Item时, 系统调用onItemClickListener上的onItemClick(), 给setOnItemClickListener().

你在onItemClick()方法里面做什么, 取决于你的app实现的结构. 在下面的例子中, 选择每一个Item都会在主要内容的布局中插入一个不同的Fragment.


MainActivity.java

package com.example.think.adapterlearn;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.AndroidCharacter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends Activity {
    private List<Map<String,Object>> mData;
    private List<Map<String,Object>> mDataSimple;
    private ListView listView;
    private ListView mDrawList;
    public FragmentManager fragmentManager;
    private static final String[] strs = new String[] {
        "first", "second", "third", "fourth", "fifth"
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        fragmentManager=getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame,new PlanetFragment()).commit();
        setContentView(R.layout.drawlayout);
//        lv = (ListView) findViewById(R.id.lv);//得到ListView对象的引用 /*为ListView设置Adapter来绑定数据*/
//        lv.setAdapter(new ArrayAdapter<String>(this,
//                android.R.layout.simple_list_item_1, strs));
//         mData=getData();
//          MyAdapter adapter=new MyAdapter(this);
        mDataSimple=getDataBySimple();
        SimpleAdapter simpleAdapter=new SimpleAdapter(this,mDataSimple,R.layout.simpleitem,new String[]{"title","img"},new int[]{R.id.titlesim,R.id.imgsim});
//         listView= (ListView) findViewById(R.id.lv);
        mDrawList=(ListView)findViewById(R.id.left_drawer);
        mDrawList.setAdapter(simpleAdapter);
        mDrawList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                Toast.makeText(getApplicationContext(),"第"+id+"行",Toast.LENGTH_LONG).show();
                  selectItem(position);
            }
        });
//         listView.setAdapter(simpleAdapter);
//       SimpleAdapter simpleAdapter=new SimpleAdapter(this,getData(),R.layout.activity_main,new String[]{"title","info","img"},new int[]{R.id.title,R.id.title1,R.id.img});
//       setContentView(R.layout.activity_main);
//       setListAdapter(adapter);
    }

    private void selectItem(int position) {
        Fragment fragment=new  PlanetFragment();
        Bundle args=new Bundle();
//        args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
        PlanetFragment.num=position;
        fragmentManager=getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame,fragment).commit();
        mDrawList.setItemChecked(position,true);
//        setTitle();
    }
    private List<Map<String, Object>> getData() {
        //map.put(参数名字,参数值)
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("title", "摩托罗拉");
        map.put("info","你好");
        map.put("img", R.mipmap.ic_launcher);
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("title", "诺基亚");
        map.put("info","你好");
        map.put("img", R.mipmap.ic_launcher);
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("title", "三星");
        map.put("info","你好");
        map.put("img", R.mipmap.ic_launcher);
        list.add(map);
        return list;
    }
    private List<Map<String, Object>> getDataBySimple() {
        //map.put(参数名字,参数值)
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("title", "摩托罗拉");
        map.put("img", R.mipmap.ic_launcher);
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("title", "诺基亚");
        map.put("img", R.mipmap.ic_launcher);
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("title", "三星");
        map.put("img",R.mipmap.ic_launcher);
        list.add(map);
        return list;
    }
    public void showInfo(){
        new AlertDialog.Builder(this).setTitle("hello").setMessage("intruduce").setPositiveButton("确定",
        new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        }).show();
    }
    public final class ViewHolder{
        public ImageView img;
        public Button viewBtn;
        public TextView title;
        public TextView info;
    }

//    @Override
//    protected void onListItemClick(ListView l, View v, int position, long id) {
////        super.onListItemClick(l, v, position, id);
//           Log.v("myListView4",(String)mData.get(position).get("title"));
//    }

    public class MyAdapter extends BaseAdapter{
        private LayoutInflater inflater;
        @Override
        public int getCount() {
            return mData.size();
        }
       public MyAdapter(Context content){
           inflater=LayoutInflater.from(content);
       }
        @Override
        public Object getItem(int position) {
            return null;
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder=new ViewHolder();
            if(convertView==null){
//                holder.img=(ImageView)inflater.inflate(R.id.img,null);
                  convertView=inflater.inflate(R.layout.items,null);
                  holder.img= (ImageView) convertView.findViewById(R.id.img);
                  holder.title= (TextView) convertView.findViewById(R.id.title);
                  holder.info= (TextView) convertView.findViewById(R.id.title1);
                  holder.viewBtn= (Button) convertView.findViewById(R.id.button1);
                  convertView.setTag(holder);
            }else{
                holder= (ViewHolder) convertView.getTag();
            }
            holder.img.setBackgroundResource((Integer) mData.get(position).get("img"));
            holder.title.setText((String) mData.get(position).get("title"));
            holder.info.setText((String)mData.get(position).get("info"));
            holder.viewBtn.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    showInfo();
                }
            } );
            return convertView;
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

 
 

PlanetFragment.java

package com.example.think.adapterlearn;

import android.app.Fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

/**
 * Created by Think on 2015/9/21.
 */
public class PlanetFragment extends Fragment implements DialogInterface.OnClickListener{
     public static String ARG_PLANET_NUMBER;
     public static int num=0;
     public View button;
     public PlanetFragment(){
     }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.planetfragment,container,false);
        button=view.findViewById(R.id.buttonfragment);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(),num+"PlanetFragment",Toast.LENGTH_LONG).show();
            }
        });
//        return super.onCreateView(inflater, container, savedInstanceState);
        return view;
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {

    }
}
 
 

你可能感兴趣的:(android,DrawLayout)