zoukankan      html  css  js  c++  java
  • 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);
        }
    }
  • 相关阅读:
    pcs7 opc 连接问题
    nuget 多个程序引用同一个库时,当个这个库更新时,引用的程序都要跟新,否则会在运行时出错
    nuget update FileConflictAction
    a
    C#版本
    .NET Framework版本与CLR版本之间的关系
    c# 将一种数组类型转成另一种数组类型
    tfs 清除缓存,在需要时
    java枚举和仿枚举
    java反射的用法
  • 原文地址:https://www.cnblogs.com/yu12/p/7464849.html
Copyright © 2011-2022 走看看