我在这里进行了快速搜索,但没有找到我的问题的任何答案,如果它已经回答,请指出我的问题..
我有一个带有滑动视图的 ActionBar 选项卡,根据这个android training实现。我的活动有 3 个标签
Weather
Comment
Dashboard
和这些片段
WeatherFragment
CommentsFragment
LoginFragment
DasboardFragment
RegisterFragment
随着活动开始,
Weather Tab
显示WeatherFragment
,
Comments Tab
显示CommentsFragment
和
Dashboard Tab
显示LoginFragment
如果登录成功LoginFragment
,DasboardFragment
应该替换LoginFragment
里面的Dashboard
Tab。因此,如果用户滑动到其他选项卡并返回Dashboard Tab
DasboardFragment
应该是可见的。
我是 android 开发的新手,所以任何代码片段或教程将不胜感激
代码我到目前为止MainActivity 类
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
AppSectionsPagerAdapter mAppSectionsPagerAdapter;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_get_network_weather);
mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(
getSupportFragmentManager());
final ActionBar actionBar = getActionBar();
//actionBar.setDisplayShowTitleEnabled(false);
//actionBar.setDisplayShowHomeEnabled(false);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab()
.setText(mAppSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {
public AppSectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
switch (i) {
case 0:
return new WeatherInfoFragment();
case 1:
return new PostsFragment();
default: //TODO method to find which fragment to display ?
return new LoginFragment();
}
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
if (position == 0) {
return "Weather";
} else if (position == 1){
return "Comments";
}
else{
return "Dashboard";
}
}
}
@Override
public void onTabReselected(ActionBar.Tab tab,
android.app.FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {
}
}
登录片段
public class LoginFragment extends Fragment implements AsyncResponse {
Button loginButton;
TextView loginError, login_url;
JSONfunctions task;
JSONObject jsonObject;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.login, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public void onStart() {
super.onStart();
loginButton = (Button) getView().findViewById(R.id.button_login);
loginError = (TextView) getView().findViewById(R.id.login_error);
loginButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
attemptLogin(finalLoginUrl);
// attemptPost(postURL);
}
});
}
private void attemptLogin(String url) {
try {
task = new JSONfunctions(getActivity());
task.listener = this;
task.execute(new String[] { url });
} catch (Exception ex) {
Log.e("attempt login", ex.getMessage());
}
}
}
@Override
public void processFinish(String result) {
try {
jsonObject = new JSONObject(result);
int success = Integer.parseInt(jsonObject.getString("Success"));
if (success == 0) {
// Replace LoginFragment and launch DashboardFragment ?
} else {
loginError.setText(jsonObject.getString("ErrorMessage"));
}
} catch (JSONException e) {
Log.e("JSON parsing from login result", e.getMessage());
}
}
}