碎片到活动到碎片通信

时间:2016-07-11 12:16:36

标签: android android-fragments

我的main.xml中有两个Frame Layouts(Containers),它包含两个片段。现在我想将一个视频网址从FragmentOne传递给Activity,然后将Activity传递给FragmentTwo。我无法在第二个片段中获取网址。以下是我的代码,请帮我解决正确的问题。

activity_main.xml中

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:layout_width="match_parent"
        android:id="@+id/top"
        android:layout_height="350dp">

    </FrameLayout>

    <FrameLayout
        android:layout_width="340dp"
        android:layout_marginTop="20dp"
        android:id="@+id/frgten"
        android:layout_below="@+id/top"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="32dp"
        android:layout_height="130dp"/>


</RelativeLayout>

fragment_one.xml

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_marginTop="15dp"
        android:id="@+id/rl1"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="100dp"
            android:id="@+id/frg1"
            android:background="#c1a499"
            android:text="PLAY"
            android:textAlignment="center"
            android:layout_marginLeft="25dp"
            android:layout_height="100dp"
            android:autoLink="web">

        </TextView>

        <TextView
            android:layout_width="100dp"
            android:id="@+id/frg2"
            android:layout_marginLeft="10dp"
            android:background="#c1a499"
            android:text="PLAY"
            android:textAlignment="center"
            android:layout_toRightOf="@+id/frg1"
            android:layout_height="100dp"
            android:autoLink="web">

        </TextView>

        <TextView
            android:layout_width="100dp"
            android:id="@+id/frg3"
            android:background="#c1a499"
            android:text="PLAY"
            android:textAlignment="center"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@+id/frg2"
            android:layout_height="100dp"
            android:autoLink="web">

        </TextView>

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:id="@+id/rl2"
        android:layout_marginTop="15dp"
        android:layout_below="@+id/rl1"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="100dp"
            android:id="@+id/frg4"
            android:background="#c1a499"
            android:text="PLAY"
            android:textAlignment="center"
            android:layout_marginLeft="25dp"
            android:layout_height="100dp"
            android:autoLink="web">

        </TextView>

        <TextView
            android:layout_width="100dp"
            android:id="@+id/frg5"
            android:background="#c1a499"
            android:text="PLAY"
            android:textAlignment="center"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@+id/frg4"
            android:layout_height="100dp"
            android:autoLink="web">

        </TextView>

        <TextView
            android:layout_width="100dp"
            android:id="@+id/frg6"
            android:background="#c1a499"
            android:text="PLAY"
            android:textAlignment="center"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@+id/frg5"
            android:layout_height="100dp"
            android:autoLink="web">

        </TextView>

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:id="@+id/rl3"
        android:layout_below="@+id/rl2"
        android:layout_marginTop="15dp"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="100dp"
            android:id="@+id/frg7"
            android:background="#c1a499"
            android:text="PLAY"
            android:textAlignment="center"
            android:layout_marginLeft="25dp"
            android:layout_height="100dp"
            android:autoLink="web">

        </TextView>

        <TextView
            android:layout_width="100dp"
            android:id="@+id/frg8"
            android:background="#c1a499"
            android:text="PLAY"
            android:textAlignment="center"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@+id/frg7"
            android:layout_height="100dp"
            android:autoLink="web">

        </TextView>

        <TextView
            android:layout_width="100dp"
            android:id="@+id/frg9"
            android:background="#c1a499"
            android:text="PLAY"
            android:textAlignment="center"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@+id/frg8"
            android:layout_height="100dp"
            android:autoLink="web">

        </TextView>

    </RelativeLayout>

</RelativeLayout>

fragment_two.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <VideoView
        android:layout_width="match_parent"
        android:id="@+id/video"
        android:layout_height="match_parent" />

</LinearLayout>

MainActivity

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity implements FragmentOne.CommunicationChannel {

    private FragmentOne sendFragment=new FragmentOne();
    private FragmentTwo getFragment=new FragmentTwo();

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

        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.top,sendFragment);
        transaction.add(R.id.frgten,getFragment);
        transaction.commit();
    }

    @Override
    public void setCommunication(String url) {
        if(null != getFragment  && getFragment.isInLayout())
        {
            getFragment.storeRecievedUrl(url);
        }

    }
}

FragmentOne

import android.app.Activity;
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;


/**
 * A simple {@link Fragment} subclass.
 */
public class FragmentOne extends Fragment {

    private CommunicationChannel mCommChListner=null;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        final View view = inflater.inflate(R.layout.fragment_one, container, false);

        final TextView t1 = (TextView) view.findViewById(R.id.frg1);

        t1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String url="http://www.androidbegin.com/tutorial/AndroidCommercial.3gp";
                sendUrl(url);
            }
        });
        return view;
    }

    interface CommunicationChannel
    {
        public void setCommunication(String url);
    }
    @Override
    public void onAttach(Activity activity)
    {
        super.onAttach(activity);
        if(activity instanceof CommunicationChannel)
        {
            mCommChListner = (CommunicationChannel)activity;
        }
        else
        {
            throw new ClassCastException();
        }

    }

    public void sendUrl(String url){
          mCommChListner.setCommunication(url);
    }

}

FragmentTwo

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 */
public class FragmentTwo extends Fragment {


    public FragmentTwo() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        final View view= inflater.inflate(R.layout.fragment_two, container, false);


        return view;
    }

    void storeRecievedUrl(String url){
        String VideoURL = url;

    }

}

1 个答案:

答案 0 :(得分:0)

我没有尝试运行您的代码,但我认为prob是Activity中的setCommunication方法。

我不认为你应该如何获得片段的实例。如果你在文档here中的google示例中看到,你会看到你需要获得像这样的片段实例

@Override
    public void setCommunication(String url) {
        FragmentTwo  getFragmentInstance = (FragmentTwo )
                getSupportFragmentManager().findFragmentById(R.id.frgten);
        if(getFragmentInstance!=null)
        {
            getFragmentInstance.storeRecievedUrl(url);
        }

    }