首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

使用Fragment实现简略的数据交互

2013-10-08 
使用Fragment实现简单的数据交互接上一文继续研究Fragment的使用,本文主要实现这样一个简单应用:左边显示

使用Fragment实现简单的数据交互

接上一文继续研究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 implementsOnTitleSelectedListener {public static String[] names = { "张三", "李四", "王五", "赵六", "蒋七", "小明", "小张","小刘", "小强" };private TextView detailText;@Overrideprotected void onCreate(Bundle arg0) {super.onCreate(arg0);setContentView(R.layout.main);detailText = (TextView) findViewById(R.id.main_tv_detail);}@Overridepublic 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);}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {return inflater.inflate(R.layout.fragment_title, container, false);}@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setListAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, MainActivity.names));}@Overridepublic void onAttach(Activity activity) {super.onAttach(activity);try {mCallBack = (OnTitleSelectedListener) activity;} catch (ClassCastException e) {e.printStackTrace();}}@Overridepublic 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>

附上图片效果:

使用Fragment实现简略的数据交互




 

 

热点排行