一个十分简洁实用的MD风格的UI主框架

2017-5-23

MainActivity

  1. public class MainActivity extends AppCompatActivity {
  2. @BindView(R.id.toolbar) Toolbar toolbar;
  3. @BindView(R.id.tabs) TabLayout tabLayout;
  4. @BindView(R.id.appbar) AppBarLayout appbar;
  5. @BindView(R.id.viewpager) ViewPager viewPager;
  6. @BindView(R.id.fab) FloatingActionButton fab;//浮动操作按钮
  7. @BindView(R.id.nav_view) NavigationView navigationView;//
  8. @BindView(R.id.drawer_layout) DrawerLayout drawerLayout;//侧滑布局
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_main);
  13. ButterKnife.bind(this);
  14. setSupportActionBar(toolbar);
  15. getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu);
  16. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  17. navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
  18. @Override
  19. public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
  20. menuItem.setChecked(true);
  21. drawerLayout.closeDrawers();
  22. return true;
  23. }
  24. });
  25. Adapter adapter = new Adapter(getSupportFragmentManager());
  26. adapter.addFragment(new CheeseListFragment(), "白乾涛");
  27. adapter.addFragment(new CheeseListFragment(), "包青天");
  28. viewPager.setAdapter(adapter);
  29. tabLayout.setupWithViewPager(viewPager);
  30. }
  31. @Override
  32. public boolean onCreateOptionsMenu(Menu menu) {
  33. getMenuInflater().inflate(R.menu.sample_actions, menu);
  34. return true;
  35. }
  36. @Override
  37. public boolean onPrepareOptionsMenu(Menu menu) {
  38. switch (AppCompatDelegate.getDefaultNightMode()) {
  39. case AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM:
  40. menu.findItem(R.id.menu_night_mode_system).setChecked(true);
  41. break;
  42. case AppCompatDelegate.MODE_NIGHT_AUTO:
  43. menu.findItem(R.id.menu_night_mode_auto).setChecked(true);
  44. break;
  45. case AppCompatDelegate.MODE_NIGHT_YES:
  46. menu.findItem(R.id.menu_night_mode_night).setChecked(true);
  47. break;
  48. case AppCompatDelegate.MODE_NIGHT_NO:
  49. menu.findItem(R.id.menu_night_mode_day).setChecked(true);
  50. break;
  51. }
  52. return true;
  53. }
  54. @Override
  55. public boolean onOptionsItemSelected(MenuItem item) {
  56. switch (item.getItemId()) {
  57. case android.R.id.home:
  58. drawerLayout.openDrawer(GravityCompat.START);
  59. return true;
  60. case R.id.menu_night_mode_system:
  61. setNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
  62. break;
  63. case R.id.menu_night_mode_day:
  64. setNightMode(AppCompatDelegate.MODE_NIGHT_NO);
  65. break;
  66. case R.id.menu_night_mode_night:
  67. setNightMode(AppCompatDelegate.MODE_NIGHT_YES);
  68. break;
  69. case R.id.menu_night_mode_auto:
  70. setNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
  71. break;
  72. }
  73. return super.onOptionsItemSelected(item);
  74. }
  75. private void setNightMode(@AppCompatDelegate.NightMode int nightMode) {
  76. AppCompatDelegate.setDefaultNightMode(nightMode);
  77. if (Build.VERSION.SDK_INT >= 11) recreate();
  78. }
  79. @OnClick(R.id.fab)
  80. public void onViewClicked() {
  81. Snackbar.make(fab, "Here's a Snackbar", Snackbar.LENGTH_LONG).show();
  82. }
  83. static class Adapter extends FragmentPagerAdapter {
  84. private final List<Fragment> mFragments = new ArrayList<>();
  85. private final List<String> mFragmentTitles = new ArrayList<>();
  86. public Adapter(FragmentManager fm) {
  87. super(fm);
  88. }
  89. public void addFragment(Fragment fragment, String title) {
  90. mFragments.add(fragment);
  91. mFragmentTitles.add(title);
  92. }
  93. @Override
  94. public Fragment getItem(int position) {
  95. return mFragments.get(position);
  96. }
  97. @Override
  98. public int getCount() {
  99. return mFragments.size();
  100. }
  101. @Override
  102. public CharSequence getPageTitle(int position) {
  103. return mFragmentTitles.get(position);
  104. }
  105. }
  106. }

