当前位置:   article > 正文

Fragment 与 TabLayout_fragment tablayout

fragment tablayout

本来不该写tabLayout   因为他自己就有着相当大的知识点。


为了使用tabLayout控件,我们需要加入compile 'com.android.support:design:23.2.1' 老一套了

我们主要说一下Fragment

这个Fragment 和 Activity周期相差无几,就用当初学习Activity生命周期的方式来刺探一下Fragment的生命周期。

贴一下日志你们就会明白;


关于生命周期就不多说了网上又说的特别好的。


前边当我写好侧滑的菜单时,我发现个问题,就是按下导航栏的条目后怎么切换到相应的layout中去,一个非常需要解决的问题,后来我用<include/>进行插入,然后就各种奇葩的问题不符合设计规则,然后昨天就帖子,找博客。大家们都一致推荐Fragment,当初学Android时只知道不过是一种布局算了,当初只写的是几个长宽不同的彩色方块放在一起重叠起来,平常没用过今天就插手干了起来。


一、主要说一下创建这个Demo 的整体思路:

1.首先是创建layout 这个也是我们最需要在乎的一部,因为我们终于知道将新的控件放在哪了。

2.我们需要创建一个新的类与这个layout一一对应,并且继承于Fragment。这类似与MainActivity 只是他继承与AppCompatActivity罢了。

3往main_actyvity.xml 中添加一个Fragment布局

4.在MainActivity中就是对Fragment的添加、移除、替换的操作。


贴一下主要的代码:

二、创建的新的Fragment布局的   layout 名字是frame1_layout

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:background="#990033"
  5. android:layout_height="match_parent">
  6. <Button
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:text="Toast_Button_1"
  10. android:textAllCaps="false"
  11. android:id="@+id/button_tos_1"
  12. android:layout_centerVertical="true"
  13. android:layout_centerHorizontal="true" />
  14. </RelativeLayout>

里面只写了一个Button.


三、创建新的与之关联类并继承Fragment 名字是 Fragment1_management

  1. package com.example.hejingzhou.fragmentdemo;
  2. import android.content.Context;
  3. import android.support.v4.app.Fragment;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.widget.Button;
  10. import android.widget.Toast;
  11. /**
  12. * Created by Hejingzhou on 2016/4/3.
  13. */
  14. public class Fragment1_management extends Fragment implements View.OnClickListener{
  15. private String TAG = getClass().getSimpleName();
  16. @Override
  17. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  18. View fragment_1_view = inflater.inflate(R.layout.fragment1_layout,container,false);
  19. Button btnToast = (Button)fragment_1_view.findViewById(R.id.button_tos_1);
  20. btnToast.setOnClickListener(this);
  21. return fragment_1_view;
  22. }
  23. @Override
  24. public void onClick(View v) {
  25. if(v.getId() == R.id.button_tos_1){
  26. Toast.makeText(getActivity(), "Fragment_1", Toast.LENGTH_SHORT).show();
  27. }
  28. }
  29. /**
  30. * 以下和创建的操作文件并没有关系
  31. * 只是刺探Fragment的生命周期
  32. * @param context
  33. */
  34. @Override
  35. public void onAttach(Context context) {
  36. super.onAttach(context);
  37. Log.i(TAG,"onAttach");
  38. }
  39. @Override
  40. public void onViewCreated(View view, Bundle savedInstanceState) {
  41. super.onViewCreated(view, savedInstanceState);
  42. Log.i(TAG,"onViewCreated");
  43. }
  44. @Override
  45. public void onCreate(Bundle savedInstanceState) {
  46. super.onCreate(savedInstanceState);
  47. Log.i(TAG,"onCreate");
  48. }
  49. @Override
  50. public void onActivityCreated(Bundle savedInstanceState) {
  51. super.onActivityCreated(savedInstanceState);
  52. Log.i(TAG,"onActivityCreated");
  53. }
  54. @Override
  55. public void onStart() {
  56. super.onStart();
  57. Log.i(TAG,"onStart");
  58. }
  59. @Override
  60. public void onDetach() {
  61. super.onDetach();
  62. Log.i(TAG,"onDetach");
  63. }
  64. @Override
  65. public void onResume() {
  66. super.onResume();
  67. Log.i(TAG,"onResume");
  68. }
  69. @Override
  70. public void onPause() {
  71. super.onPause();
  72. Log.i(TAG,"onPause");
  73. }
  74. @Override
  75. public void onStop() {
  76. super.onStop();
  77. Log.i(TAG,"onStop");
  78. }
  79. @Override
  80. public void onDestroyView() {
  81. super.onDestroyView();
  82. Log.i(TAG,"onDestroyView");
  83. }
  84. }

