将活动转换为片段

问题描述:

我将我的活动转换为片段。
我需要帮助操纵我的活动代码作为片段运行。将活动转换为片段

我知道我必须将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; 
} 

如何正确地做到这一点?
我尝试过不同的事情,但我无法弄清楚。

+0

你不明白吗? –

+0

我的理解是,我需要将onCreate转换为onCreateView,但问题在于每次查看onCreate,然后在onCreateView示例中,我对如何开始转换它感到困惑。 – Ducky

+0

通过视图替换setContentView v = inflater.inflate(R.layout.fragment_bug_list,container,false); –

简要谈谈的

我需要从活动改变问题进行分段


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

当然,稍后您将需要更改活动的布局,包括片段,但这不是问题...

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

这里是我们要转换的活动。请注意0​​加载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); 
    } 

} 

这里是与上述活动完全相同的片段。注意,现在使用inflate.inflatefragment_layout.xml文件来获取视图,以便使用rootView.findViewById来抓取TextView。需要从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; 
    } 
}