分离片段代码和MainActivity代码

时间:2014-12-30 10:22:41

标签: java android fragment

我不擅长Android,我正在学习阶段。 这是我的问答。 以下函数位于MainActivity.class

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)看起来如下;

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

在ReportsFragment类中创建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();

     }

}

请帮助我实现这一目标。我的动机是保持密码分离。

2 个答案:

答案 0 :(得分:1)

如果此片段尚未附加到任何活动,则从片段内部调用getFragmentManager()getActivity()将始终返回null。 Android中用于您正在尝试实现的正确模式是在片段中使用静态公共方法,如下所示:

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

答案 1 :(得分:0)

您可以在ReportsFragment类中创建公共静态方法,该方法将返回使用某些参数准备的片段的新实例。但是你应该只在你的Activity中使用FragmentManager。