分离不起作用(两个片段与 FlipView )



我正在处理碎片和flipview,但我在这方面遇到了一些问题。

当我的应用程序打开第一个屏幕时,使用FlipView显示我的提要。当我选择片段的另一个选项卡时,提要的片段没有隐藏或分离。我调试了很多次,代码到达分离的行,但没有工作。

这是我的代码

主活动类


@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FontManager.getInstance().initialize(this, R.xml.fonts);
        setContentView(R.layout.activity_main);
        mFBSession = new FBSessionsManager();
        mActionBar = getActionBar();
        fm = getFragmentManager();
        mTabHost = (TabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup();
        TabHost.OnTabChangeListener mTabChangeListener = new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                ft = fm.beginTransaction();
                ft.setTransition(FragmentTransaction.TRANSIT_ENTER_MASK);
                FeedPrincipalList mFeedFragment = (FeedPrincipalList) fm.findFragmentByTag(TAB_TAG_FEED);
                EverythingFragmentList mEverythingFragment = (EverythingFragmentList) fm.findFragmentByTag(TAB_TAG_ALL_EVERYTHING);
                PaymentFragment mPaymentFragment = (PaymentFragment) fm.findFragmentByTag(TAB_TAG_PAYMENT);
                ConsultFragmentList mConsultFragmentList = (ConsultFragmentList) fm.findFragmentByTag(TAB_TAG_M_CONSULT);
                ProfileFragmentList mProfileFragmentList = (ProfileFragmentList) fm.findFragmentByTag(TAB_TAG_M_PROFILE);
                if (mFeedFragment != null){
                    ft.detach(mFeedFragment);
//                    ft.commit();
                }
                if (mEverythingFragment != null)
                    ft.detach(mEverythingFragment);
                if (mPaymentFragment != null)
                    ft.detach(mPaymentFragment);
                if (mConsultFragmentList != null)
                    ft.detach(mConsultFragmentList);
                if (mProfileFragmentList != null){
                    ft.detach(mProfileFragmentList);
//                    ft.commit();
                }
                if (tabId.equalsIgnoreCase(TAB_TAG_FEED)) {
                    manageContextualActions(true, false);
                    if (mFeedFragment == null) {
                        mFeedFragment = new FeedPrincipalList();
                        ft.add(R.id.realTabContent, mFeedFragment, TAB_TAG_FEED);
                    } else {
                        mFeedFragment = new FeedPrincipalList();
                        ft.attach(mFeedFragment);
                    }
                }
                if (tabId.equalsIgnoreCase(TAB_TAG_ALL_EVERYTHING)) {
                    manageContextualActions(false, true);
                    if (mEverythingFragment == null) {
                        mEverythingFragment = new EverythingFragmentList();
                        ft.replace(R.id.realTabContent, mEverythingFragment, TAB_TAG_ALL_EVERYTHING);
                    } else {
                        ft.attach(mEverythingFragment);
                    }
                }
                if (tabId.equalsIgnoreCase(TAB_TAG_PAYMENT)) {
                    manageContextualActions(false, true);
                    if (mPaymentFragment == null) {
                        mPaymentFragment = new PaymentFragment();
                        ft.replace(R.id.realTabContent, mPaymentFragment, TAB_TAG_PAYMENT);
                    } else {
                        ft.attach(mPaymentFragment);
                    }
                }
                if (tabId.equalsIgnoreCase(TAB_TAG_M_CONSULT)) {
                    manageContextualActions(false, true);
                    if (mConsultFragmentList == null) {
                        mConsultFragmentList = new ConsultFragmentList();
                        ft.replace(R.id.realTabContent, mConsultFragmentList, TAB_TAG_M_CONSULT);
                    } else {
                        ft.attach(mConsultFragmentList);
                    }
                }
                if (tabId.equalsIgnoreCase(TAB_TAG_M_PROFILE)) {
                    manageContextualActions(false, false);
                    if (mProfileFragmentList == null) {
                        mProfileFragmentList = new ProfileFragmentList();
                        ft.replace(R.id.realTabContent, mProfileFragmentList, TAB_TAG_M_PROFILE);
                    } else {
                        mProfileFragmentList = new ProfileFragmentList();
                        ft.attach(mProfileFragmentList);
                    }
                }
                ft.commit();
            }
        };
        mTabHost.setOnTabChangedListener(mTabChangeListener);
        createAndConfigureTabs();
    }
