问题描述
- 使用viewpager,fragment底栏导航,新的fragment替换旧的fragment
-
主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)
解决方案
使用FragmentManager,具体的话看这篇文章http://blog.csdn.net/lmj623565791/article/details/37970961
讲的非常的详细
解决方案二:
使用TabHost+Fragment实现导航
解决方案三:
尽量用hide而不是replace
时间: 2024-09-30 20:03:52