四、看看主要的MainActivity

主要是这几个类:

  1. <span style="font-size:18px;color:#009900;"><span style="white-space:pre"> </span>android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
  2. Fragment fragment = manager.findFragmentByTag(tag);
  3. android.support.v4.app.FragmentTransaction transaction = manager.beginTransaction();</span>



  1. package com.example.hejingzhou.fragmentdemo;
  2. import android.support.design.widget.TabLayout;
  3. import android.support.v4.app.Fragment;
  4. import android.support.v4.app.FragmentTransaction;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.EditText;
  11. import android.widget.Toast;
  12. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  13. private EditText editTextRemovenumber;
  14. private EditText editTextReplace;
  15. private String TAG = getClass().getCanonicalName();
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);;
  19. setContentView(R.layout.activity_main);
  20. findViewById();
  21. /**
  22. * 滑动选项卡
  23. */
  24. /* TabLayout tabLayout = (TabLayout)findViewById(R.id.tabLayout);
  25. Log.i(TAG,"tabLayout.getId()"+tabLayout.getId());//tabLayout.getId()2131492975
  26. Log.i(TAG,"tabLayout.getSelectedTabPosition()"+tabLayout.getSelectedTabPosition());//-1
  27. Log.i(TAG, "tabLayout.getTabCount()"+tabLayout.getTabCount());//0
  28. tabLayout.setAlpha(0.5f);
  29. tabLayout.addTab(tabLayout.newTab().setText("笑话"));
  30. tabLayout.addTab(tabLayout.newTab().setText("视频"));
  31. tabLayout.addTab(tabLayout.newTab().setText("狗血"));
  32. tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
  33. @Override
  34. public void onTabSelected(TabLayout.Tab tab) {
  35. if (tab.getText().equals("笑话")) {
  36. Fragment1_management fragment1 = new Fragment1_management();
  37. addFragment(fragment1, "1");
  38. } else if (tab.getText().equals("狗血")) {
  39. Fragment2_management fragment2_management = new Fragment2_management();
  40. addFragment(fragment2_management, "2");
  41. } else if (tab.getText().equals("视频")) {
  42. Toast.makeText(getApplicationContext(), "选择视频", Toast.LENGTH_SHORT).show();
  43. }
  44. }
  45. @Override
  46. public void onTabUnselected(TabLayout.Tab tab) {
  47. Log.i(TAG,"滑动选项卡未选择");
  48. }
  49. @Override
  50. public void onTabReselected(TabLayout.Tab tab) {
  51. Log.i(TAG,"滑动选项卡选择");
  52. }
  53. });*/
  54. }
  55. private void findViewById() {
  56. Button btnFrag_1 = (Button) findViewById(R.id.button_fra1);
  57. btnFrag_1.setOnClickListener(this);
  58. Button btnFrag_2 = (Button) findViewById(R.id.button_fra2);
  59. btnFrag_2.setOnClickListener(this);
  60. editTextRemovenumber = (EditText) findViewById(R.id.editView);
  61. Button remove = (Button) findViewById(R.id.remove);
  62. remove.setOnClickListener(this);
  63. Button btnReplace = (Button) findViewById(R.id.buttonReplace);
  64. btnReplace.setOnClickListener(this);
  65. editTextReplace = (EditText) findViewById(R.id.editTextReplace);
  66. }
  67. @Override
  68. public void onClick(View v) {
  69. switch (v.getId()) {
  70. case R.id.button_fra1:
  71. Fragment1_management fragment1 = new Fragment1_management();
  72. addFragment(fragment1, "1");
  73. break;
  74. case R.id.button_fra2:
  75. Fragment2_management fragment2_management = new Fragment2_management();
  76. addFragment(fragment2_management, "2");
  77. break;
  78. case R.id.remove:
  79. Log.i(TAG,""+editTextRemovenumber.getText());
  80. if (editTextRemovenumber.getText() != null) {
  81. removeFragment(editTextRemovenumber.getText().toString());
  82. } else {
  83. Log.i(TAG,"确定editView为空,运行到这里还没出错");
  84. Toast.makeText(MainActivity.this, "请填写要删除的编号1或2", Toast.LENGTH_SHORT).show();
  85. }
  86. break;
  87. case R.id.buttonReplace:
  88. if (editTextReplace.getText() != null) {
  89. Log.i(TAG, "edit不为null");
  90. Log.i(TAG, editTextReplace.getText().toString());
  91. replace(editTextReplace.getText().toString());
  92. } else {
  93. Toast.makeText(MainActivity.this, "请填写要替换的号1或2", Toast.LENGTH_SHORT).show();
  94. }
  95. break;
  96. }
  97. }
  98. /**
  99. * 添加Fragment
  100. *
  101. * @param fragment
  102. * @param tag
  103. */
  104. private void addFragment(Fragment fragment, String tag) {
  105. if (fragment.isAdded()) {//不是这么做的 这样做并没效果
  106. } else {
  107. android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
  108. android.support.v4.app.FragmentTransaction transaction = manager.beginTransaction();
  109. transaction.add(R.id.fragment, fragment, tag);
  110. transaction.commit();
  111. }
  112. }
  113. /**
  114. * 删除Fragment
  115. *
  116. * @param tag
  117. */
  118. private void removeFragment(String tag) {
  119. android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
  120. Fragment fragment = manager.findFragmentByTag(tag);
  121. android.support.v4.app.FragmentTransaction transaction = manager.beginTransaction();
  122. transaction.remove(fragment);
  123. transaction.commit();
  124. }
  125. /**
  126. * 替换
  127. *
  128. * @param fragNum
  129. */
  130. private void replace(String fragNum) {
  131. Log.i(TAG, "运行到了1");
  132. Log.i(TAG, String.valueOf(1));
  133. if (fragNum.equals(String.valueOf(1))) {
  134. Log.i(TAG, "运行到了2");
  135. android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
  136. Fragment1_management fragment1 = new Fragment1_management();
  137. FragmentTransaction transaction = manager.beginTransaction();
  138. transaction.replace(R.id.fragment, fragment1);
  139. transaction.commit();
  140. } else if (fragNum.equals(String.valueOf(2))) {
  141. android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
  142. Fragment2_management fragment2 = new Fragment2_management();
  143. FragmentTransaction transaction = manager.beginTransaction();
  144. transaction.replace(R.id.fragment, fragment2);
  145. transaction.commit();
  146. }
  147. }
  148. @Override
  149. protected void onDestroy() {
  150. super.onDestroy();
  151. }
  152. }

三个方法而已。



五、然后我们再说说滑动选项卡:(以上注释掉的就是关于选项卡)

这个首先在main_activity中写一个Tablayout控件。


  1. <!--
  2. 1. app:tabIndicatorColor tab的指示符颜色
  3. 2. app:tabSelectedTextColor 选择tab的文本颜色
  4. 3. app:tabTextColor 普通tab字体颜色
  5. 4. app:tabMode 模式,可选fixed和scrollable fixed是指固定个数,scrollable是可以横行滚动的(逼格高)
  6. 5. app:tabGravity 对齐方式,可选fill和center
  7. -->

  1. <android.support.design.widget.TabLayout
  2. android:id="@+id/tabLayout"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. app:tabIndicatorColor="#990033"
  6. app:tabSelectedTextColor="#008b8b"
  7. app:tabMode="scrollable"
  8. app:tabGravity="fill"
  9. android:layout_below="@+id/buttonReplace"
  10. android:layout_alignRight="@+id/button_fra2"
  11. android:layout_alignEnd="@+id/button_fra2" />

就这么简单。



源码:http://download.csdn.net/detail/csdnhejingzhou/9480342

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/225314
推荐阅读