Android Introduction (VI) fragment

Original link: http://www.orlion.ga/493/

1、 Fragment

Fragment is a UI fragment that can be embedded in activities. It can make the program more reasonable and make full use of the space of the large screen, so it is widely used on tablets. Although fragment should be a new concept for you, I believe you should learn effortlessly, because it is so similar to activities. It can also contain layout and have its own life cycle. You can even think of fragments as a mini activity, although this mini activity may be as large as an ordinary activity.

So how to use debris to make full use of the space of flat screen? Imagine that we are developing a news application. One interface uses listview to display the titles of a group of news. When you click one of the titles, you will open another interface to display the details of the news. If it is designed in the mobile phone, we can put the news title list in one activity and the details of the news in another activity, as shown in the figure:

However, if it is also designed on the tablet, the list of news titles will be stretched to fill the screen of the tablet, and the news titles will not be too long, which will lead to a large number of blank areas on the interface,

Therefore, a better design scheme is to place the news title list interface and news details interface in two fragments respectively, and then introduce the two fragments in the same activity, so as to make full use of the screen space, as shown in the figure

2、 Experience fragment layout

First create an Android project, and then create the left fragment layout_ fragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
	<Button
	    android:id="@+id/button"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_gravity="center_horizontal"
	    android:text="Button"/>
</LinearLayout>

Then create the right fragment layout right_ fragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00ff00"
    android:orientation="vertical" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="20sp"
        android:text="This is right fragment"/>
</LinearLayout>

Next, create a leftfragment class, which inherits from fragment:

package ga.orlion.fragmentdemo;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class LeftFragment extends Fragment {

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

Here, only the oncreateview () method of fragment is rewritten, and then the left defined just now is changed in this method through the inflate () method of layoutinflator_ The fragment layout is loaded dynamically, and then a rightfragment is created. The code is as follows:

package ga.orlion.fragmentdemo;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class RightFragment extends Fragment {

	@Override
	public View onCreateView(LayoutInflater inflater, Bundle savedInstanceState) {
		
		View view = inflater.inflate(R.layout.right_fragment , false);
		return view;
	}
}

Then modify the activity_ main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
	
    <fragment 
        android:id="@+id/left_fragment"
        android:name="ga.orlion.fragmentdemo.LeftFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>
    
    <fragment 
        android:id="@+id/right_fragment"
        android:name="ga.orlion.fragmentdemo.RightFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>
</LinearLayout>

Operation results:

3、 Dynamically add fragments

Create another_ right_ Fragment.xml Code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffff00"
    android:orientation="vertical" >
    
	<TextView
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_gravity="center_horizontal"
	    android:textSize="20sp"
	    android:text="This is another right fragment"/>
	
</LinearLayout>

Then create anotherrightfragment:

package ga.orlion.fragmentdemo;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class AnotherRightFragment extends Fragment{

	@Override
	public View onCreateView(LayoutInflater inflater, Bundle savedInstanceState) {
		
		View view = inflater.inflate(R.layout.another_right_fragment , false);
		return view;
	}
}

The next step is to dynamically load the fragment into the activity. Modify activity_ main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
	
    <fragment 
        android:id="@+id/left_fragment"
        android:name="ga.orlion.fragmentdemo.LeftFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>
    
    <FrameLayout 
        android:id="@+id/right_layout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">
        
        <fragment
            android:id="@+id/right_fragment"
            android:name="ga.orlion.fragmentdemo.RightFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </FrameLayout>
</LinearLayout>

FrameLayout does not have any positioning, and all spaces are placed in the upper left corner of the layout, because only a fragment needs to be placed in the layout, so it is very suitable for FrameLayout. Then we will replace the contents in FrameLayout in the code to realize the function of dynamically adding fragments and modify the code in mainactivity, as shown below:

package ga.orlion.fragmentdemo;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button button = (Button) findViewById(R.id.button);
		button.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		
		switch (v.getId()) {
		case R.id.button:
			AnotherRightFragment fragment = new AnotherRightFragment();
			FragmentManager fragmentManager = getFragmentManager();
			FragmentTransaction transaction = fragmentManager.beginTransaction();
			transaction.replace(R.id.right_layout, fragment);
			transaction.commit();
			break;
		default:
			break;	
		}
	}
	
	
}

You can see that we registered a click event for the button in the fragment on the left, and then put the logic of dynamically adding fragments in the click event. Adding fragments dynamically is mainly divided into five steps:

1. Create a fragment instance to be added

2. Get the fragmentmanager and directly call the getfragmentmanager () method in the activity

3. Start a transaction by calling the begintransaction () method

4. Adding fragments to the container is generally implemented by the replace () method. The ID of the container and the fragments to be added need to be passed in

Slice instance.

5. Commit the transaction and call the commit () method to complete it.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>