Android Note 4 -- Fragment usage

Source: Internet
Author: User

Description 1:We created a project in the previous method. If the latest ADT is used, the Minimum Android SDK selects android2. * or 1. *. A compatible project is created by default ,--Appcompat_v7: the project cannot be deleted, and an error is returned when you delete the project ..

NOTE 2:After the project is created, it is found that the layout folder contains two default files: adtivity and fragment:


1. Fragment Overview:

Fragment refers to Fragment and Fragment. To put it bluntly, it refers to a module, so I don't have to say much about it ..
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + kernel + mzybK/t9ajuzwvcD4KPHA + kernel/IztLDx8/Iv7TSu8/CztLDx9KqtO + kernel = "http://www.2cto.com/uploadfile/Collfiles/20140529/2014052909233527.jpg" alt = "\">


2. fragment_main.xml file:

The content is as follows:

     
      
      
  
 
Two buttons and a text display are defined in fragment, and a text display is also defined in activity. Click different fragment buttons to control the text display.

The content of activity_main.xml is as follows:

     
          
      
      
 

3. Create MainFragment. java class:

Package com. javen. xresource. activity. fragment; import android. app. fragment; import android. OS. bundle; import android. view. layoutInflater; import android. view. view; import android. view. view. onClickListener; import android. view. viewGroup; import android. widget. button; import android. widget. textView; import com. javen. xresource. activity. r; public class MainFragment extends Fragment {private TextView fragmentTextView; private Button button; public MainFragment () {}@ Overridepublic View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View rootView = inflater. inflate (R. layout. fragment_main, container, false); // use fragment_main.xml to initialize Fragmentbutton = (Button) rootView. findViewById (R. id. btn); // get the ButtonfragmentTextView = (TextView) rootView in Fragment. findViewById (R. id. ftxv); // get TextViewbutton in Fragment. setOnClickListener (new OnClickListener () {// listener event @ Overridepublic void onClick (View v) {fragmentTextView. setText ("I Have Been clicked by fragment... ") ;}}); return rootView ;}}

Now Fragment is ready...

4. MainActivity. java

The Code is as follows:

Package com. javen. xresource. activity; import android. app. activity; import android. app. fragment; import android. OS. bundle; import android. view. menu; import android. view. menuItem; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. textView; public class MainActivity extends Activity {private Button button; private TextView activityTextView; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);/* if (savedInstanceState = null) {getFragmentManager (). beginTransaction (). add (R. id. container, new MainFragment ()). commit (); // the java code Method Embedding Fragment in the activity, same as the preceding xml method} */activityTextView = (TextView) findViewById (R. id. atxv); // get TextViewFragment fragment = getFragmentManager () in the activity (). findFragmentById (R. id. mft); // get the fragmentbutton = (Button) fragment in the activity. getView (). findViewById (R. id. btn2); // get the Button2button in fragment. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {activityTextView. setText ("I Have Been clicked by activity... ") ;}}) ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {getMenuInflater (). inflate (R. menu. main, menu); return true ;}@ Overridepublic boolean onOptionsItemSelected (MenuItem item) {int id = item. getItemId (); if (id = R. id. action_settings) {return true;} return super. onOptionsItemSelected (item );}}

Run the project...

6. The question is: Can I change the TextView operation in the activity by using Button2 in Fragment? --> Extended: Can Fragment communicate with Activity?

After derivative issues, communication is certainly possible.

All operations on TextView are stored in Fragment... I tried to use:

getActivity().findViewById(R.id.atxv);

The View element in the Activity cannot be obtained. The return value of this statement is null. So The Button2 operation must be placed inActivity.

Why? You are welcome to comment on this issue...





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.