@Override
    protected void onStart() {
        super.onStart();
    }
    @Override
    protected void onStop() {
        super.onStop();
    }
    public void setupActionBar(int stringTitleID) {
        mActionBar.setDisplayShowCustomEnabled(true);
        mActionBar.setDisplayShowTitleEnabled(false);
        mActionBar.setDisplayUseLogoEnabled(false);
        final LayoutInflater inflater = LayoutInflater.from(this);
        final View customTitle = inflater.inflate(R.layout.custom_title, null);
        ((TextView) customTitle.findViewById(R.id.customTitleActionbar)).setText(getResources().getString(stringTitleID));
        mActionBar.setCustomView(customTitle);
    }
    private void createAndConfigureTabs() {
        mTabFeed = mTabHost.newTabSpec(TAB_TAG_FEED);
        mTabFeed.setIndicator(null, getResources().getDrawable(R.drawable.custom_btn_feed));
        mTabFeed.setContent(new TabContentCreator(this));
        mTabHost.addTab(mTabFeed);
        mTabAllEverything = mTabHost.newTabSpec(TAB_TAG_ALL_EVERYTHING);
        mTabAllEverything.setIndicator(null, getResources().getDrawable(R.drawable.custom_btn_all_everything));
        mTabAllEverything.setContent(new TabContentCreator(this));
        mTabHost.addTab(mTabAllEverything);
        mTabCameraAndPayment = mTabHost.newTabSpec(TAB_TAG_PAYMENT);
        mTabCameraAndPayment.setIndicator(null, getResources().getDrawable(R.drawable.btn_camera));
        mTabCameraAndPayment.setContent(new TabContentCreator(this));
        mTabHost.addTab(mTabCameraAndPayment);
        mTabConsult = mTabHost.newTabSpec(TAB_TAG_M_CONSULT);
        mTabConsult.setIndicator(null, getResources().getDrawable(R.drawable.custom_btn_consult));
        mTabConsult.setContent(new TabContentCreator(this));
        mTabHost.addTab(mTabConsult);
        mTabMyProfile = mTabHost.newTabSpec(TAB_TAG_M_PROFILE);
        mTabMyProfile.setIndicator(null, getResources().getDrawable(R.drawable.custom_btn_profile));
        mTabMyProfile.setContent(new TabContentCreator(this));
        mTabHost.addTab(mTabMyProfile);
        mTabWidget = mTabHost.getTabWidget();
        for (int i = 0; i minor 5; i++) {
             View v = mTabWidget.getChildAt(i);
            v.setBackgroundResource(android.R.drawable.screen_background_light_transparent);
            if (i == 4)
                v.setPadding(0, 0, 0, 12);
        }
    }
private class TabContentCreator implements TabHost.TabContentFactory {
        private Context mContext;
        public TabContentCreator(Context context) {
            mContext = context;
        }
        @Override
        public View createTabContent(String tag) {
            View v = new View(mContext);
            return v;
        }
    }

FeedFragment类


