DrawerLayout简单框架

Posted 随笔记

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DrawerLayout简单框架相关的知识,希望对你有一定的参考价值。

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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.ListView;
import android.widget.TextView;

import java.lang.reflect.GenericArrayType;
import java.security.cert.CertPathValidatorException;
import java.util.ArrayList;
import java.util.List;



public class MainActivity extends BaseActivity {

    private List<String>list=new ArrayList<String>();
    //TabLayout标签
    private String[] mTitle=new String[]{"推荐","热点","北京","科技","军事","视频","社会"};
    private DrawerLayout drawerLayout;
    private ViewPager vp;
    private ListView lv;
    private ActionBarDrawerToggle toggle;
    private TabLayout tabLayout;
    private   String tltie;
    private ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initData();
    }

    private void initData() {
        for (int i=0;i<5;++i){
             list.add("这是"+i);
        }
    }

    private void initView() {
        //找控件
         drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayou);
         vp = (ViewPager) findViewById(R.id.vp);
         lv = (ListView) findViewById(R.id.lv);
        //找到标签布局控件
         tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        //初始化ActionBar
        //参数1:上下文
        //参数2:抽屉
        //参数3:打开抽屉的描述
        //参数4:关闭抽屉的描述
         toggle = new ActionBarDrawerToggle(this,drawerLayout, R.string.clop,R.string.op);
         //设置状态同步
         toggle.syncState();
        //设置抽屉打开关闭监听
         drawerLayout.addDrawerListener(toggle);
        //设置适配器
        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
        lv.setAdapter(adapter);

        for (String str:mTitle){
            //根据标签数生成标签
            tabLayout.addTab(tabLayout.newTab().setText(str));
        }
        //TabLayout关联ViewPager
        vp.setAdapter(new MyPageAdapter(getSupportFragmentManager()));
        //让TabLayout发生关联
        tabLayout.setupWithViewPager(vp);



    }
    //重写该方法,监听actionbar 条目监听(箭头、三个横线)
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return toggle.onOptionsItemSelected(item)| super.onOptionsItemSelected(item);
    }


    class MyPageAdapter extends FragmentPagerAdapter{


        public MyPageAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return Fragment1.read(mTitle[position]);
        }

        @Override
        public int getCount() {
            return mTitle.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitle[position];
        }
    }

}

//上面是主Activity需要写的代码

//下面是布局

 

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawerLayour"

    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context="com.bawei.newinfo.MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
       <android.support.design.widget.TabLayout
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:id="@+id/tabLayout"
           app:tabMode="scrollable"
           ></android.support.design.widget.TabLayout>
          <android.support.v4.view.ViewPager
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:id="@+id/vp"
              ></android.support.v4.view.ViewPager>

    </LinearLayout>
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lv"
        android:layout_gravity="start"
        android:background="#08f"
        ></ListView>

</android.support.v4.widget.DrawerLayout>

 

//Fragment

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bawei.newinfo.R;



public class Fragment1 extends Fragment {
    private String title;
    private static final  String  PAGE_TITLE="PAGE_TITLE";
    private TextView tv;


    public static Fragment1 newInstance(String title){
     Bundle bundle=new Bundle();
     bundle.putString(PAGE_TITLE,title);
     Fragment1 fragment1=new Fragment1();
     fragment1.setArguments(bundle);
     return  fragment1;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getArguments().getStringArrayList(PAGE_TITLE);


    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.f1,container,false);
        tv = (TextView) view.findViewById(R.id.textView);
        tv.setText(title);
        return view;
    }
}

//设置头布局箭头

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.AttributeSet;
import android.view.View;


public class BaseActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
}

 

以上是关于DrawerLayout简单框架的主要内容,如果未能解决你的问题,请参考以下文章

DrawerLayout 的项目点击 - 啥时候更换片段合适?

DrawerLayout简单框架

如何在导航控件片段中关闭导航 DrawerLayout onBackPressed

Android Design Support控件之DrawerLayout简单使用

从android中的片段管理工具栏的导航和后退按钮

Android在drawerlayout的listview底部添加textview