您的位置:首页 > 移动开发 > Android开发

android:ListFragment简单记录(12)

2016-01-23 00:00 561 查看
摘要: ListFragment 的好处是不用建立一个Listview,因为它内含有ListView的布局,直接绑定,程序功能的简单点击:点击一个按钮,显示出一个ListFragment,将ListFragment的内容给右边的Fragment显示出来

//功能:点击一个按钮,显示出一个ListFragment,将ListFragment的内容给右边的Fragment显示出来
//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) {
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.add(R.id.center, new ListFragmentDemo(), "ListFragment");
transaction.commit();

}
}

//继承的是ListFragment,,ListFragmentDemo中代码
public class ListFragmentDemo extends ListFragment {
private ArrayAdapter<String> adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<String> list = new ArrayList<String>();
for (int i = 0; i < 30; i++) {
list.add("mike嘿嘿嘿" + i);
}
adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, list);
setListAdapter(adapter);
// ListFragment 的好处是不用建立一个Listview,因为它内含有ListView的布局,直接绑定
}

// 而且也有监听事件
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// 传值给另外一个Fragment
// 注意这里不是getSupportFragmentManager()了,这个V4版本才有
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
ShowListFragment showListFragment = new ShowListFragment();
transaction.add(R.id.right, showListFragment, "ShowListFragment");
// 从ListFragment获取数据
// 这里需要用到适配器获取数据,最好定义全局
String name = adapter.getItem(position);
Bundle bundle = new Bundle();
bundle.putString("name", name);
showListFragment.setArguments(bundle);
transaction.commit();

}
}

public class ShowListFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Bundle bundle = getArguments();
String name = bundle.getString("name");
TextView textView=new TextView(getActivity());
textView.setText(name);
return textView;
}
}

//主布局文件
<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="vertical" >

<Button
android:id="@+id/bnt_show"
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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: