在SherlockFragment中切换布局XML

时间:2012-05-02 22:11:57

标签: android android-layout android-fragments actionbarsherlock

我有一个带ABS的动作栏标签系统。第一个选项卡是登录选项卡。我将登录用户存储在本地SQLite数据库和Web SQL数据库中。当用户点击loginButton并且Async loginTask完成时,我想更改登录选项卡的xml布局。

所以基本上我可以运行测试来查看用户是否已登录,如果是,则使用新布局。 有没有合理的方法来完成驻留在actionBar标签系统中的SherlockFragment中的布局切换?

显然我需要在onResume内部运行相同的检查,以确保用户是否登录,显示正确的UI。我的问题是当前在onCreateView中放宽了我的布局,它没有重新运行。

以下是一些代码:

public class LoginFragment extends SherlockFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    userFunctions = new UserFunctions();
//test ive made that has no effect since onCreateView only runs once.
    if(userFunctions.isUserLoggedIn(activity.getApplicationContext())) {
        View v = inflater.inflate(R.layout.loggedin, container, false);
        return v;
    } else {//standard method when I didnt use the above test
    View v = inflater.inflate(R.layout.loginfragment, container, false);
    return v;
    }
}

我有一个托管标签等的活动,这是相关代码:

public static class TabsAdapter extends FragmentPagerAdapter
implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
    private final Context mContext;
    private final ActionBar mActionBar;
    private final ViewPager mViewPager;
    private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

    static final class TabInfo {
        private final Class<?> clss;
        private final Bundle args;

        TabInfo(Class<?> _class, Bundle _args) {
            clss = _class;
            args = _args;
        }
    }
    /*Constructor method that adds a TabsAdapter to each tab that is created.
     * It also adds the ViewPager to each tab so that the user can swipe to change tabs.
     */
    public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {
        super(activity.getSupportFragmentManager());
        mContext = activity;
        mActionBar = activity.getSupportActionBar();
        mViewPager = pager;
        mViewPager.setAdapter(this);
        mViewPager.setOnPageChangeListener(this);
    }
    /*A fairly simple method that sets the TabInfo for each tab so that the TabsAdapter
     * knows which class the tab that is being added actually belonds to. It also updates
     * the UI interface when each tab is added. 
     */
    public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
        TabInfo info = new TabInfo(clss, args);
        tab.setTag(info);
        tab.setTabListener(this);
        mTabs.add(info);
        mActionBar.addTab(tab);
        notifyDataSetChanged();
    }

    public int getCount() {
        return mTabs.size();
    }
    /*A method that is used in other classes to allow each tab Fragment to 
     * access its inherited methods from a mother-class, in this case, SherlockFragment
     */
    public SherlockFragment getItem(int position) {
        TabInfo info = mTabs.get(position);
        return (SherlockFragment)Fragment.instantiate(mContext, info.clss.getName(), info.args);
    }
    /*This method reads the user's selection for a new tab and sets that tab as
     * the new current focus.*/
    public void onPageSelected(int position) {
        mActionBar.setSelectedNavigationItem(position);
    }
    public void onPageScrollStateChanged(int state) {}
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
    /* This is the method that actually draws the newest tab onto the screen when
     * it is selected.*/
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        mViewPager.setCurrentItem(tab.getPosition());
        Object tag = tab.getTag();
        for (int i=0; i<mTabs.size(); i++) {
            if (mTabs.get(i) == tag) {
                mViewPager.setCurrentItem(i);
            }
        }
    }
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {}
    public void onTabReselected(Tab tab, FragmentTransaction ft) {}
    public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {}
    public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {}
}

2 个答案:

答案 0 :(得分:2)

在片段布局中,您可以使用ViewSwitcher在两个视图之间切换,一个用于登录,另一个用于在用户登录时显示的信息。

答案 1 :(得分:1)

也许我的解决方案会过于简​​单但我在我的一个活动中有类似的事情,据我所知,我没有取得巨大的成绩。这就是我的工作。

我将所有xml项目放在一个布局.xml文件中。将RelativeLayout设为父布局,然后在RelativeLayout中创建两个布局。一个包含 R.layout.loggedin 中的xml,另一个包含 R.layout.loginfragment xml项目。然后我在onActivityCreated上进行以下调用。

if(userFunctions.isUserLoggedIn(activity.getApplicationContext())) {
   loginfragment.setVisibility(RelativeLayout.GONE);
   //Your other appropriate code here to interact with the items in this layout
} else {//standard method when I didnt use the above test
    loggedin.setVisibility(RelativeLayout.GONE);
   //More code here to interact with the items in this layout
}

我的TabsAdapter类等效设置为

 android:stateNotNeeded="true"

在我的AndroidManifest.xml中。这使我能够启动对db的活动写入,当我回来时,由于屏幕被重绘,所以进行了相应的更改。使用这种方法,虽然我不确定你为什么要这样做,但你甚至可以在活动中反复调用xml中的更改。

我在一个活动中成功地使用了它,它可以游泳。希望这有帮助!