2

我在这里进行了快速搜索,但没有找到我的问题的任何答案,如果它已经回答,请指出我的问题..

我有一个带有滑动视图的 ActionBar 选项卡,根据这个android training实现。我的活动有 3 个标签 Weather Comment Dashboard 和这些片段
WeatherFragment CommentsFragment LoginFragment DasboardFragment RegisterFragment

随着活动开始, Weather Tab显示WeatherFragmentComments Tab显示CommentsFragmentDashboard Tab显示LoginFragment

如果登录成功LoginFragmentDasboardFragment应该替换LoginFragment里面的DashboardTab。因此,如果用户滑动到其他选项卡并返回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());
    }
}
}
4

2 回答 2

1

我不确定这是处理此问题的最佳方法,但您可以在您的内部定义一个静态布尔值,MainActivity如下所示:

public static boolean loggedIn = false;

(例如下面ViewPager mViewPager;

然后,在你的方法processFinish(...)里面LoginFragmentwhen刚刚success设置0MainActivity.loggedIn = true;

这样,您可以简单地在 getItem 方法中的默认情况下放入 if 语句来检查用户是否已登录(如果是,则调用 Dashboard-Fragment)或未登录(显示 Login-Fragment)。

希望这对你有用!

编辑:登录片段

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) {             
            MainActivity.loggedIn = true;                 
        } else {
            loginError.setText(jsonObject.getString("ErrorMessage"));
        }

    } catch (JSONException e) {
        Log.e("JSON parsing from login result", e.getMessage());
    }
}
}
于 2013-04-07T00:16:00.987 回答
1

没有彻底阅读这里的答案是我自己的错。实现了该答案中的代码并使功能正常工作:)

于 2013-04-12T18:38:03.790 回答