【问题标题】:Can anyone explain me how the execution process runs in this java code for Fragments in Android Studio [closed]谁能解释我执行过程如何在Android Studio中片段的这个java代码中运行[关闭]
【发布时间】:2016-01-23 15:32:04
【问题描述】:

这段代码我是按照视频教程found here写的 关于Android Fragments,但我无法理解执行流程如何通过这些代码运行。与教程中一样,我使用 Main Activity 单独制作了两个片段。整个程序运行良好,但我无法理解执行流程是如何进行的。请好心解释一下。这是我的源代码,我是 真的很抱歉,因为它们太长了,我发布了我所有的代码。这是主要的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.java

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Button;
import android.app.Activity;
import java.util.ServiceConfigurationError;


public class top_layout_fragment extends Fragment {
    private static EditText firstText, secondText;
    TopSelectionListener activityCommand;


    public interface TopSelectionListener {

        public void createMeme(String top, String bottom);

    }

    @Override
    public void onAttach(Activity activity) {

        super.onAttach(activity);

        try {
            activityCommand = (TopSelectionListener) activity;
        }
        catch(ClassCastException e){
            throw new ClassCastException (activity.toString());
        }


    }




    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.top_layout_fragment, container, false);



        firstText = (EditText) view.findViewById(R.id.firstText);
        secondText = (EditText) view.findViewById(R.id.secondText);
        final Button button = (Button) view.findViewById(R.id.button);

        button.setOnClickListener(
                new View.OnClickListener() {
                    public void onClick(View v) {
                        buttonClicked(v);


                    }

                }


        );
        return view;  

    }

    public void buttonClicked(View view) {

        activityCommand.createMeme(firstText.getText().toString(), secondText.getText().toString());


    }
}

这是 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>

这是bottom_layout_fragment.java

package com.example.kasun.fragments;

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

public class bottom_layout_fragment extends Fragment {

    private TextView topText,bottomText;


    // need to overide the onCreateViewMethod
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view=inflater.inflate(R.layout.bottum_layout_fragment,container,false);
        topText=(TextView)view.findViewById(R.id.topText);
        bottomText=(TextView)view.findViewById(R.id.bottomText);

        return view;


    }


    public void setText(String topText,String bottomText){
        this.topText.setText(topText);
        this.bottomText.setText(bottomText);

    }
}

这是bottom_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"
    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>

【问题讨论】:

    标签: java android android-fragments android-studio


    【解决方案1】:

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

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

    activityCommand = (TopSelectionListener) activity;
    

    当点击顶部片段中的按钮时,它将调用activityCommand.createMeme(...),它会调用您活动中的createMeme 方法。

    createMeme 方法然后查找对底部 Fragment 的引用并在其上调用 setText 方法以更新它显示的文本。

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

    【讨论】:

    • @KasunSiyambalapitiya 你还有什么特别的问题吗?
    • 你能解释一下这条线是什么意思 activityCommand = (TopSelectionListener) activity;这个活动指的是什么,是指实现这个片段的任何活动还是这里存在的主要活动
    • 您的MainActivity 实现了TopSelectionListener 接口。在那里,您将活动投射到接口TopSelectionListener。这是必要的,因此您可以稍后在单击按钮 activityCommand.createMeme(...) 时在您的活动类上调用 createMeme(...) 方法。
    • 根据您对活动引用片段的宿主活动的评论的编辑。在这种情况下,它是MainActivity。
    • 这样对吗,宿主activity被强制转换为TopSelectionListener接口并赋值给activityCommand对象,所以当activityCommand.createMeme(...)被调用时,它直接调用到@987654341托管活动中的@方法
    猜你喜欢
    • 1970-01-01
    • 2011-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-16
    • 2018-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多