深圳全飞鸿

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 605|回复: 2
打印 上一主题 下一主题

Tabbed Activity

[复制链接]

800

主题

1379

帖子

7725

积分

版主

Rank: 7Rank: 7Rank: 7

积分
7725
跳转到指定楼层
楼主
发表于 2019-5-22 20:28:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. package com.example.administrator.myapplicationb;

  2. import android.support.design.widget.FloatingActionButton;
  3. import android.support.design.widget.Snackbar;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.support.v7.widget.Toolbar;

  6. import android.support.v4.app.Fragment;
  7. import android.support.v4.app.FragmentManager;
  8. import android.support.v4.app.FragmentPagerAdapter;
  9. import android.support.v4.view.ViewPager;
  10. import android.os.Bundle;
  11. import android.view.LayoutInflater;
  12. import android.view.Menu;
  13. import android.view.MenuItem;
  14. import android.view.View;
  15. import android.view.ViewGroup;

  16. import android.widget.TextView;

  17. public class MainActivity extends AppCompatActivity {

  18.     /**
  19.      * The {@link android.support.v4.view.PagerAdapter} that will provide
  20.      * fragments for each of the sections. We use a
  21.      * {@link FragmentPagerAdapter} derivative, which will keep every
  22.      * loaded fragment in memory. If this becomes too memory intensive, it
  23.      * may be best to switch to a
  24.      * {@link android.support.v4.app.FragmentStatePagerAdapter}.
  25.      */
  26.     private SectionsPagerAdapter mSectionsPagerAdapter;

  27.     /**
  28.      * The {@link ViewPager} that will host the section contents.
  29.      */
  30.     private ViewPager mViewPager;

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

  35.         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  36.         setSupportActionBar(toolbar);

  37.         // Create the adapter that will return a fragment for each of the three
  38.         // primary sections of the activity.
  39.         mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

  40.         // Set up the ViewPager with the sections adapter.
  41.         mViewPager = (ViewPager) findViewById(R.id.container);
  42.         mViewPager.setAdapter(mSectionsPagerAdapter);

  43.         FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  44.         fab.setOnClickListener(new View.OnClickListener() {
  45.             @Override
  46.             public void onClick(View view) {
  47.                 Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
  48.                         .setAction("Action", null).show();
  49.             }
  50.         });

  51.     }


  52.     @Override
  53.     public boolean onCreateOptionsMenu(Menu menu) {
  54.         // Inflate the menu; this adds items to the action bar if it is present.
  55.         getMenuInflater().inflate(R.menu.menu_main, menu);
  56.         return true;
  57.     }

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

  64.         //noinspection SimplifiableIfStatement
  65.         if (id == R.id.action_settings) {
  66.             return true;
  67.         }

  68.         return super.onOptionsItemSelected(item);
  69.     }

  70.     /**
  71.      * A placeholder fragment containing a simple view.
  72.      */
  73.     public static class PlaceholderFragment extends Fragment {
  74.         /**
  75.          * The fragment argument representing the section number for this
  76.          * fragment.
  77.          */
  78.         private static final String ARG_SECTION_NUMBER = "section_number";

  79.         public PlaceholderFragment() {
  80.         }

  81.         /**
  82.          * Returns a new instance of this fragment for the given section
  83.          * number.
  84.          */
  85.         public static PlaceholderFragment newInstance(int sectionNumber) {
  86.             PlaceholderFragment fragment = new PlaceholderFragment();
  87.             Bundle args = new Bundle();
  88.             args.putInt(ARG_SECTION_NUMBER, sectionNumber);
  89.             fragment.setArguments(args);
  90.             return fragment;
  91.         }

  92.         @Override
  93.         public View onCreateView(LayoutInflater inflater, ViewGroup container,
  94.                                  Bundle savedInstanceState) {
  95.             View rootView = inflater.inflate(R.layout.fragment_main, container, false);
  96.             TextView textView = (TextView) rootView.findViewById(R.id.section_label);
  97.             textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
  98.             return rootView;
  99.         }
  100.     }

  101.     /**
  102.      * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
  103.      * one of the sections/tabs/pages.
  104.      */
  105.     public class SectionsPagerAdapter extends FragmentPagerAdapter {

  106.         public SectionsPagerAdapter(FragmentManager fm) {
  107.             super(fm);
  108.         }

  109.         @Override
  110.         public Fragment getItem(int position) {
  111.             // getItem is called to instantiate the fragment for the given page.
  112.             // Return a PlaceholderFragment (defined as a static inner class below).
  113.             return PlaceholderFragment.newInstance(position + 1);
  114.         }

  115.         @Override
  116.         public int getCount() {
  117.             // Show 3 total pages.
  118.             return 3;
  119.         }
  120.     }
  121. }
