2016-04-20 7 views
0

開発中のアプリケーションの画面の1つに、下部ナビゲーションバーによって制御されるフラグメントとして実装された複数のサブスクリーンがあります。まだユーザー入力を受け取っている非表示のフラグメント

これは、ユーザーが断片を切り替えたいときのために私のコードです:

protected final void switchTab(int index) { 
    if (curTabIndex == index) { 
     return; 
    } 

    if (bottomNavigationBar.getCurrentTabPosition() != index) { 
     bottomNavigationBar.selectTabAtPosition(index, true); 
    } 

    curTabIndex = index; 
    FragmentTransaction ft = fragmentManager.beginTransaction(); 

    ft.setCustomAnimations(R.anim.alpha_in, R.anim.alpha_out); 

    detachCurrentFragment(ft); 

    // Attempt to reattach previous fragment. 
    BottomTabbedActivityFragment fragment = reattachExistingFragment(ft); 

    if (fragment != null) { 
     ft.commit(); 
    } else { 
     fragment = fragments.get(curTabIndex); 
     ft.add(contentContainer.getId(), fragment, (fragment.getClass().getName())); 
     ft.commit(); 
    } 

    fragmentManager.executePendingTransactions(); 

    curFragment = fragment; 
} 

/** 
* Attemps to detach any current fragment if it exists, and if none is found, returns; 
* 
* @param ft the current transaction being performed 
*/ 
private void detachCurrentFragment(FragmentTransaction ft) { 
    // Attempt to detach current fragment. 
    if (curFragment != null) { 
     ft.hide(curFragment); 
    } 
} 

/** 
* Will attempt to reattach a previous fragment in the FragmentManager, or return null if not able to, 
* 
* @param ft current fragment transaction 
* @return Fragment if we were able to find and reattach it 
*/ 
@Nullable 
private BottomTabbedActivityFragment reattachExistingFragment(FragmentTransaction ft) { 
    Fragment fragment = fragmentManager.findFragmentByTag(fragments.get(curTabIndex).getTag()); 

    if (fragment != null) { 
     ft.show(fragment); 
    } 

    return (BottomTabbedActivityFragment) fragment; 
} 

上記のスニペットを使用して、私はスムーズ断片を切り替えることができますよ。しかし、このアプローチには大きな問題が1つあります。追加されたlasフラグメントは、隠されているにもかかわらずユーザーの入力を保持します。言い換えれば

は、私のユーザーは、この変更シーケンス谷行きましたと言う: - >Subscreen B - >Subscreen C -

Subscreen A>をSubscreen A

論理的には、入力は今Subscreen Aによって処理されます

Subscreen Cが非表示になっているためです。しかし、現実はそれ以外のことを言っています: Subscreen Cは依然としてユーザー入力を受け取ります。

Subscreen Cを表示していて、Subsreen Aにスクロールしようとしたときに、いくつかの不具合が発生することがあります。

私はこれを修正できますか?


P.S.、私が今持っているサブスクリーンは、再初期化に非常に高価です。それらはすべて複雑なビュー階層を持ち、ユーザーはそれらの間で多くの切り替えを行うことが予想されます。私はattach()/detach()を試しましたが、テストではとても遅くなりました。

+0

現在のフラグメントがまだアクティブな状態である場合は、現在のフラグメントを置き換えないでください。イベントのリスナーを使用している場合は、表示されていない限りnullを設定してください(無効に設定する必要があります)。 –

+0

目に見えなくても活動しています。入ってくるユーザー入力を処理しないようにするだけです(見えない部分)。私が言っているのは、 'View.setVisibility(INVISIBLE)'の代わりに 'View.setVisibility(GONE)'のようなものを使って隠したいということです。 – ridsatrio

+0

古いですが多分役立つかもしれませんが、あなたはバケツからclickeventを防ぐための方法が必要です。http://stackoverflow.com/questions/10389620/fragment-over-another-fragment-issue –

答えて

0

android:clickable="true"をフラグメントのレイアウトファイルのルートビューに追加するだけです。

+0

解決策を試してみました。 – ridsatrio

+0

この線を3つのA、B、Cのすべてのレイアウトに置きます。 – sonngaytho

+0

私はそれらのすべてにそれを入れました... – ridsatrio

関連する問題