随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)、QQ技术交流群(183198395)。
接上一文继续研究Fragment的使用,本文主要实现这样一个简单应用:左边显示标题栏,然后点击它,右边就显示不同的内容,内容根据需求自己作相应变化。代码如下:
MainActivity:
package com.home.testfragment;
import com.home.testfragment.TitleFragment.OnTitleSelectedListener;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.TextView;
public class MainActivity extends FragmentActivity implements
OnTitleSelectedListener {
public static String[] names = { "张三", "李四", "王五", "赵六", "蒋七", "小明", "小张",
"小刘", "小强" };
private TextView detailText;
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.main);
detailText = (TextView) findViewById(R.id.main_tv_detail);
}
@Override
public void OnShowDetails(int position) {
detailText.setText("我是" + names[position]);
}
}
TitleFragment:
package com.home.testfragment;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class TitleFragment extends ListFragment {
OnTitleSelectedListener mCallBack;
public interface OnTitleSelectedListener {
public void OnShowDetails(int position);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_title, container, false);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, MainActivity.names));
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallBack = (OnTitleSelectedListener) activity;
} catch (ClassCastException e) {
e.printStackTrace();
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
mCallBack.OnShowDetails(position);
}
}
main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<fragment
android:id="@+id/main_FirstFragment"
android:name="com.home.testfragment.TitleFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/main_tv_detail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_weight="0.5"
android:textSize="40sp" />
</LinearLayout>
fragment_title.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" >
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:drawSelectorOnTop="false" />
</LinearLayout>
附上图片效果: