Demo1Fragment.java 3.1 KB
Newer Older
B
Blankj 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
package com.blankj.androidutilcode.fragment;

import android.graphics.Color;
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;

import com.blankj.androidutilcode.R;
import com.blankj.androidutilcode.activity.FragmentActivity;
import com.blankj.utilcode.utils.FragmentUtils;

import java.util.Random;

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 17/02/02
 *     desc  :
 * </pre>
 */
public class Demo1Fragment extends Fragment
B
Blankj 已提交
27
        implements View.OnClickListener{
B
Blankj 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

    public static Demo1Fragment newInstance() {

        Bundle args = new Bundle();

        Demo1Fragment fragment = new Demo1Fragment();
        fragment.setArguments(args);
        return fragment;
    }

    private TextView tvAboutFragment;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_demo1, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
B
Blankj 已提交
48
        view.findViewById(R.id.btn_show_about_fragment).setOnClickListener(this);
B
Blankj 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
        view.findViewById(R.id.btn_hide_show).setOnClickListener(this);
        tvAboutFragment = (TextView) view.findViewById(R.id.tv_about_fragment);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        Random random = new Random();
        FragmentUtils.setBackgroundColor(this, Color.rgb(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onClick(View view) {
        tvAboutFragment.setText("");
        switch (view.getId()) {
B
Blankj 已提交
64 65 66 67 68 69 70
            case R.id.btn_show_about_fragment:
                tvAboutFragment.setText("---all fragments---\n"
                        + FragmentUtils.getAllFragments(getFragmentManager()).toString()
                        + "\n-------------------\n\n"
                        + "---stack top---\n"
                        + FragmentUtils.getAllFragmentsInStack(getFragmentManager()).toString()
                        + "\n---stack bottom---\n\n"
B
Blankj 已提交
71
                        + "\ntopFragment: " + FragmentUtils.getLastAddFragment(getFragmentManager()).getClass().getSimpleName()
B
Blankj 已提交
72 73
                        + "\ntopShowFragment: " + FragmentUtils.getTopShowFragment(getFragmentManager()).getClass().getSimpleName()
                );
B
Blankj 已提交
74 75
                break;
            case R.id.btn_hide_show:
B
Blankj 已提交
76
                FragmentUtils.hideShowFragment(this, ((FragmentActivity) getActivity()).rootFragment);
B
Blankj 已提交
77 78 79
                break;
        }
    }
B
Blankj 已提交
80 81 82 83 84 85 86 87

//    @Override
//    public boolean onBackClick() {
//        LogUtils.d("onBackClick");
//        FragmentUtils.showFragment(((FragmentActivity) getActivity()).rootFragment);
//        FragmentUtils.popFragment(getFragmentManager());
//        return true;
//    }
B
Blankj 已提交
88
}