有人能解释一下Android Studio中片段的java代码中执行过程是如何运行的吗

有人能解释一下Android Studio中片段的java代码中执行过程是如何运行的吗,java,android,android-fragments,android-studio,Java,Android,Android Fragments,Android Studio,我根据视频教程编写了这段代码 关于Android片段,但我无法理解执行流是如何通过这些代码运行的。正如在教程中一样,我在主要活动中单独制作了两个片段。整个程序运行良好,但我无法理解执行流程是如何进行的。请给我解释一下。这是我的源代码,我是 真的很抱歉,因为它们太长了,我发布了我所有的代码。这是主要的Activity.java import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import andr

我根据视频教程编写了这段代码 关于Android片段,但我无法理解执行流是如何通过这些代码运行的。正如在教程中一样,我在主要活动中单独制作了两个片段。整个程序运行良好,但我无法理解执行流程是如何进行的。请给我解释一下。这是我的源代码,我是 真的很抱歉,因为它们太长了,我发布了我所有的代码。这是主要的Activity.java

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity implements top_layout_fragment.TopSelectionListener {

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

    //overiding the method from the interface
    @Override
    public void createMeme(String top, String bottom) {


        bottom_layout_fragment bottomLayoutFragment = (bottom_layout_fragment) getSupportFragmentManager().findFragmentById(R.id.fragment2);
        bottomLayoutFragment.setText(top, bottom);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
这是activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <fragment
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:name="com.example.kasun.fragments.top_layout_fragment"
        android:id="@+id/fragment"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        tools:layout="@layout/top_layout_fragment" />

    <fragment
        android:layout_width="320dp"
        android:layout_height="320dp"
        android:name="com.example.kasun.fragments.bottom_layout_fragment"
        android:id="@+id/fragment2"
        tools:layout="@layout/bottum_layout_fragment"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>
这是top_layout_fragment.xml

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


    <EditText
        android:id="@+id/firstText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="320dp"
        android:layout_marginTop="20dp"
        android:layout_centerHorizontal="true"
        />
    <EditText
        android:id="@+id/secondText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="320dp"
        android:layout_marginTop="20dp"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/firstText"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/secondText"
        android:layout_marginTop="20dp"
        android:text="@string/button_Texg"
        android:id="@+id/button" />


</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="@drawable/image1">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/topText"
        android:layout_marginTop="34dp"
        android:textColor="#FFF"
        android:gravity="center_horizontal"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/bottomText"
        android:layout_alignParentBottom="true"
        android:layout_alignLeft="@+id/topText"
        android:layout_alignStart="@+id/topText"
        android:layout_marginBottom="35dp"
        android:textColor="#FFF"
        android:gravity="center_horizontal" />
</RelativeLayout>
这是底部的_layout _fragment.xml

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


    <EditText
        android:id="@+id/firstText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="320dp"
        android:layout_marginTop="20dp"
        android:layout_centerHorizontal="true"
        />
    <EditText
        android:id="@+id/secondText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="320dp"
        android:layout_marginTop="20dp"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/firstText"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/secondText"
        android:layout_marginTop="20dp"
        android:text="@string/button_Texg"
        android:id="@+id/button" />


</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="@drawable/image1">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/topText"
        android:layout_marginTop="34dp"
        android:textColor="#FFF"
        android:gravity="center_horizontal"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/bottomText"
        android:layout_alignParentBottom="true"
        android:layout_alignLeft="@+id/topText"
        android:layout_alignStart="@+id/topText"
        android:layout_marginBottom="35dp"
        android:textColor="#FFF"
        android:gravity="center_horizontal" />
</RelativeLayout>

这是为了让一个活动显示两个片段而设置的。您可以看到两个片段都包含在活动布局文件中

顶部片段需要与底部片段通信以更新其文本,并通过名为
TopSelectionListener
的接口进行通信。您将注意到您的活动实现了该接口。另一个重要部分是顶部片段保留了对使用接口的活动的引用。您可以在顶部片段的
onAttach
方法中看到这一点。显示此片段的活动必须实现
TopSelectionListener
接口,否则您将得到
ClassCastException
,应用程序将崩溃

activityCommand = (TopSelectionListener) activity;
单击顶部片段中的按钮时,它将调用
activityCommand.createMeme(…)
,它将调用活动中的
createMeme
方法

然后,
createMeme
方法查找对底部片段的引用,并对其调用
setText
方法来更新显示的文本


还有另一个例子,这在Android中是如何工作的。

@kasuniyambalapitiya您还有其他特别的问题吗?您能解释一下这一行的意思是什么吗activityCommand=(TopSelectionListener)activity;此活动指的是什么,它指的是实现此片段的任何活动还是此处存在的主活动您的
MainActivity
实现了
TopSelectionListener
界面。在这里,您正在将活动强制转换到界面
TopSelectionListener
。这是必需的,这样您以后可以在单击按钮时调用activityCommand.createMeme(…)activity类上的
createMeme(…)
方法。根据对注释的编辑,activity引用片段的宿主活动。在本例中,它是
MainActivity
。这是否正确,宿主
activity
被强制转换到
TopSelectionListener
接口,并被分配给
activityCommand
对象,因此当调用
activityCommand.createMeme(…)
时,它直接调用
createMeme(…)
托管活动中的方法