Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

android - Converting an Activity to a Fragment

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

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

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

I need to use this code structure

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To briefly address the problem of

I need to change from activity to fragment


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>

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

}

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.

And OnCreateView needs to return that View from the inflater.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...