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 유지하는 것입니다.