2016-09-19 10 views
1

MainActivityのCODE:元のフラグメントの内容を保持する方法、他のフラグメントに移動して元のフラグメントに戻す方法はありますか?

public class MainActivity extends AppCompatActivity implements OnColorButtonListener { 

    @Override 

    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_main); 

    } 



    @Override 

    public void onColorClick(int color) { 

     Fragment fragment = null; 

     switch (color) { 

      case 0: 

       fragment = new RedFragment(); 

       break; 

      case 1: 

       fragment = new BlueFragment(); 

       break; 

     } 

     getSupportFragmentManager().beginTransaction() 

       .replace(R.id.fragment_ex, fragment).commit(); 

    } 

} 

RedFragmentのCODE:

public class RedFragment extends Fragment implements FlowerAdapter.FlowerClickListener { 



    private SwipeRefreshLayout swipeContainer_red; 

    private RecyclerView mRecyclerView; 

    private RestManager mRestManager; 

    private FlowerAdapter mFlowerAdapter; 



    @Nullable 

    @Override 

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 

     View view = inflater.inflate(R.layout.fragment_red, null); 

     swipeContainer_red = (SwipeRefreshLayout) view.findViewById(R.id.swipeContainer_red); 

     swipeContainer_red.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 

      @Override 

      public void onRefresh() { 

       mRestManager = new RestManager(); 

       Call<List<Flower>> listCall = mRestManager.getmFlowerApiService(getActivity()).getAllFlowers(); 

       listCall.enqueue(new Callback<List<Flower>>() { 

        @Override 

        public void onResponse(Call<List<Flower>> call, Response<List<Flower>> response) { 

         if (response.isSuccessful()) { 

          mFlowerAdapter.clear(); 

          List<Flower> flowerList = response.body(); 

          for(int i =0; i<flowerList.size(); i++) { 

           Flower flower = flowerList.get(i); 

           mFlowerAdapter.addFlower(flower); 

          } 

          swipeContainer_red.setRefreshing(false); 

         } 

        } 

        @Override 

        public void onFailure(Call<List<Flower>> call, Throwable t) { 

        } 

       }); 



      } 

     }); 

     configViews(view); 



     return view; 

    } 



    private void configViews(View view) { 

     mRecyclerView = (RecyclerView) view.findViewById(R.id.rv_flower_red); 

     mRecyclerView.setHasFixedSize(true); 

     mRecyclerView.setRecycledViewPool(new RecyclerView.RecycledViewPool()); 

     mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity().getApplicationContext(), 3)); 

     mFlowerAdapter = new FlowerAdapter(this); 

     mRecyclerView.setAdapter(mFlowerAdapter); 

    } 

    @Override 

    public void onClick(int position) { 

    } 

} 

私はRedFragmentの内容を読んだとき、私は青フラグメントに行くとRedFragmentに戻ってきた、RedFragmentが上だったすべての内容を失いました以前のRedFragment。

別のフラグメントをトリップしている間は、RedFragmentは変更されません。

質問

は、どのように私は他のフラグメントの物事を見ながらRedFragmentはそのままに設定することができますか?

編集// RowFragment.java

public class RowFragment extends Fragment implements View.OnClickListener { 

    private OnColorButtonListener onColorButtonListener; 
    public RowFragment() { 
    } 
    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
     onColorButtonListener = (OnColorButtonListener) context; 
    } 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View view = inflater.inflate(R.layout.fragment_row, container, false); 
     view.findViewById(R.id.v_red).setOnClickListener(this); 
     view.findViewById(R.id.v_blue).setOnClickListener(this); 
     return view; 
    } 

    @Override 
    public void onClick(View view) { 

     switch (view.getId()) { 
      case R.id.v_red: 
       onColorButtonListener.onColorClick(0); 
       break; 

      case R.id.v_blue: 
       onColorButtonListener.onColorClick(1); 
       break; 
     } 

マイ修正CODE:MainActivityのonColorClick方法

@Override 
public void onColorClick(int color) { 

    Fragment frag = null; 
    switch (color) { 
     case 0: 
      frag = getSupportFragmentManager().findFragmentByTag("red"); 
      if(frag==null){ 
       frag=new RedFragment(); 
       getSupportFragmentManager().beginTransaction().add(R.id.fragment_ex, frag, "red").commit(); 
      } 
      break; 
     case 1: 
      frag = getSupportFragmentManager().findFragmentByTag("blue"); 
      if(frag==null){ 
       frag=new BlueFragment(); 
       getSupportFragmentManager().beginTransaction().add(R.id.fragment_ex, frag, "blue").commit(); 
      } 
      break; 
    } 
     getSupportFragmentManager().beginTransaction().show(frag).commit(); 

    } 

しかし、最初のクリックは、それがうまく機能したときにそのコードは、問題を持っていますが、同じ色をもう一度クリックすると、それ以上は機能しません。私はそれを解決するために何をすべきですか?代わりにnewFragment毎回の

+0

http://stackoverflow.com/questions/39543988/fragments-display-getting-resetこれを読んで、問題を解決します –

+0

フラグメントスタックを使用できます。 –

答えて

2

Fragment frag=getSupportFragmentManager().findFragmentByTag(TAG); 
if(frag==null){ 
frag=new Fragment(); 
} 

と後であなたがカムバックして見直すことができるようにフラグメントがタグを指定加えながらにしてみてください。

また、setRetainInstanceメソッドを使用し、onCreateセットを使用することができます。

+0

申し訳ありませんが、あなたの素敵な答えを適用するにはあまりにも初心者です。タグを取得するには?またはどのようにしてTAGを設定するのか?もっとヒントを教えてもらえますか? – touchingtwist

+0

フラグメントを追加するときは、TAGもreplaceメソッドに渡します。 3つのフラグメントがあり、最初の1つのタグが「Frag_1」であり、2番目の「Frag_2」などであるとします。同じTAGを使用して、フラグメントも見つけてください。 –

+0

please教え方によっては自分のコード(My modified code:)をチェックしますか?あなたに迷惑をかけて申し訳ありません – touchingtwist

関連する問題