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を使用して 2 つのビューを切り替えることができます。1 つはログイン用、もう 1 つはユーザーがログインしたときに表示する情報用です。

于 2012-05-10T12:01:54.987 に答える
1

おそらく私のソリューションは単純化されすぎているでしょうが、アクティビティの 1 つでそのようなことが起こっており、私が知る限り、パフォーマンスに大きな影響はありません。これが私がすることです。

すべての xml アイテムを単一のレイアウト .xml ファイルに配置します。RelativeLayout を親レイアウトにしてから、RelativeLayout 内に 2 つのレイアウトを作成します。1 つはR.layout.loggedinからの xml を含み、もう 1 つは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 の変更をアクティビティ内で何度でも呼び出すことができます。

私はこれを私が持っているアクティビティでうまく使用しており、うまく動作します。お役に立てれば!

于 2012-05-11T04:20:39.620 に答える