鸟语天空
列表碎片——ListFragment
post by:追风剑情 2016-2-21 18:59

fragment1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#00FF00" >
    
	<ListView 
	    android:id="@id/android:list"
	    android:layout_width="match_parent"
	    android:layout_height="match_parent"
	    android:layout_weight="1"
	    android:drawSelectorOnTop="false" />
</LinearLayout>

fragment2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#FFFE00" >
    
	<TextView 
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
	    android:text="This is fragment #2"
	    android:textColor="#000000"
	    android:textSize="25sp"/>

</LinearLayout>

activity_fragments.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    tools:context="${relativePackage}.${activityClass}" >

    <fragment
        android:name="com.example.androidtest.Fragment1"
        android:id="@+id/fragment1"
        android:layout_weight="1"
        android:layout_width="0px"
        android:layout_height="match_parent" />
    
     <fragment
        android:name="com.example.androidtest.Fragment2"
        android:id="@+id/fragment2"
        android:layout_weight="1"
        android:layout_width="0px"
        android:layout_height="match_parent" />

</LinearLayout>

Fragment1.java

package com.example.androidtest;

import android.app.Activity;
import android.app.Fragment;
import android.app.ListFragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class Fragment1 extends ListFragment{
	
	String[] presidents = {
		"Dwight D. Eisenhower",
		"John F. Kennedy",
		"Lyndon B. Johnson",
		"Richard Nixon",
		"Gerald Ford",
		"Jimmy Carter",
		"Ronald Reagan",
		"George H. W. Bush",
		"Bill Clinton",
		"George W. Bush",
		"Barack Obama"
	};
	
	@Override
	public View onCreateView(LayoutInflater inflater,
						ViewGroup container,
						Bundle savedInstanceState)
	{
		Log.d("Fragment1", "onCreateView");
		return inflater.inflate(R.layout.fragment1, container, false);
	}
	
	@Override
	public void onAttach(Activity activity)
	{
		super.onAttach(activity);
		Log.d("Fragment1", "onAttach");
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		Log.d("Fragment1", "onCreate");
		
		setListAdapter(new ArrayAdapter<String>(getActivity(),
				android.R.layout.simple_list_item_1, presidents));
	}
	
	public void onListItemClick(ListView parent, View v, int position, long id)
	{
		Toast.makeText(getActivity(), "You have selected " + presidents[position],
				Toast.LENGTH_SHORT).show();
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState)
	{
		super.onActivityCreated(savedInstanceState);
		Log.d("Fragment1", "onActivityCreated");
	}
	
	@Override
	public void onStart()
	{
		super.onStart();
		Log.d("Fragment1", "onStart");
	}
	
	@Override
	public void onResume()
	{
		super.onResume();
		Log.d("Fragment1", "onResume");
	}
	
	@Override
	public void onPause()
	{
		super.onPause();
		Log.d("Fragment1", "onPause");
	}
	
	@Override
	public void onStop()
	{
		super.onStop();
		Log.d("Fragment1", "onStop");
	}
	
	@Override
	public void onDestroyView()
	{
		super.onDestroyView();
		Log.d("Fragment1", "onDestroyView");
	}
	
	@Override
	public void onDetach()
	{
		super.onDetach();
		Log.d("Fragment1", "onDetach");
	}
}

Fragment2.java

package com.example.androidtest;

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

public class Fragment2 extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater,
						ViewGroup container,
						Bundle savedInstanceState)
	{
		return inflater.inflate(R.layout.fragment2, container, false);
	}
	
	@Override
	public void onStart()
	{
		super.onStart();
	}
}

FragmentsActivity.java

package com.example.androidtest;

import android.app.Activity;
import android.os.Bundle;

public class FragmentsActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_fragments);
	}
}

运行效果

11111111.png

评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容