如何去从​​片段活动 [英] How to go to fragment from activity

查看:220
本文介绍了如何去从​​片段活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ActionBarSherlock

,我不能去,从活动来延长SherlockFragment类

我需要从活动转移到片段类

下面是我的Activity类

 意图notificationIntent =新意图(背景下,MessagesFragment.class);

和片断类是像

 公共类MessagesFragment扩展SherlockFragment工具
    OnItemClickListener {//布局参数声明
私人PullToRefreshListView lv_messages;
私人ImageView的iv_no_data;
私人的LinearLayout ll_bg;公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
        捆绑savedInstanceState){
    getSherlockActivity()。getSupportActionBar()。setDisplayOptions(
            ActionBar.DISPLAY_SHOW_CUSTOM);
    getSherlockActivity()。getSupportActionBar()。setDisplayHomeAsUpEnabled(
            真正);
    。getSherlockActivity()getSupportActionBar()setHomeButtonEnabled(真)。
    getSherlockActivity()。getSupportActionBar()。setDisplayShowHomeEnabled(
            真正);
    getSherlockActivity()。getSupportActionBar()。setCustomView(
            R.layout.header);
    getSherlockActivity()。getSupportActionBar()。setBackgroundDrawable(
            新ColorDrawable(Color.parseColor(#009fe3)));
    TextView的TXT =(TextView中)getActivity()。findViewById(
            R.id.tv_title_header);
    字体的字体= Typeface.createFromAsset(getActivity()。getAssets(),
            georgia.ttf);
    txt.setText(信息);
    txt.setTypeface(字体);
    返回inflater.inflate(R.layout.listview_refreshable,NULL);
}@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(真);
}




}

如果我用 switchfragment 方法,它显示了很多错误,在 FragmentChangeActivity

 私人无效switchFragment(片段片段){
    如果(getActivity()== NULL)
        返回;    如果(getActivity()的instanceof FragmentChangeActivity){
        FragmentChangeActivity FCA =(FragmentChangeActivity)getActivity();
        fca.switchContent(片段);    }
}


解决方案

您需要创建一个扩展FragmentActivity类,并开始yourfragment有

 公共类MessagesFragmentActivity扩展SherlockFragmentActivity {
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        如果(savedInstanceState == NULL){
            getSupportFragmentManager()调用BeginTransaction()
                    。新增(android.R.id.content,新MessagesFragment())提交();}
    }
}

您片段构造。

 公共YourFragment(){
}

然后从你打电话的活动,开始您的片段活动以正常方式

 意向书I =新意图(YourActivity.this,MessagesFragment.class);
startActivity(ⅰ);

I am using ActionBarSherlock, I am not able to go to class that extends SherlockFragment from activity

I need to move from Activity to fragment class

Here is my Activity class

Intent notificationIntent = new Intent(context,MessagesFragment.class);

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);
}
.
.
.
.
}

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

    }
}

解决方案

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

Your fragment constructor.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