public class FeedPrincipalList extends android.app.Fragment implements FlipView.OnFlipListener, FlipView.OnOverFlipListener, FlipView.OnClickListener {
    private static int timelinePage = 1, mPageCount = 0;
    private FlipView mFlipView;
    private FeedPrincipalAdapter mAdapter;
    private FBSessionsManager fbSessionsManager;
    private List mList;
    private Context context;
    private int posicaoTela;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        context = getActivity().getApplicationContext();
        View view = inflater.inflate(R.layout.feed_flipview, container);
        ((MainFragmentActivity) getActivity()).setupActionBar(R.string.title_fragment_feed);
        mFlipView = (FlipView) view.findViewById(R.id.fv_feedFlipView_FlipView);
        mFlipView.setOnFlipListener(this);
        mFlipView.setOverFlipMode(OverFlipMode.RUBBER_BAND);
        mFlipView.setEmptyView(view.findViewById(R.id.empty_view));
        mFlipView.setOnOverFlipListener(this);
        mFlipView.setOnClickListener(this);
        fbSessionsManager = new FBSessionsManager(getActivity());
        if (mList == null)
            new LoadFeedPrincipalData().execute();
        return super.onCreateView(inflater,container,savedInstanceState);
    }
    @Override
    public void onFlippedToPage(FlipView v, int position, long id) {
        Log.i("pageflip", "Page: " + position);
        posicaoTela = position;
        if(position > mFlipView.getPageCount()-3 && mFlipView.getPageCount() {
        private UtilWS ws;
        private JSONObject toSend;
        private JsonArray rsArray;
        private JsonParser parser;
        public LoadFeedPrincipalData() {
            ws = new UtilWS();
            toSend = new JSONObject();
            parser = new JsonParser();
        }
        @Override
        protected Void doInBackground(Void... params) {
            try {
                toSend.put("token", fbSessionsManager.getStoredPrivateSession()[0]);
                toSend.put("userId", fbSessionsManager.getStoredPrivateSession()[1]);
                toSend.put("page", timelinePage);
                String[] jsonResult = ws.post(UtilWS.URL_TIMELINE, toSend.toString());
                if (jsonResult[0].equals("200")) {
                    JsonObject temp = parser.parse(jsonResult[1]).getAsJsonObject();
                    Log.i("JSON",": "+temp.toString());
                    if (temp.get("success").getAsBoolean()) {
                        rsArray = temp.get("looks").getAsJsonArray();
                        if (mPageCount == 0)
                            mPageCount = temp.get("pageCount").getAsInt();
                        for (JsonElement el : rsArray) {
                            if (mList == null)
                                mList = new ArrayList();
                            temp = (JsonObject) el;
                            FeedPrincipalTO tempTudo = new FeedPrincipalTO(temp.get("lookId").getAsString(), temp.get("photoUrl1").getAsString(), temp.get("photoUrl2").getAsString(), temp.get("jaVotou").getAsBoolean(),temp.get("photoVoted").getAsInt() ,temp.get("photo1Total").getAsString(), temp.get("photo2Total").getAsString());
                            mList.add(tempTudo);
                        }
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void aVoid) {
            if (mList != null) {
                Log.i("mList","mLista");
                mAdapter = new FeedPrincipalAdapter(context,mList);
                mAdapter.setCallback(new FeedPrincipalAdapter.Callback() {
                    @Override
                    public void onPageRequested(int page) {
                        mFlipView.smoothFlipTo(page);
                    }
                    @Override
                    public void voteUp(int position) {
                        Log.i("TESTE","position = " + posicaoTela);
                        mAdapter.getView(position, null, null).postInvalidate();
//      mAdapter.notifyDataSetChanged();
                    }
                });
                if (timelinePage == 1) {
                    mFlipView.setAdapter(mAdapter);
//                    initListenerList();
                } else {
                    ((FeedPrincipalAdapter) mFlipView.getAdapter()).updateList(mList);
                    ((FeedPrincipalAdapter) mFlipView.getAdapter()).notifyDataSetChanged();
                }
            } else {
                mFlipView.setAdapter(new FeedPrincipalAdapter(context,null));
            }
        }
//        private void initListenerList() {
//            getListView().setOnScrollListener(new AbsListView.OnScrollListener() {
//                @Override
//                public void onScrollStateChanged(AbsListView view, int scrollState) {
//
//                }
//
//                @Override
//                public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
//                    if (firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount != 0) {
//                        if (timelinePage minor mPageCount) {
//                            timelinePage++;
//                            new LoadFeedPrincipalData().execute();
//                        }
//                    }
//                }
//            });
//        }
    }
}

请帮帮我!!!

我正在使用FlipViewhttps://github.com/emilsjolander/android-FlipView

谢谢!!!

最后我解决了这个问题。

首先我稍微改变了一下主课。

我在MainActivity类中更改了这两个if(这个片段使用FlipView)。


if (tabId.equalsIgnoreCase(TAB_TAG_FEED)) {
    manageContextualActions(true, false);
    if (mFeedFragment == null) {
        mFeedFragment = new FeedPrincipalList();
        ft.add(R.id.realTabContent, mFeedFragment, TAB_TAG_FEED);
    } else {
        // mFeedFragment = new FeedPrincipalList();
        ft.attach(mFeedFragment);
    }
}
if (tabId.equalsIgnoreCase(TAB_TAG_M_PROFILE)) {
    manageContextualActions(false, false);
    if (mProfileFragmentList == null) {
        mProfileFragmentList = new ProfileFragmentList();
        ft.add(R.id.realTabContent, mProfileFragmentList, TAB_TAG_M_PROFILE);
    } else {
        // mProfileFragmentList = new ProfileFragmentList();
        ft.attach(mProfileFragmentList);
    }
}

其次,我更改了FeedFragment类的CreateView方法。


@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        context = getActivity().getApplicationContext();
        ((MainFragmentActivity) getActivity()).setupActionBar(R.string.title_fragment_feed);
        fbSessionsManager = new FBSessionsManager(getActivity());
        if (mList == null){
            view = inflater.inflate(R.layout.feed_flipview, container,true);
            mFlipView = (FlipView) view.findViewById(R.id.fv_feedFlipView_FlipView);
            mFlipView.setOnFlipListener(this);
            mFlipView.setOverFlipMode(OverFlipMode.RUBBER_BAND);
            mFlipView.setEmptyView(view.findViewById(R.id.empty_view));
            mFlipView.setOnOverFlipListener(this);
            new LoadFeedPrincipalData().execute();
        }
        return super.onCreateView(inflater,container,savedInstanceState);
    }

你可以看到区别。我只是第一次夸大了这个观点。在此之后,我的mList(它是MyGetSetClass的列表)不再为空。

最后,我在FeedFragment类上重写这两个方法。


@Override
    public void onDetach() {
        super.onDetach();
//        Log.i("onDetach", "FeedPrincipalList");
        mList = null;
        timelinePage = 1;
        mPageCount = 0;
    }
    @Override
    public void onStart() {
        super.onStart();
//        Log.i("onStart", "FeedPrincipalList");
        this.mFlipView.setVisibility(View.VISIBLE);
    }
    @Override
    public void onStop() {
        super.onStop();
//        Log.i("onStop","FeedPrincipalList");
        this.mFlipView.setVisibility(View.INVISIBLE);
    }

在分离方法中,我清理了所有静态变量,这些变量将在我再次启动应用程序时使用。

onStart和onStop用于更改"动画视图"的"可见性"。

就是这样。

我希望这对每个人都有帮助!!!

最新更新