静态添加的碎片中相互通信

来源:互联网 发布:网络女主播真的漂亮吗 编辑:程序博客网 时间:2024/06/07 11:59

在静态添加的碎片中通信要借助他们的宿主Activity,(插句题外话:在activity中findViewById()只能寻找到setContentView()加载的布局中的控件,不在里面的控件是找不到的

一个通信小例子,右边Fragment中包含一个Button,左边Fragment包含一个TextView,点击右边Fragment中的Button可以改变左边Fragment中文本中的文字,

右边Fragment中的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent"    >    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="replace"        android:id="@+id/button"/></LinearLayout>
右碎片
public class RightFragment extends Fragment {        private Button button;    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.right_fragment, container,false);        button =(Button) view.findViewById(R.id.button);        return view;    }    public void changeContent(final TextView getView){        button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                getView.setText("change by right");                Log.d("test", "button click");            }        });    }
左边碎片的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@android:color/holo_red_light">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/left1"        android:text="this is left fragment"/></LinearLayout>
左边碎片
public class leftFragment extends Fragment {    RightFragmentListener callBack;    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.left_fragment, container);        return view;    }}
宿主Activity
public class MyActivity extends FragmentActivity {    private TextView textView;    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.content);        //如果布局content中不包括控某件的话,用findViewById(R.id.left1)是找不到该控件的        /*TextView textView = (TextView) findViewById(R.id.left1);        textView.setText("aa");*/        TextView textView = (TextView) findViewById(R.id.left1);        RightFragment rightFragment = (RightFragment) getFragmentManager().findFragmentById(R.id.right);        rightFragment.changeContent(textView);    }

宿主中的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal" android:layout_width="match_parent"    android:layout_height="match_parent"    android:id="@+id/linearLayout">    <fragment        android:layout_width="0dp"        android:layout_height="match_parent"        android:name="com.example.li.fragmentui.RightFragment"        android:id="@+id/right"        android:layout_weight="1"/>    <FrameLayout        android:layout_width="0dp"        android:layout_height="match_parent"        android:id="@+id/framelayout"        android:layout_weight="1">        <fragment            android:layout_width="match_parent"            android:layout_height="match_parent"            android:id="@+id/left_fragment"            android:name="com.example.li.fragmentui.leftFragment" />    </FrameLayout></LinearLayout>

0 0
原创粉丝点击