2017-10-07 9 views
0

私は拡張ループを使用していますが、配列に保持されている値の数に関連したレイアウトを動的に拡張します。動的ビューページに保持されているビューの状態を保存する方法

これは完全に機能しますが、各繰り返しで呼び出されるメソッドがありますが、これも機能しますが、解決に役立つ大きなバグがあります。

私の配列に5つのアイテムがあるとしたら、5つのレイアウトが膨らんでいるとします。これらのレイアウトでは、レイアウト上に小さなスクラッチカードタイプのセクションがあります。

ユーザーがページ1にいる場合は、スクラッチカードを使用してから2ページ目に移動し、スクラッチカードなどを使用すると正常に動作します。

しかし、ユーザーが1ページ目で5ページ目に移動してから1ページ目(基本的にランダムな順序)に戻ると、スクラッチカードは機能しません。

私が理解しているように、この方法は各反復で実装され、ビューはランダムな順序でスクロールバックまたはスクロールするとその状態が失われています。

したがって、作成したビューステートをビューページに保存する方法が必要です。

これは私のシナリオで可能ですか?私は解決策を見つけるために最善を尽くしましたが、私の質問に関連する何かを見つけることはできません。

ここに問題のコードのスニペットがあります。ご指導やご提案ありがとうございます!

for (String x : array1) { 
       //loop out the number of layouts relative to the number of questions held in x 
       View current_layout = LayoutInflater.from(getActivity()).inflate(R.layout.question_fragment, null); 
       //use the pageAdapter to add the layout to the users view 
       pagerAdapter.addView(current_layout); 

       //call method to add functionality to the scratchcard 
       isCorrect(current_layout); 
} 



public void isCorrect(View current_layout) { 



     ScratchoffController controller1 = new ScratchoffController(getActivity()) 
       .setThresholdPercent(0.40d) 
       .setTouchRadiusDip(getActivity(), 30) 
       .setFadeOnClear(true) 
       .setClearOnThresholdReached(true) 
       .setCompletionCallback(() -> { 
       }) 
       .attach(current_layout.findViewById(R.id.scratch_view1), current_layout.findViewById(R.id.scratch_view_behind1)); 



     ScratchoffController controller2 = new ScratchoffController(getActivity()) 
       .setThresholdPercent(0.40d) 
       .setTouchRadiusDip(getActivity(), 30) 
       .setFadeOnClear(true) 
       .setClearOnThresholdReached(true) 
       .setCompletionCallback(() -> { 
       }) 
       .attach(current_layout.findViewById(R.id.scratch_view2), current_layout.findViewById(R.id.scratch_view_behind2)); 

     ScratchoffController controller3 = new ScratchoffController(getActivity()) 
       .setThresholdPercent(0.40d) 
       .setTouchRadiusDip(getActivity(), 30) 
       .setFadeOnClear(true) 
       .setClearOnThresholdReached(true) 
       .setCompletionCallback(() -> { 
       }) 
       .attach(current_layout.findViewById(R.id.scratch_view3), current_layout.findViewById(R.id.scratch_view_behind3)); 

     ScratchoffController controller4 = new ScratchoffController(getActivity()) 
       .setThresholdPercent(0.40d) 
       .setTouchRadiusDip(getActivity(), 30) 
       .setFadeOnClear(true) 
       .setClearOnThresholdReached(true) 
       .setCompletionCallback(() -> { 
       }) 
       .attach(current_layout.findViewById(R.id.scratch_view4), current_layout.findViewById(R.id.scratch_view_behind4)); 



    } 
+0

実際のビューページャーの実装(フラグメントやビューだけを保持しているようなものですか?)にコードを投稿する必要があります。コードサンプルにはコンテキストがありません。 – dominicoder

答えて

0

私は々ussuallyフラグメントでViewPagerを使用して、私はviewpagerの外(私の場合)フラグメントのインスタンスへの参照を維持しようとしたときに何を言及することは、私に起こっています。

これは、あなたが言及した方法でタブを再表示するときに、ビューページがそのフラグメントの新しいインスタンスを作成する可能性があるためです。これが起こると、ビューページ外で保持しているインスタンス参照は、ビューページャが表示しているものではなくなります。

あなたの場合、according to this questionの場合、instatiateItemdestroyItemを超える必要があります。これらのメソッドを使用して状態の復元状態を保存し、instantiateItemが呼び出されたときに外部参照を更新できると思います。

関連する問題