我有一门课MainActivity
,我在其中实现标签+滑动导航。我正在为不同的碎片创建不同的 java 文件。但问题是我将无法访问MainActivity
类中变量的值。现在我已经将片段类放在MainActivity
类中,并且应用程序运行良好。如果要为单独的片段创建单独的 java 文件,如何实现相同的结果。这是我现在的fagment类:
public static class fragmentLeft extends Fragment{
public fragmentLeft(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_left,
container, false);
View.OnClickListener mStartListener = new OnClickListener() {
public void onClick(View v) {
//mChronometer.start();
if (v.getId() == R.id.sign_in_button && !mPlusClient.isConnected()) {
Toast.makeText(getActivity(), "sadf", Toast.LENGTH_LONG).show();
if (mConnectionResult == null) {
mConnectionProgressDialog.show();
} else {
try {
mConnectionResult.startResolutionForResult(getActivity(), REQUEST_CODE_RESOLVE_ERR);
} catch (SendIntentException e) {
// Try connecting again.
Toast.makeText(getActivity(),(CharSequence) e, Toast.LENGTH_LONG).show();
mConnectionResult = null;
mPlusClient.connect();
}
}
}
}
};
rootView.findViewById(R.id.sign_in_button).setOnClickListener(mStartListener);
return rootView;
}
}
MainActivity
这些是我想从片段类访问的类中存在的变量:
private static ProgressDialog mConnectionProgressDialog;
private static PlusClient mPlusClient;
private static ConnectionResult mConnectionResult;