靜態添加的碎片中相互通信

在靜態添加的碎片中通信要藉助他們的宿主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>

發佈了32 篇原創文章 · 獲贊 4 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章