gpt4 book ai didi

android - 如何保存原始 fragment 的内容,同时去其他 fragment 并返回原始 fragment ?

转载 作者:行者123 更新时间:2023-11-29 19:33:28 25 4
gpt4 key购买 nike

主要 Activity 代码:

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 代码:

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 的内容时,我转到蓝色 fragment 并返回到 RedFragment,RedFragment 丢失了之前在 RedFragment 上的所有内容。

我希望 RedFragment 在我绊倒另一个 Fragment 时不会发生任何变化。

问题

当我看到其他 fragment 时,如何让 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;
}

我修改的代码: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

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

并在添加 fragment 时指定一个标签,以便稍后您可以回来重新访问。

您还可以使用 setRetainInstance 方法并在 onCreate 中设置。

关于android - 如何保存原始 fragment 的内容,同时去其他 fragment 并返回原始 fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39571402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com