如何从活动转到片段 [英] How to go to fragment from activity
本文介绍了如何从活动转到片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 ActionBarSherlock,我无法进入从活动扩展 SherlockFragment 的类
I am using ActionBarSherlock, I am not able to go to class that extends SherlockFragment from activity
我需要从 Activity 转移到片段类
I need to move from Activity to fragment class
这是我的活动课
Intent notificationIntent = new Intent(context,MessagesFragment.class);
Fragment 类就像
And the Fragment class is like
public class MessagesFragment extends SherlockFragment implements
OnItemClickListener {
// Layout parameters declaration
private PullToRefreshListView lv_messages;
private ImageView iv_no_data;
private LinearLayout ll_bg;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getSherlockActivity().getSupportActionBar().setDisplayOptions(
ActionBar.DISPLAY_SHOW_CUSTOM);
getSherlockActivity().getSupportActionBar().setDisplayHomeAsUpEnabled(
true);
getSherlockActivity().getSupportActionBar().setHomeButtonEnabled(true);
getSherlockActivity().getSupportActionBar().setDisplayShowHomeEnabled(
true);
getSherlockActivity().getSupportActionBar().setCustomView(
R.layout.header);
getSherlockActivity().getSupportActionBar().setBackgroundDrawable(
new ColorDrawable(Color.parseColor("#009fe3")));
TextView txt = (TextView) getActivity().findViewById(
R.id.tv_title_header);
Typeface font = Typeface.createFromAsset(getActivity().getAssets(),
"georgia.ttf");
txt.setText("MESSAGES");
txt.setTypeface(font);
return inflater.inflate(R.layout.listview_refreshable, null);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
.
.
.
.
}
如果我使用 switchfragment
方法,它会在 FragmentChangeActivity
If I use switchfragment
method it shows lots of errors in FragmentChangeActivity
private void switchFragment(Fragment fragment) {
if (getActivity() == null)
return;
if (getActivity() instanceof FragmentChangeActivity) {
FragmentChangeActivity fca = (FragmentChangeActivity) getActivity();
fca.switchContent(fragment);
}
}
推荐答案
你需要创建一个扩展 FragmentActivity 的类并在那里启动你的fragment
You need to created a class that extends FragmentActivity and start yourfragment there
public class MessagesFragmentActivity extends SherlockFragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null){
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, new MessagesFragment ()).commit();}
}
}
您的片段构造函数.
public YourFragment() {
}
然后从您调用活动开始,以正常方式启动您的片段活动
then from you calling activity, start your fragment activity in the normal way
Intent i = new Intent(YourActivity.this,MessagesFragment.class);
startActivity(i);
这篇关于如何从活动转到片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文