Fragment ChuanZhi

本文介绍了一个使用Android支持库中的Fragment实现左右布局数据交互的案例。通过定义回调接口,LeftFragment负责展示列表并传递选中项数据给RightFragment,RightFragment接收数据并显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//Fragment是静态注册

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }
    
}

//Activity的布局界面

<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/fragment_left"
        android:name="com.bwie.fragmenttofragment.LetfFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <fragment
        android:id="@+id/fragment_right"
        android:name="com.bwie.fragmenttofragment.RightFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />
</LinearLayout>

//LeftFragment界面

import java.util.ArrayList;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class LetfFragment extends Fragment {
    private View view;
    private ArrayList<String> list;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.activity_fragment_left, container,
                false);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ListView listView = (ListView) view.findViewById(R.id.listview);
        list = new ArrayList<String>();
        for (int i = 0; i < 20; i++) {
            list.add("这是第" + i + "条数据");
        }
        listView.setAdapter(new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, list));
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                String string = list.get(position);
                listener.onItemClick(string);
            }
        });

    }

    // 定义回调接口的步骤
    // 1. 定义一个接口OnListClickListener
    // 2.定义接口中的方法来将数据回传到需要接收数据的组件中onItemClick(String str)
    // 3.给外部提供一个设置回调接口的方法setListener(),参数就是回调接口的一个实例
    // 4.在需要回传数据的地方调用 外部 传来的接口实例的回调方法
    private OnListClickListener listener;

    public void setListener(OnListClickListener listener) {
        this.listener = listener;
    }

    interface OnListClickListener {
        void onItemClick(String str);
    }
}

//LeftFragment的布局文件

<?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/listview"
           android:layout_width="match_parent"
           android:layout_height="match_parent" >
       </ListView>
</LinearLayout>

//RightFragment的页面

import com.bwie.fragmenttofragment.LetfFragment.OnListClickListener;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class RightFragment extends Fragment {
    private View view;
    private TextView textView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.activity_fragment_right, container,false);
        return view;
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        textView = (TextView) view.findViewById(R.id.textview);
        LetfFragment letfFragment = (LetfFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.fragment_left);
        // 外部在需要设置回调接口的地方,设置回调接口的实例,然后在接口的回调方法中接收传回来的数据
        letfFragment.setListener(new OnListClickListener() {
            
            @Override
            public void onItemClick(String str) {
                textView.setText(str);
            }
        });
    }

}

//RightFragment的布局文件

<?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/textview"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:gravity="center" />
</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SuperMonsterH

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值