MainActivity布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. android:id="@+id/drawer_layout"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:fitsSystemWindows="true">
  8. <include layout="@layout/include_list_viewpager"/>
  9. <!--侧滑布局-->
  10. <android.support.design.widget.NavigationView
  11. android:id="@+id/nav_view"
  12. android:layout_width="wrap_content"
  13. android:layout_height="match_parent"
  14. android:layout_gravity="start"
  15. android:fitsSystemWindows="true"
  16. app:headerLayout="@layout/nav_header"
  17. app:menu="@menu/drawer_view"/>
  18. </android.support.v4.widget.DrawerLayout>

MainActivity内容区域布局

  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. android:id="@+id/main_content"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent">
  7. <android.support.design.widget.AppBarLayout
  8. android:id="@+id/appbar"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
  12. <android.support.v7.widget.Toolbar
  13. android:id="@+id/toolbar"
  14. android:layout_width="match_parent"
  15. android:layout_height="?attr/actionBarSize"
  16. android:background="?attr/colorPrimary"
  17. app:layout_scrollFlags="scroll|enterAlways|snap"
  18. app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
  19. <android.support.design.widget.TabLayout
  20. android:id="@+id/tabs"
  21. android:layout_width="match_parent"
  22. android:layout_height="wrap_content"/>
  23. </android.support.design.widget.AppBarLayout>
  24. <android.support.v4.view.ViewPager
  25. android:id="@+id/viewpager"
  26. android:layout_width="match_parent"
  27. android:layout_height="match_parent"
  28. app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
  29. <android.support.design.widget.FloatingActionButton
  30. android:id="@+id/fab"
  31. android:layout_width="wrap_content"
  32. android:layout_height="wrap_content"
  33. android:layout_gravity="end|bottom"
  34. android:layout_margin="@dimen/fab_margin"
  35. android:src="@drawable/ic_done"/>
  36. </android.support.design.widget.CoordinatorLayout>

Fragment

  1. public class CheeseListFragment extends Fragment {
  2. @Override
  3. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  4. RecyclerView recyclerView = (RecyclerView) inflater.inflate(R.layout.fragment_cheese_list, container, false);
  5. recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
  6. recyclerView.setAdapter(new MyAdapter(getActivity(), getRandomSublist()));
  7. return recyclerView;
  8. }
  9. private List<String> getRandomSublist() {
  10. String[] array = Cheeses.sCheeseStrings;
  11. ArrayList<String> list = new ArrayList<String>();
  12. while (list.size() < 30) {
  13. list.add(array[new Random().nextInt(array.length)]);
  14. }
  15. return list;
  16. }
  17. }

Adapter

  1. public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
  2. private List<String> mList;
  3. private Context context;
  4. public MyAdapter(Context context, List<String> mList) {
  5. this.mList = mList;
  6. this.context = context;
  7. }
  8. @Override
  9. public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  10. return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.list_item, parent, false));
  11. }
  12. @Override
  13. public void onBindViewHolder(final ViewHolder holder, int position) {
  14. holder.mTextView.setText(mList.get(position));
  15. holder.ll_item.setOnClickListener(new View.OnClickListener() {
  16. @Override
  17. public void onClick(View v) {
  18. Intent intent = new Intent(context, CheeseDetailActivity.class);
  19. intent.putExtra(CheeseDetailActivity.EXTRA_NAME, mList.get(holder.getAdapterPosition()));
  20. context.startActivity(intent);
  21. }
  22. });
  23. Glide.with(holder.mImageView.getContext())
  24. .load(Cheeses.getRandomCheeseDrawable())
  25. .fitCenter()
  26. .into(holder.mImageView);
  27. }
  28. @Override
  29. public int getItemCount() {
  30. return mList.size();
  31. }
  32. static class ViewHolder extends RecyclerView.ViewHolder {
  33. @BindView(R.id.avatar) CircleImageView mImageView;
  34. @BindView(R.id.text) TextView mTextView;
  35. @BindView(R.id.ll_item) View ll_item;
  36. ViewHolder(View view) {
  37. super(view);
  38. ButterKnife.bind(this, view);
  39. }
  40. }
  41. }
2017-5-23




附件列表

    原文地址:https://www.cnblogs.com/baiqiantao/p/6895885.html