赞
踩
本来不该写tabLayout 因为他自己就有着相当大的知识点。
为了使用tabLayout控件,我们需要加入compile 'com.android.support:design:23.2.1' 老一套了
我们主要说一下Fragment。
这个Fragment 和 Activity周期相差无几,就用当初学习Activity生命周期的方式来刺探一下Fragment的生命周期。
贴一下日志你们就会明白;
关于生命周期就不多说了网上又说的特别好的。
前边当我写好侧滑的菜单时,我发现个问题,就是按下导航栏的条目后怎么切换到相应的layout中去,一个非常需要解决的问题,后来我用<include/>进行插入,然后就各种奇葩的问题不符合设计规则,然后昨天就帖子,找博客。大家们都一致推荐Fragment,当初学Android时只知道不过是一种布局算了,当初只写的是几个长宽不同的彩色方块放在一起重叠起来,平常没用过今天就插手干了起来。
1.首先是创建layout 这个也是我们最需要在乎的一部,因为我们终于知道将新的控件放在哪了。
2.我们需要创建一个新的类与这个layout一一对应,并且继承于Fragment。这类似与MainActivity 只是他继承与AppCompatActivity罢了。
3往main_actyvity.xml 中添加一个Fragment布局
4.在MainActivity中就是对Fragment的添加、移除、替换的操作。
贴一下主要的代码:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:background="#990033"
- android:layout_height="match_parent">
-
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Toast_Button_1"
- android:textAllCaps="false"
- android:id="@+id/button_tos_1"
- android:layout_centerVertical="true"
- android:layout_centerHorizontal="true" />
- </RelativeLayout>
- package com.example.hejingzhou.fragmentdemo;
-
- import android.content.Context;
- import android.support.v4.app.Fragment;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.Toast;
-
-
- /**
- * Created by Hejingzhou on 2016/4/3.
- */
- public class Fragment1_management extends Fragment implements View.OnClickListener{
- private String TAG = getClass().getSimpleName();
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- View fragment_1_view = inflater.inflate(R.layout.fragment1_layout,container,false);
- Button btnToast = (Button)fragment_1_view.findViewById(R.id.button_tos_1);
- btnToast.setOnClickListener(this);
- return fragment_1_view;
- }
-
- @Override
- public void onClick(View v) {
- if(v.getId() == R.id.button_tos_1){
- Toast.makeText(getActivity(), "Fragment_1", Toast.LENGTH_SHORT).show();
- }
- }
-
- /**
- * 以下和创建的操作文件并没有关系
- * 只是刺探Fragment的生命周期
- * @param context
- */
- @Override
- public void onAttach(Context context) {
- super.onAttach(context);
- Log.i(TAG,"onAttach");
- }
-
- @Override
- public void onViewCreated(View view, Bundle savedInstanceState) {
- super.onViewCreated(view, savedInstanceState);
- Log.i(TAG,"onViewCreated");
- }
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- Log.i(TAG,"onCreate");
- }
-
- @Override
- public void onActivityCreated(Bundle savedInstanceState) {
- super.onActivityCreated(savedInstanceState);
- Log.i(TAG,"onActivityCreated");
- }
-
- @Override
- public void onStart() {
- super.onStart();
- Log.i(TAG,"onStart");
- }
-
- @Override
- public void onDetach() {
- super.onDetach();
- Log.i(TAG,"onDetach");
- }
-
- @Override
- public void onResume() {
- super.onResume();
- Log.i(TAG,"onResume");
- }
-
- @Override
- public void onPause() {
- super.onPause();
- Log.i(TAG,"onPause");
- }
-
- @Override
- public void onStop() {
- super.onStop();
- Log.i(TAG,"onStop");
- }
-
- @Override
- public void onDestroyView() {
- super.onDestroyView();
- Log.i(TAG,"onDestroyView");
- }
- }
主要是这几个类:
- <span style="font-size:18px;color:#009900;"><span style="white-space:pre"> </span>android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
- Fragment fragment = manager.findFragmentByTag(tag);
- android.support.v4.app.FragmentTransaction transaction = manager.beginTransaction();</span>
- package com.example.hejingzhou.fragmentdemo;
-
-
- import android.support.design.widget.TabLayout;
- import android.support.v4.app.Fragment;
- import android.support.v4.app.FragmentTransaction;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
-
- public class MainActivity extends AppCompatActivity implements View.OnClickListener {
- private EditText editTextRemovenumber;
- private EditText editTextReplace;
- private String TAG = getClass().getCanonicalName();
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);;
- setContentView(R.layout.activity_main);
- findViewById();
- /**
- * 滑动选项卡
- */
- /* TabLayout tabLayout = (TabLayout)findViewById(R.id.tabLayout);
- Log.i(TAG,"tabLayout.getId()"+tabLayout.getId());//tabLayout.getId()2131492975
- Log.i(TAG,"tabLayout.getSelectedTabPosition()"+tabLayout.getSelectedTabPosition());//-1
- Log.i(TAG, "tabLayout.getTabCount()"+tabLayout.getTabCount());//0
- tabLayout.setAlpha(0.5f);
- tabLayout.addTab(tabLayout.newTab().setText("笑话"));
- tabLayout.addTab(tabLayout.newTab().setText("视频"));
- tabLayout.addTab(tabLayout.newTab().setText("狗血"));
- tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
- @Override
- public void onTabSelected(TabLayout.Tab tab) {
- if (tab.getText().equals("笑话")) {
- Fragment1_management fragment1 = new Fragment1_management();
- addFragment(fragment1, "1");
- } else if (tab.getText().equals("狗血")) {
- Fragment2_management fragment2_management = new Fragment2_management();
- addFragment(fragment2_management, "2");
- } else if (tab.getText().equals("视频")) {
- Toast.makeText(getApplicationContext(), "选择视频", Toast.LENGTH_SHORT).show();
- }
- }
- @Override
- public void onTabUnselected(TabLayout.Tab tab) {
- Log.i(TAG,"滑动选项卡未选择");
- }
- @Override
- public void onTabReselected(TabLayout.Tab tab) {
- Log.i(TAG,"滑动选项卡选择");
- }
- });*/
- }
- private void findViewById() {
- Button btnFrag_1 = (Button) findViewById(R.id.button_fra1);
- btnFrag_1.setOnClickListener(this);
- Button btnFrag_2 = (Button) findViewById(R.id.button_fra2);
- btnFrag_2.setOnClickListener(this);
- editTextRemovenumber = (EditText) findViewById(R.id.editView);
- Button remove = (Button) findViewById(R.id.remove);
- remove.setOnClickListener(this);
- Button btnReplace = (Button) findViewById(R.id.buttonReplace);
- btnReplace.setOnClickListener(this);
- editTextReplace = (EditText) findViewById(R.id.editTextReplace);
- }
-
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.button_fra1:
- Fragment1_management fragment1 = new Fragment1_management();
- addFragment(fragment1, "1");
- break;
- case R.id.button_fra2:
- Fragment2_management fragment2_management = new Fragment2_management();
- addFragment(fragment2_management, "2");
- break;
- case R.id.remove:
- Log.i(TAG,""+editTextRemovenumber.getText());
- if (editTextRemovenumber.getText() != null) {
- removeFragment(editTextRemovenumber.getText().toString());
- } else {
- Log.i(TAG,"确定editView为空,运行到这里还没出错");
- Toast.makeText(MainActivity.this, "请填写要删除的编号1或2", Toast.LENGTH_SHORT).show();
- }
- break;
- case R.id.buttonReplace:
- if (editTextReplace.getText() != null) {
- Log.i(TAG, "edit不为null");
- Log.i(TAG, editTextReplace.getText().toString());
- replace(editTextReplace.getText().toString());
- } else {
- Toast.makeText(MainActivity.this, "请填写要替换的号1或2", Toast.LENGTH_SHORT).show();
- }
- break;
- }
- }
- /**
- * 添加Fragment
- *
- * @param fragment
- * @param tag
- */
- private void addFragment(Fragment fragment, String tag) {
- if (fragment.isAdded()) {//不是这么做的 这样做并没效果
- } else {
- android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
- android.support.v4.app.FragmentTransaction transaction = manager.beginTransaction();
- transaction.add(R.id.fragment, fragment, tag);
- transaction.commit();
- }
- }
-
- /**
- * 删除Fragment
- *
- * @param tag
- */
- private void removeFragment(String tag) {
- android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
- Fragment fragment = manager.findFragmentByTag(tag);
- android.support.v4.app.FragmentTransaction transaction = manager.beginTransaction();
- transaction.remove(fragment);
- transaction.commit();
- }
-
- /**
- * 替换
- *
- * @param fragNum
- */
- private void replace(String fragNum) {
- Log.i(TAG, "运行到了1");
- Log.i(TAG, String.valueOf(1));
- if (fragNum.equals(String.valueOf(1))) {
- Log.i(TAG, "运行到了2");
- android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
- Fragment1_management fragment1 = new Fragment1_management();
- FragmentTransaction transaction = manager.beginTransaction();
- transaction.replace(R.id.fragment, fragment1);
- transaction.commit();
- } else if (fragNum.equals(String.valueOf(2))) {
- android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
- Fragment2_management fragment2 = new Fragment2_management();
- FragmentTransaction transaction = manager.beginTransaction();
- transaction.replace(R.id.fragment, fragment2);
- transaction.commit();
- }
- }
-
- @Override
- protected void onDestroy() {
- super.onDestroy();
- }
- }
这个首先在main_activity中写一个Tablayout控件。
- <!--
- 1. app:tabIndicatorColor tab的指示符颜色
- 2. app:tabSelectedTextColor 选择tab的文本颜色
- 3. app:tabTextColor 普通tab字体颜色
- 4. app:tabMode 模式,可选fixed和scrollable fixed是指固定个数,scrollable是可以横行滚动的(逼格高)
- 5. app:tabGravity 对齐方式,可选fill和center
- -->
- <android.support.design.widget.TabLayout
- android:id="@+id/tabLayout"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- app:tabIndicatorColor="#990033"
- app:tabSelectedTextColor="#008b8b"
- app:tabMode="scrollable"
- app:tabGravity="fill"
- android:layout_below="@+id/buttonReplace"
- android:layout_alignRight="@+id/button_fra2"
- android:layout_alignEnd="@+id/button_fra2" />
就这么简单。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。