android - 将 Activity 转换为 fragment

标签 android android-fragments

我正在将我的 Activity 转换为 Fragment。
我需要帮助来处理我的 Activity 代码以作为 fragment 运行。

我知道我必须将 onCreate() 方法转换为 onCreateView()
我不知道该怎么做。

我的 onCreate() 代码...

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_author_fact);
        //to eget reference to TextView for the fact
        mAuthorFactTextView = (TextView) findViewById(R.id.authorFactTextView);
    //Extract the resource id for the fact from the intent
    //If none is provided, display the "fact missing" message
    int authorFact = getIntent().getIntExtra(QuoteFragment.EXTRA_AUTHOR_FACT,
            R.string.fact_error);
    // put the fact string in the fact TextView
    mAuthorFactTextView.setText(authorFact);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

我需要使用这个代码结构

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

        //Setup floating action button to add a new bug
        final View addButton = v.findViewById(R.id.add_button);
    addButton.setOnClickListener(new View.OnClickListener(){
        @Override
    public void onClick(View view){
            addBug();
        }
    });
    return v;
}

如何正确执行此操作?
我尝试了不同的方法,但我无法弄清楚。

最佳答案

简单的解决

的问题

I need to change from activity to fragment


让它成为我们要转换的布局。只是一个带有居中 TextView 的简单 RelativeLayout。当您将 Activity 转换为 Fragment 时,您可以使用完全相同的布局。我将其命名为 fragment_layout.xml

当然,您稍后需要更改 Activity 的布局以包含 Fragment,但这不是问题...

<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">

    <TextView
            android:id="@+id/textView"
            android:text="Hello World"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"/>

</RelativeLayout>

这是我们要转换的 Activity 。请注意,setContentView 加载了 fragment_layout.xml 文件,并使用 findViewById 抓取了 TextView

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView textView;

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

        textView = (TextView) findViewById(R.id.textView);
    }

}

这里是 Fragment,其行为与上面的 Activity 完全相同。注意,现在使用 inflate.inflatefragment_layout.xml 文件来获取 View ,以便使用 rootView 获取 TextView .findViewById

并且 OnCreateView 需要从 inflater 返回那个 View

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;

public class MainFragment extends Fragment {

    private TextView textView;

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

        textView = (TextView) rootView.findViewById(R.id.textView);

        return rootView;
    }
}

关于android - 将 Activity 转换为 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35785049/

相关文章:

java - Android Studio - 从图库中导入图像

Android 2.2 + SQLite + 定义参照完整性约束

android - 在第一个 fragment 上调用第二个 fragment (选项卡)的 onCreateView

android - 如何在工具栏中为不同的 fragment 设置不同的菜单?

android - 在数据 fragment 之间传递数据

android - 如何在运行时使用 fragment 动态设置操作栏中可滑动选项卡的数量

java - 如何解析多部分/表单数据?

java - 在 Android 应用程序中播放视频时出错

android - 防止布局溢出超过屏幕尺寸

android - fragment customAnimation 在 Lollipop 中不起作用