在Android的片段,片段通信 [英] Fragment-Fragment communication in Android

查看:76
本文介绍了在Android的片段,片段通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android的编程初学者的水平,所以我需要你的真诚帮助了这一点。任何人都请帮助我。

I am at beginner level in Android programming, so I need your sincere help for this. Anyone help me please.

我想建立使用碎片滑动UI

I am trying to build a SLIDING UI using fragments

所以我实际的疑问是......我有一个片段(比如片段 A),它有一个的TextView 它和另一个片段(比如<按钮 code>片段 B)。它有一个的TextView 在里面。我需要显示片段A 的TextView 的在片段 B的的TextView ,在片段答:当我pressed的按钮 我尝试过很多办法,可惜我没得到正确的输出。 我相信你的人民知道。请帮我。

so my actual doubt is ... I have a Fragment (say Fragment A) it has a TextView and Button in it and another Fragment (say Fragment B). It has a TextView in it. I need to show the Fragment A's TextView's content in Fragment B's TextView, when I pressed the Button in FRAGMENT A. I tried many ways, unfortunately I didn't got the proper output. I am sure you peoples know about it. Please help me.

感谢您

推荐答案

它应该做的想监听,因此碎片仍然没有彼此依赖,并且可以在一个或两个窗格模式下使用。活动应该处理两个片段的听众。

It should be done thought listener, so Fragments are still not depend on each other and can be used in one or two pane mode. Activity should handle listeners of both fragments.

下面是活动的两个片段的例子:

Here is an example of activity with two Fragments:

package com.example;

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

import com.example.fragment.FragmentA;
import com.example.fragment.FragmentA.TextChangeListener;
import com.example.fragment.FragmentB;

public class ActivityAB extends FragmentActivity {

    FragmentA fragmentA;
    FragmentB fragmentB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ab);

        FragmentManager manager = getSupportFragmentManager();
        fragmentA = (FragmentA) manager.findFragmentById(R.id.fragmentA);
        fragmentB = (FragmentB) manager.findFragmentById(R.id.fragmentB);

        fragmentA.setTextChangeListener(new TextChangeListener() {

            @Override
            public void onTextChange(CharSequence newText) {
                fragmentB.updateTextValue(newText);
            }
        });
    }

}

下面是片段A,具有自定义监听文本改变事件。

Here is Fragment A, that has custom listener for text change event.

package com.example.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.example.R;

public class FragmentA extends Fragment {

    TextChangeListener listener;

    public interface TextChangeListener {
        public void onTextChange(CharSequence newText);
    }

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

        Button btn = (Button) view.findViewById(R.id.button1);
        final TextView textView = (TextView) view.findViewById(R.id.textView1);

        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (null != listener) {
                    listener.onTextChange(textView.getText());
                }

            }
        });
        return view;
    }

    public void setTextChangeListener(TextChangeListener listener) {
        this.listener = listener;
    }
}

下面是片段B,有公共的方法来更新文本字段:

Here is Fragment B that has public method to update text field:

package com.example.fragment;

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;

import com.example.R;

public class FragmentB extends Fragment {

    TextView textView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_b, container, false);
        textView = (TextView) view.findViewById(R.id.textView1);
        return view;
    }

    public void updateTextValue(CharSequence newText) {
        textView.setText(newText);
    }
}

ActivityAB XML布局:

ActivityAB xml Layout:

<?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:baselineAligned="false"
    android:orientation="horizontal" >

    <fragment
        android:id="@+id/fragmentA"
        android:name="com.example.fragment.FragmentA"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <fragment
        android:id="@+id/fragmentB"
        android:name="com.example.fragment.FragmentB"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

</LinearLayout>

片段的XML布局:

Fragment A xml layout:

<?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/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show" />

</LinearLayout>

片段B XML布局:

Fragment B xml layout:

<?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/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="(here will be text)"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

这篇关于在Android的片段,片段通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