zoukankan      html  css  js  c++  java
  • TabLoaout简单框架

    import android.os.Bundle;
    import android.support.annotation.Nullable;
    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.ActionBarDrawerToggle;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    
    import com.bwie.newsinfo.fragment.Fragment1;
    
    import java.util.ArrayList;
    import java.util.List;
    
    
    public class HomeActivity extends BaseActivity implements AdapterView.OnItemClickListener {
    
    
        private DrawerLayout drawerLayout;
        private ListView leftDrawer;
        private List<String> list = new ArrayList<String>();
        private ArrayAdapter<String> adapter;
        private ActionBarDrawerToggle toggle;
        private final String[] mTitles = new String[]{"推荐","热点","北京","科技","军事","视频","社会"};
        private ViewPager vp;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            initData();
            initView();
    
    
        }
    
        /**
         * 初始化数据
         */
        private void initData() {
    
    
            for (int i = 0; i < 5; ++i) {
                list.add("泡妞0" + i);
            }
    
    
        }
    
        /**
         * 初始化View
         */
        private void initView() {
    
    
            drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
            leftDrawer = (ListView) findViewById(R.id.leftDrawer);
            //初始化ActionBar
            //参数1:上下文
            //参数2:抽屉
            //参数3:打开抽屉的描述
            //参数4:关闭抽屉的描述
            toggle = new ActionBarDrawerToggle(this,drawerLayout, R.string.openDrawer,R.string.closeDrawer);
            //设置状态同步
            toggle.syncState();
            //设置抽屉打开关闭监听
            drawerLayout.addDrawerListener(toggle);
            //设置适配器
            adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
            leftDrawer.setAdapter(adapter);
            leftDrawer.setOnItemClickListener(this);
    
    
            //找到标签布局控件
            TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
    
            for (String str : mTitles)
            {
                //更具标签数生成标签
                tabLayout.addTab(tabLayout.newTab().setText(str));
            }
    
            //TabLayout关联ViewPager
            vp = (ViewPager) findViewById(R.id.vp);
            vp.setAdapter(new MyFragmentAdapter(getSupportFragmentManager()));
            //让TabLayout发生关联
            tabLayout.setupWithViewPager(vp);
    
    
    
        }
    
        //重写该方法,监听actionbar 条目监听(箭头、三个横线)
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            //如果抽屉打开,点击按钮,就会关闭,反之亦然
            return toggle.onOptionsItemSelected(item) | super.onOptionsItemSelected(item);
        }
    
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    
            //关闭抽屉
            drawerLayout.closeDrawer(leftDrawer);
        }
    
    
    
        class MyFragmentAdapter extends FragmentPagerAdapter{
    
            public MyFragmentAdapter(FragmentManager fm) {
                super(fm);
            }
    
            @Override
            public Fragment getItem(int position) {
    
    
    
                return Fragment1.newInstance(mTitles[position]);
            }
    
            @Override
            public int getCount() {
                return mTitles.length;
            }
    
            //重写该方法,显示标签字体 bug
            @Override
            public CharSequence getPageTitle(int position) {
                return mTitles[position];
            }
        }
    
    
    
    
    
    
    
    }


    标题栏

    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v7.app.ActionBar;
    import android.support.v7.app.AppCompatActivity;
    
    
    public class BaseActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //隐藏标题栏
            //getSupportActionBar().hide();
            //标题栏
            ActionBar actionBar = getSupportActionBar();
            //显示箭头
            actionBar.setDisplayHomeAsUpEnabled(true);
    
    
        }
    }
    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;
    
    
    public class Fragment1 extends Fragment {
    
        private static final String PAGE_TITLE = "PAGE_TITLE";
        private String title;
    
    
        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);
    
            //当Fragemnt一旦被创建,我就可以接受对方传递过来的值
            title = getArguments().getString(PAGE_TITLE);
    
    
        }
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    
            TextView textView = new TextView(getActivity());
            textView.setText(title);
            textView.setTextSize(50);
    
    
            return textView;
        }
    }

    XML

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout
        android:id="@+id/draw"
        xmlns:android="http://schemas.android.com/apk/res/android"
        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.bwie.lianxi23.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="40dp"
              android:id="@+id/tb"
              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:layout_gravity="start"
        android:id="@+id/lv"
        android:background="@color/colorAccent"
        ></ListView>
    </android.support.v4.widget.DrawerLayout>



  • 相关阅读:
    HDU 4334
    HDU 1280
    HDU 1060
    HDU 4033
    大三角形分成4个面积相等的小三角形
    HDU 1087
    HDU 4313
    Sleep(0)及其使用场景
    Decorator(装饰、油漆工)对象结构型模式
    Debug Assertion Failed!
  • 原文地址:https://www.cnblogs.com/shangliang88/p/7464800.html
Copyright © 2011-2022 走看看