复制代码
回复

使用道具 举报

800

主题

1379

帖子

7725

积分

版主

Rank: 7Rank: 7Rank: 7

积分
7725
沙发
 楼主| 发表于 2019-5-22 20:28:41 | 只看该作者
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:id="@+id/main_content"
  6.     android:layout_width="match_parent"
  7.     android:layout_height="match_parent"
  8.     android:fitsSystemWindows="true"
  9.     tools:context="com.example.administrator.myapplicationb.MainActivity">

  10.     <android.support.design.widget.AppBarLayout
  11.         android:id="@+id/appbar"
  12.         android:layout_width="match_parent"
  13.         android:layout_height="wrap_content"
  14.         android:paddingTop="@dimen/appbar_padding_top"
  15.         android:theme="@style/AppTheme.AppBarOverlay">

  16.         <android.support.v7.widget.Toolbar
  17.             android:id="@+id/toolbar"
  18.             android:layout_width="match_parent"
  19.             android:layout_height="?attr/actionBarSize"
  20.             android:layout_weight="1"
  21.             android:background="?attr/colorPrimary"
  22.             app:layout_scrollFlags="scroll|enterAlways"
  23.             app:popupTheme="@style/AppTheme.PopupOverlay"
  24.             app:title="@string/app_name">

  25.         </android.support.v7.widget.Toolbar>

  26.     </android.support.design.widget.AppBarLayout>

  27.     <android.support.v4.view.ViewPager
  28.         android:id="@+id/container"
  29.         android:layout_width="match_parent"
  30.         android:layout_height="match_parent"
  31.         app:layout_behavior="@string/appbar_scrolling_view_behavior" />

  32.     <android.support.design.widget.FloatingActionButton
  33.         android:id="@+id/fab"
  34.         android:layout_width="wrap_content"
  35.         android:layout_height="wrap_content"
  36.         android:layout_gravity="end|bottom"
  37.         android:layout_margin="@dimen/fab_margin"
  38.         app:srcCompat="@android:drawable/ic_dialog_email" />

  39. </android.support.design.widget.CoordinatorLayout>
复制代码
回复 支持 反对

使用道具 举报

800

主题

1379

帖子

7725

积分

版主

Rank: 7Rank: 7Rank: 7

积分
7725
板凳
 楼主| 发表于 2019-5-22 20:29:02 | 只看该作者
  1. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:app="http://schemas.android.com/apk/res-auto"
  3.     xmlns:tools="http://schemas.android.com/tools"
  4.     android:id="@+id/constraintLayout"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     tools:context="com.example.administrator.myapplicationb.MainActivity$PlaceholderFragment">

  8.     <TextView
  9.         android:id="@+id/section_label"
  10.         android:layout_width="wrap_content"
  11.         android:layout_height="wrap_content"
  12.         android:layout_marginBottom="@dimen/activity_vertical_margin"
  13.         android:layout_marginEnd="@dimen/activity_horizontal_margin"
  14.         android:layout_marginStart="@dimen/activity_horizontal_margin"
  15.         android:layout_marginTop="@dimen/activity_vertical_margin"
  16.         app:layout_constraintLeft_toLeftOf="parent"
  17.         app:layout_constraintTop_toTopOf="@+id/constraintLayout"
  18.         tools:layout_constraintLeft_creator="1"
  19.         tools:layout_constraintTop_creator="1" />

  20. </android.support.constraint.ConstraintLayout>
复制代码
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|nagomes  

GMT+8, 2025-6-29 07:02 , Processed in 0.059432 second(s), 20 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表