[Android] p7-1 fragment study notes

This article from "android authority Programming Guide 3rd Edition" Chapter 7 UI fragment and fragment

Chapter 7 Contents APP is to achieve a record of bad behavior (partially implemented), there is a list of specific behaviors content display. Chapter 7, mainly to complete the individual behavior of UI setting details for understanding the process of initializing the fragment.


The following records at initialization and load some fragment of understanding (just stick some of the core code)

First, I introduced the xml file of the main activity of <FrameLayout> tag, just as a placeholder, accounting for a good space, id is fragment_container.

<FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fragment_container"
    ></FrameLayout>

The main activity OnCteate process is initialized

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentManager fm = this.getSupportFragmentManager();
        Fragment f = fm.findFragmentById(R.id.fragment_container);
        if(f == null){
            f = new CrimeFragment();
            fm.beginTransaction()
                    .add(R.id.fragment_container, f)
                    .commit();
        }
    }

To sum up like this:

  1. Get FragmentManager, calling Activity.getSupportFragmentManager () method available.
  2. Get findFragmentById (this method is equivalent to the activity in the findviewbyid).
  3. The fragment was then added FragmentManager in the Transaction.
  4. With key, fragment similar to the way Keep in Map FragmentManager in.

 Fragment of code in the OnCreate achieve initialization parameters:

public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.mCrime = new Crime();
    }

 

 Perform the functions OnCreateView method:

        View v = inflater.inflate(R.layout.fragment_crime, container, false);
        this.mTitleFiled = (EditText)v.findViewById(R.id.crime_title);
        this.mTitleFiled.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                /**
                 * 文本变化自动填充Crime类
                 */
                mCrime.setTitle(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        this.mDateButton = (Button)v.findViewById(R.id.crime_date);
        this.mDateButton.setText(MyDateUtil.getDateStr(mCrime.getDate(), null));
        this.mDateButton.setEnabled(false);
        this.mSolvedCheckBox = (CheckBox)v.findViewById(R.id.crime_solved);
        this.mSolvedCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                CrimeFragment.this.mCrime.setSolved(isChecked);
            }
        });

        return v;

 

View object is obtained, this is not the Activity, can not be directly invoked before obtaining View object, and then call the View object
View v = inflater.inflate(R.layout.fragment_crime, container, false);
Then, as in Activity initialize the various components and related Listener
this.mTitleFiled = (EditText)v.findViewById(R.id.crime_title);
this.mTitleFiled.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
/**
* 文本变化自动填充Crime类
*/
mCrime.setTitle(s.toString());
}

@Override
public void afterTextChanged(Editable s) {

}
});
Finally, the View returned for Activity use.
return v;

Guess you like

Origin www.cnblogs.com/aocshallo1/p/12462801.html