2

我有一个带有 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) {}
}
4

2 回答 2

2

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

于 2012-05-10T12:01:54.987 回答
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 中。这使我能够启动写入数据库的活动,当我回来时,由于屏幕被重绘,所以会进行适当的更改。使用这种方法,虽然我不确定您为什么要这样做,但您甚至可以根据需要在活动中反复调用 xml 中的更改。

我在我拥有的 Activity 中成功使用了它,并且它可以流畅地工作。希望这可以帮助!

于 2012-05-11T04:20:39.620 回答