将活动转换为片段 [英] Converting an Activity to a Fragment

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

问题描述

我正在将活动"转换为片段.
我需要操纵活动"代码以作为片段运行的帮助.

I am converting my Activity to a Fragment.
I need help manipulating my Activity code to run as a Fragment.

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

I know I have to convert the onCreate() method to be onCreateView().
I don't know how to do so.

我的onCreate()代码...

My code for the 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;
}

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

How do I do this correctly?
I have tried different things, but I can't figure it out.

推荐答案

简要解决

我需要从活动变为片段

I need to change from activity to fragment


这就是我们要转换的布局.只是一个带有居中TextView的简单RelativeLayout.将活动转换为片段时,可以使用完全相同的布局.我将其命名为fragment_layout.xml.


Let this be the layout we want to convert. Just a simple RelativeLayout with a centered TextView. You can use the exact same layout when you convert the Activity to a Fragment. I named it fragment_layout.xml.

当然,您稍后将需要更改活动"的布局以包含片段",但这不是问题所在……

Of course, you will later need to change the Activity's layout to include the Fragment, but that was not the question...

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

这是我们要转换的活动.注意,setContentView加载了fragment_layout.xml文件,并使用findViewById捕获了TextView.

Here is an Activity that we are going to convert. Notice the setContentView loads the fragment_layout.xml file and it grab out that TextView using findViewById.

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);
    }

}

这是片段,其作用与上述活动完全相同.请注意,现在将inflate.inflatefragment_layout.xml文件一起使用以获取视图,以便使用rootView.findViewById捕获该TextView.

And here is the Fragment that will act the exact same as the Activity above. Notice, now using inflate.inflate with the fragment_layout.xml file to get the View in order to grab out that TextView using rootView.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;
    }
}

这篇关于将活动转换为片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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