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 on theColorClick方法
@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();
}
但是这个代码有问题,当第一次点击时,它运作良好,但是第二次点击相同的颜色,它不再起作用了。我该怎么做才能解决它?
答案 0 :(得分:2)
每次尝试
而不是newFragmentFragment frag=getSupportFragmentManager().findFragmentByTag(TAG);
if(frag==null){
frag=new Fragment();
}
并且在添加片段时指定标记,以便稍后您可以回归并重新访问。
此外,您可以使用setRetainInstance
方法并在onCreate
设置回来。