2 lazyyoun LazyYoun 于 2016.04.15 19:15 提问

使用viewpager,fragment底栏导航,新的fragment替换旧的fragment 1C

主activity
public class MyActivity extends FragmentActivity {

private ViewPager mViewPager;
private IconTabPageIndicator mIndicator;
List<BaseFragment> fragments;
Button LoginButton;
LayoutInflater inflater;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bmob.initialize(this, "d77a8e1ebfd8e187cbc814145918626d");
    //requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_my);

    mViewPager = (ViewPager) findViewById(R.id.view_pager);
    mIndicator = (IconTabPageIndicator) findViewById(R.id.indicator);
    fragments =new ArrayList<BaseFragment>();

    BaseFragment planFragment = new BaseFragment();
    planFragment.setTitle("计划");
    planFragment.setIconId(R.drawable.tab_plan_selector);


    //BaseFragment writeFragment = new BaseFragment();
    Fragment01 writeFragment=new Fragment01();
    writeFragment.setTitle("记账");
    writeFragment.setIconId(R.drawable.tab_write_selector);


    Fragment02 recordFragment = new Fragment02();
    recordFragment.setTitle("账单");
    recordFragment.setIconId(R.drawable.tab_record_selector);


    Fragment03 userFragment = new Fragment03();
    userFragment.setTitle("用户");
    userFragment.setIconId(R.drawable.tab_user_selector);

    fragments.add(writeFragment);
    fragments.add(planFragment);
    fragments.add(recordFragment);
    fragments.add(userFragment);

    FragmentAdapter adapter = new FragmentAdapter(fragments,getSupportFragmentManager());

    mViewPager.setAdapter(adapter);
    mIndicator.setViewPager(mViewPager);
}


**fragment03.java**
fragment03中有一个button,下面是onclick消息,在点击按钮是怎样把fragment03加载成新的fragment???
class MyButton implements View.OnClickListener {
    public void onClick(View v){
        edit_username = (EditText)view.findViewById(R.id.userNameText) ;
        edit_password = (EditText)view.findViewById(R.id.passwdText) ;
        string_username = edit_username.getText().toString();//登录账号
        string_password = edit_password.getText().toString();//登录密码

                    //云验证
        BmobQuery<login> bmobQuery= new BmobQuery<login>();;
        bmobQuery.addWhereEqualTo("username",string_username);
        bmobQuery.addWhereEqualTo("password",string_password);

        bmobQuery.findObjects(getActivity(), new FindListener<login>() {
            @Override
            public void onSuccess(List<login> list) {
                for (login gameScore : list) {
                    //获得playerName的信息
                    gameScore.getUsername();
                    gameScore.getPassword();
                    //Toast.makeText(getActivity(),gameScore.getUsername()+gameScore.getPassword(), Toast.LENGTH_SHORT).show();
                   //切换fragment   出错???
                    Fragment newFragment = new Fragment01();
                    FragmentTransaction transaction =getFragmentManager().beginTransaction();
                    transaction.replace(R.id.view_pager,newFragment);
                    transaction.commit();
                }

            }
            public void onError(int i, String s) {
               Toast.makeText(getActivity(), "登录失败 ", Toast.LENGTH_SHORT).show();
                //Log.v("test","登录失败");
            }
        });


    }
}


    用viewpager加fragment实现了底栏切换fragment,在某个fragment怎样重新加载fragment,某个fragment是登录功能,登录成功要加载用户信息,也就是新的fragment。怎么实现?在onCreateView中?


    ![图片说明](http://img.ask.csdn.net/upload/201604/15/1460718920_546782.png)

3个回答

Wei_Smile
Wei_Smile   2016.04.15 22:17

使用FragmentManager,具体的话看这篇文章http://blog.csdn.net/lmj623565791/article/details/37970961
讲的非常的详细

CSDNXIAON
CSDNXIAON   2016.04.19 21:02

使用TabHost+Fragment实现导航
----------------------同志你好,我是CSDN问答机器人小N,奉组织之命为你提供参考答案,编程尚未成功,同志仍需努力!

dongfeng9ge
dongfeng9ge   Ds   Rxr 2016.04.30 17:38

尽量用hide而不是replace

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!