实现如下效果
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal" >
<fragment
android:id="@+id/articleFragment"
android:name="com.example.fragmentdemo.IndexFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="@+id/contentFragment"
android:name="com.example.fragmentdemo.ContentFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" />
</LinearLayout>
content_fragment.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.fragmentdemo.MainActivity" >
<TextView
android:id="@+id/contentTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50sp" />
</RelativeLayout>
index_fragment.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.fragmentdemo.MainActivity" >
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/textView"
android:textSize="50sp"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</TextView>
ContentFragment .ja
package com.example.fragmentdemo;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@SuppressLint("NewApi")
public class ContentFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
// 第一个是resource ID,指明了当前的Fragment对应的资源文件;
// 第二个参数是父容器控件;
// 第三个布尔值参数表明是否连接该布局和其父容器控件,在这里的情况设置为false,
// 因为系统已经插入了这个布局到父控件,设置为true将会产生多余的一个View
// Group。
return inflater.inflate(R.layout.content_fragment, container, false);
}
@Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
IndexFragment.java
package com.example.fragmentdemo;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@SuppressLint("NewApi")
public class IndexFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
// 第一个是resource ID,指明了当前的Fragment对应的资源文件;
// 第二个参数是父容器控件;
// 第三个布尔值参数表明是否连接该布局和其父容器控件,在这里的情况设置为false,
// 因为系统已经插入了这个布局到父控件,设置为true将会产生多余的一个View
// Group。
return inflater.inflate(R.layout.index_fragment, container, false);
}
@Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
MainActivity.java
package com.example.fragmentdemo;
import java.util.ArrayList;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends FragmentActivity {
private ListView listView;
private TextView contentTxt;
private ArrayList<String> content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
contentTxt = (TextView) findViewById(R.id.contentTxt);
ArrayList<String> name = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
name.add(i + "");
}
content = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
content.add("" + i);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
MainActivity.this, R.layout.list_item, name);
listView.setAdapter(adapter);
listView.setOnItemClickListener(ListViewItemClickListener);
}
OnItemClickListener ListViewItemClickListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
contentTxt.setText("点击的数字是:" + content.get(position));
}
};
}