Fragment Basics

Fragment basics
Fragment
a .Fragment introduce
two .Fragment application
how to achieve three Fragment
four .Fragment the life cycle of
five .Fragment static display
six .Fragment dynamic display
seven .Fragment fallback stack
by value between eight .Fragment
way a: pass values between fragment: using EventBus
pass values between two .Fragment manner: Handler
three ways: by value between fragment: callback Interface
fragment
a .Fragment introduced
fragment, English meaning fragments that early in plate designing

public class Fragment1 extends Fragment {
    private View rootView;
    private GridView grid_1;
    private List<JavaBean.DataBean> data;
    private MyAdapter myAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.data = MainActivity.data;
        if (null == rootView) {
            rootView = inflater.inflate(R.layout.fragment1, container,
                    false);
            init(rootView);
        }
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (null != parent) {
            parent.removeView(rootView);
        }
        return rootView;
    }
    private void init(View rootView){
        grid_1 = rootView.findViewById(R.id.grid_1);
        myAdapter = new MyAdapter(data, getActivity());
        grid_1.setAdapter(myAdapter);
    }
}

Two .Fragment application

How to achieve three Fragment
Step 1: Create a New Fragment Fragment Object :( Right-click further to get)

(1) Define a class that inherits Fragment
(2). Method override the parent class onCreateView ()
(. 3). In onCreateView () method, to create the UI interface Fragment

Step 2: Display Fragment
(1) static display Fragment - layout page directly write

a. Activity in the corresponding page, by the introduction of the label
b. In the tag, the id attribute must be specified to uniquely identify Fragment of c is in the label, the name attribute must be used to identify the current display Fragment

(2) dynamic display Fragment - the code shown in Activity.java

a. Activity corresponding to the page, the layout required by the container occupying
b. Fragment obtained in the Activity Manager object code
c. to give Fragment transaction manager to add, remove, display, hide, commit the transaction replaced Fragment

Four .Fragment life cycle
1.onAttach (): Fragment associated with Activity.
2.onCreate (): create Fragment
3.onCreateView (): create Fragment view, try not to do time-consuming operation
4.onActivityCreated (): When the Activity onCreate method of execution after the call.
5.onStart (): Start.
6.onResume (): visible
7.onPause (): invisible
8.onStop (): Stop.
9. onDestroyView (): Destruction Fragment view
10.onDestroy (): Object Destruction fragment
11.onDetach (): When Fragment and Activity disassociate call

Five .Fragment static display
(1) Fragment Code: MyFragment.java

(2) static display: activity_main.xml

Six .Fragment dynamic display
(1) to create a plurality of classes Fragment: omitted here
(2) MainActivity Code: MainActivity.java

(3) activity_main.xml layout files:

Seven .Fragment fallback stack

Fragment2 f2 = new Fragment2();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction tx = fm.beginTransaction();
tx.replace(R.id.fl, f2);
//将当前的事务添加到了回退栈
tx.addToBackStack(null);
tx.commit();

By value between eight .Fragment

Way: by value between Fragment: Use EventBus
Example: fragment1 page has an edit box, fragment2 page has a button and an edit box, click on the button, and transmits the data to the fragment1 displayed in the fragment1.
Sender: fragment2
EventBus.getDefault () POST (data);.
Recipient: fragment1
the onCreate: Register
onDestory: deregistration
@subscribe defined method accepts data
transmission value between the two .Fragment manner: Handler
1. In a transmission in Fragment news

Message message = new Message();
 message.arg1 = 1;
 message.obj = "datas";
 Fragment2.handler.sendMessage(message);

2. Fragment receive messages in a further

public static Handler handler=new Handler(){
 public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.arg1==1){
String   data= msg.obj.toString();
}

Three ways: by value between Fragment: Callback Interface
1. defines the interface

public interface CallBackListener {
    void setData(String s);
}

2. Send the message in a Fragment

callBackListener.setData("添加数据");

3. Send Message Fragment in another

public class MyFragment extends Fragment implements CallBackListeners{
...
    public void setData(String data) {
      Toast.makeText(this,data, Toast.LENGTH_SHORT).show();          }
}

Guess you like

Origin blog.csdn.net/lizhuang_666/article/details/93135613