2016-03-03 76 views
1

我將我的活動轉換爲片段。
我需要幫助操縱我的活動代碼作爲片段運行。將活動轉換爲片段

我知道我必須將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); –

回答

1

簡要談談的

我需要從活動改變問題進行分段


讓這成爲我們要轉換的佈局。只需一個帶有居中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; 
    } 
}