2014-12-30 1 views
0

Android가 좋지 않아서 학습 단계에 있습니다. 여기 내 의견이 있습니다. 다음 함수는 MainActivity.class조각 코드와 MainActivity 코드를 구분합니다.

public void callReportsFragment(int position) { 
    ReportsFragment cFragment = new ReportsFragment(); 
    Bundle data = new Bundle(); 
    data.putInt("position", position); 

    // Setting the position to the fragment 
    cFragment.setArguments(data); 
    // 
    FragmentManager fragmentManager = getFragmentManager(); 

    // Creating a fragment transaction 
    FragmentTransaction ft = fragmentManager.beginTransaction(); 

    // Adding a fragment to the fragment transaction 
    ft.replace(R.id.content_frame, cFragment); 

    // Committing the transaction 
    ft.commit(); 

} 

아래 그리고 다음 내 ReportsFragment 클래스입니다.

package com.example.reports; 

import android.os.Bundle; 

import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 
import android.app.Fragment; 




public class ReportsFragment extends Fragment{ 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) { 

     // Retrieving the currently selected item number 
     int position = getArguments().getInt("position"); 

     // List of option 
     String[] options = getResources().getStringArray(R.array.sidebar1); 

     // Creating view correspoding to the fragment 
     View v = inflater.inflate(R.layout.fragment_layout, container, false); 

     // Getting reference to the TextView of the Fragment 
     TextView tv = (TextView) v.findViewById(R.id.tv_content); 

     // Setting currently selected option name in the TextView 
     tv.setText(options[position]); 

     return v; 
    } 
} 

응용 프로그램이 정상적으로 작동합니다. 하지만 내가 원하는 것은 callReportsFragment(int position)가 다음과 같이 보이도록하려는 것입니다. 저 같이 따라 ReportsFragment 클래스에서

public void callReportsFragment(int position) { 
    ReportsFragment cFragment = new ReportsFragment(); 
    cFragment.fetchReportView(position); 
} 

그리고 만들 fetchReportView 방법.

public class ReportsFragment extends Fragment { 


    @ 
    Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

     // Retrieving the currently selected item number 
     int position = getArguments().getInt("position"); 

     // List of option 
     String[] options = getResources().getStringArray(R.array.sidebar1); 

     // Creating view correspoding to the fragment 
     View v = inflater.inflate(R.layout.fragment_layout, container, false); 

     // Getting reference to the TextView of the Fragment 
     TextView tv = (TextView) v.findViewById(R.id.tv_content); 

     // Setting currently selected option name in the TextView 
     tv.setText(options[position]); 

     return v; 
    } 

    public void fetchReportView(int pos) { 

     Bundle data = new Bundle(); 
     data.putInt("position", pos); 


     this.setArguments(data); 

     FragmentManager fragmentManager = getFragmentManager(); 


     FragmentTransaction ft = fragmentManager.beginTransaction(); 

     // Adding a fragment to the fragment transaction 
     ft.replace(R.id.content_frame, this); 

     // Committing the transaction 
     ft.commit(); 

    } 

} 

이 작업을 도와주세요. 내 동기는 코드를 seprated 유지하는 것입니다.

답변

0

특정 매개 변수로 준비된 조각의 새 인스턴스를 반환하는 public static 메서드를 ReportsFragment 클래스에 만들 수 있습니다. 그러나 액티비티에서만 FragmentManager를 사용해야합니다.

1

조각 내에서 getFragmentManager() 또는 getActivity()을 호출하면이 조각이 아직 활동에 연결되어 있지 않은 경우 항상 null이 반환됩니다. 다음

public static ReportsFragment newInstance(int position) { 
    Bundle data = new Bundle(); 
    data.putInt("position", position); 
    ReportsFragment fragment = new ReportsFragment(); 
    fragment.setArguments(data); 
    return fragment; 
} 

그리고 당신의 호스팅 활동 호출에서 : getFragmentManager().beginTransaction().replace(R.id.content_frame, ReportsFragment.newInstance(position)).commit();

당신이 달성하려고하는 것에 대해 안드로이드에서 사용되는 정확한 패턴과 같이 조각에 정적 공개 방법을 가지고있다
관련 문제