fragment的数据传输方式(一)

目前已知的fragment的数据传输方式有三种:

第一种

在活动内获取 fragment里面的控件信息,在活动中类似控件进行数据处理

案例需求:

  • 在同一个页面内,加载两个Fragment 视图,左边为listview ,右边为 textview ,点击左边listview 的条目,右边展示相关数据。

示例代码

1.1   在res->layout中定义xml
1.1.1 fragment1.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:background="#00ffff"
    android:orientation="vertical" >

    <ListView 
    android:id="@+id/fragment1_listview"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
    ></ListView>
</LinearLayout>

1.1.2 fragment2.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:background="#ff00ff"
    android:orientation="vertical" >

    <TextView
    android:id="@+id/fragment2_textview"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</LinearLayout>


1.1.3 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" >
    <fragment
    android:id="@+id/fragment1"
    android:name="com.example.fragment_tongxin.Fragment1"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1" />

    <fragment
    android:id="@+id/fragment2"
    android:name="com.example.fragment_tongxin.Fragment2"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="3" />
</LinearLayout>

1.2 在src 中

1.2.1 Fragment1类 

    public class Fragment1 extends Fragment {

        /*
         * 加载视图
         */
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View view=inflater.inflate(R.layout.fragment1, container, false);
            //找控件赋值
            return view;
        }
    }


1.2.2 Fragment2类 

    public class Fragment2 extends Fragment {
        /*
         * 加载视图
         */
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View view=inflater.inflate(R.layout.fragment2, container, false);
            return view;
        }
    }


1.2.3 在main_activity 类中实现

        public class MainActivity extends FragmentActivity {

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

                //找控件,赋值
            ListView fragment1_listview=(ListView) findViewById(R.id.fragment1_listview);

            fragment2_textview = (TextView) findViewById(R.id.fragment2_textview);

            //设置数据源 模拟数据

            ArrayList<String> list=new ArrayList<String>();
            for (int i = 0; i < 20; i++) {
                list.add("数据"+i);
            }

            //配置适配器
            fragment1_listview.setAdapter(new MyAdapter(list));

            //设置list条目点击事件

            fragment1_listview.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position,
                        long id) {


                    //数据处理  重点  
                    TextView f1_tv=(TextView) view;
                    fragment2_textview.setText(f1_tv.getText().toString());


                }
            });

            }
            class MyAdapter extends BaseAdapter{
                private ArrayList<String> list;

                public MyAdapter(ArrayList<String> list) {

                    this.list = list;
                }

                @Override
                public int getCount() {
                    // TODO Auto-generated method stub
                    return list.size();
                }

                @Override
                public Object getItem(int position) {
                    // TODO Auto-generated method stub
                    return list.get(position);
                }

                @Override
                public long getItemId(int position) {
                    // TODO Auto-generated method stub
                    return position;
                }

                @Override
                public View getView(int position, View convertView, ViewGroup parent) {

                    TextView textView=new TextView(MainActivity.this);
                    textView.setText(list.get(position));
                    return textView;
                }

            }


        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值