您的位置:首页 > 其它

ListFragment的基本使用

2016-01-24 00:00 309 查看
摘要: ListFragment

ListFragment的基本使用

效果图示例



//2个布局一个一个主布局
//一个右边碎片的布局 -- 只有一个TextView控件

//3个类 一个MainActivigty类 -- 左边碎片类

// -- 继承FragmentActivity

//一个中间碎片类 -- 继承 ListFragment

//一个右边碎片类 -- 继承 Fragment

detail_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" >
<TextView
android:id="@+id/detailText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />

</LinearLayout>

----------------------

activity_main.xml布局文件

代码

<LinearLayout 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:orientation="horizontal" >

<LinearLayout
android:id="@+id/left"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#cccccc"
android:orientation="horizontal" >

<Button
android:id="@+id/button_showlist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="clickButton"
android:text="显示"
android:textSize="14sp" />
</LinearLayout>

<LinearLayout
android:id="@+id/center"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:background="#AFEEEE"
android:orientation="vertical" >
</LinearLayout>

<LinearLayout
android:id="@+id/right"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:background="#00FFFF"
android:orientation="vertical" >
</LinearLayout>

</LinearLayout>

==================

MainActivity 类

代码

public class MainActivity extends FragmentActivity {

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

//按钮的 事件 监听
public void clickButton(View view){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
//动态创建Fragment(碎片)
//Center_ListFragment类的onCreateView方法返回视图到
//该方法第一个参数的布局上
transaction.add(R.id.center, new Center_ListFragment(), "center_fragment");
transaction.commit();
}
}

--------------------------

中间碎片类

代码

public class Center_ListFragment extends ListFragment {

private List<String> list;
private ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//初始化listview数据
list = new ArrayList<String>();
for(int i = 0;i<20;i++){
list.add("O(∩_∩O哈" + i);
}
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,list);
//ListFragment适配器 -- ListFragment不用布局ListView视图了
setListAdapter(adapter );
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
//获取Fragment管理器
FragmentManager fragmentManager = getFragmentManager();
//通过Fragment管理器开启一个 事物
FragmentTransaction transaction = fragmentManager.beginTransaction();
//new一个Right_fragment类 该类的onCreateView方法 会返回一个View(视图)
Right_fragment rf = new Right_fragment();
//获取listview点中的内容
String item = adapter.getItem(position);
transaction.replace(R.id.right, rf);
// transaction.add(R.id.right, rf);
Bundle bundle = new Bundle();
bundle.putString("center", item);
//setArguments -- 继承Fragment类 有的方法
//该方法用来在个个Fragment(碎片)之间传递数据
rf.setArguments(bundle);
// Log.i("data","item:" + item);
//启动事物
transaction.commit();
}
}

==================

右边碎片类

代码

public class Right_fragment extends Fragment {
private TextView textview;
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.detail_fragment, container, false);
textview = (TextView) view.findViewById(R.id.detailText);
//Log.i("data", "bundle前...");
//利用getArguments方法接收数据
Bundle bundle = getArguments();
String data = bundle.getString("center");
textview.setText(data);
//Log.i("data", "sadfsafd:" + bundle.getString("center"));
return view;
}
}

================================

问题:

把replace换成add有问题 -- 不能一直增加点中的数据 但是把右边碎片类的代码中的固定的TextView视图布局

代码换成 每次都new一个TextView 就可以一直增加

这个有空再分析。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ListFragment