我有一个包含一个RecyclerView和一个按钮的片段。该片段是在按下前一个片段中的一个按钮(该按钮添加了一些数据)时提出的,回收器显示正确填充,按钮工作。然后,我回到前面的片段,再次按下按钮(添加更多数据),同一段代码将显示相同的片段,但是RecyclerView是未填充的,该按钮无法工作。通过阅读这个question,我猜想我正在进行片段管理--因为一旦我改变了方向并重新创建了片段,列表就会显示得很好,并且按钮也能正常工作。
这就是我对碎片的管理--我是从互联网上的一个例子来的,但我担心这是不正确的。
首先,我为我的片段创建了一个基类:
public class BaseFragment extends Fragment {
private AbstractFragmentCallback mCallback;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallback = (AbstractFragmentCallback) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement " +
AbstractFragmentCallback.class.getCanonicalName());
}
}
/**
* This method replaces the currently shown fragment with a new
fragment of a particular class.
* If a fragment of the required class already shown - does
nothing.
* @param clazz the class of the fragment to show
* @param addToBackStack whether the replacement should be added
to back-stack
* @param args arguments for the newly created fragment (can be
null)
*/
public void replaceFragment(Class<? extends Fragment> clazz,
boolean addToBackStack,
Bundle args) {
mCallback.replaceFragment(clazz, addToBackStack, args);
}
public interface AbstractFragmentCallback {
/**
* Call to this method replaces the currently shown fragment
with a new one
*
* @param clazz the class of the new fragment
* @param addToBackStack whether the old fragment should be
added to the back stack
* @param args arguments to be set for the new fragment
*/
void replaceFragment(Class<? extends Fragment> clazz, boolean addToBackStack,
Bundle args);
}
}因此,我的MainActivity实现了BaseFragment.AbstractfragmentCallback,因此:
public class MainActivity extends AppCompatActivity implements BaseFragment.AbstractFragmentCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(....);
if (null == savedInstanceState) {
replaceFragment(WelcomeFragment.class, false, null);
}
}
// ---------------------------------------------------------------------------------------------
//
// Fragments management
@Override
public void replaceFragment(Class<? extends Fragment> clazz, boolean addToBackStack,
Bundle args) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment newFragment;
try {
// Create new fragment
newFragment = clazz.newInstance();
if (args != null) newFragment.setArguments(args);
} catch (InstantiationException e) {
e.printStackTrace();
return;
} catch (IllegalAccessException e) {
e.printStackTrace();
return;
}
if (addToBackStack) {
ft.addToBackStack(null);
}
// Change to a new fragment
ft.replace(R.id.container, newFragment, clazz.getName());
ft.commit();
}
// End of fragments management
//
// ---------------------------------------------------------------------------------------------
}当我的带有按钮的片段想要打开它调用的列表片段时
replaceFragment(ListFragment.class, true, null);因此-为了明确起见,出现了ListFragemnt,它的所有代码触发,包括重新填充RecyclerView和重新填充-我甚至看到适配器中的所有代码都在为每个项调用,但是这些项没有出现在列表中。
public class ListFragment extends BaseFragment implements ListFragmentMvc.ListViewMvcListener {
private List<MyStuff> stuffList;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstance) {
stuffList = new ArrayList<>();
return inflater.inflate(R.layout.list_activity, container, true);
}
@Override
public void onResume() {
super.onResume();
recyclerView = mRootView.findViewById(R.id.recycler_view);
letsGoButton = mRootView.findViewById(R.id.lets_go_button);
letsGoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Activity activity = getActivity();
if (activity != null) {
getActivity().onBackPressed();
}
}
});
recyclerView.setItemAnimator(new DefaultItemAnimator());
DividerItemDecoration itemDecor = new DividerItemDecoration(context, HORIZONTAL);
recyclerView.addItemDecoration(itemDecor);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(context);
recyclerView.setLayoutManager(mLayoutManager);
setData(MyApplication.getAllStuffItems());
}
public void setData(List<MyStuff> stuff) {
stuffList = stuff;
mAdapter = new StuffAdapter(stuffList);
recyclerView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
}
}所以,在我重新进入listFragment时改变方向之前,有谁知道为什么这不起作用?
谢谢
发布于 2019-04-04 13:46:54
哇,真是浪费了一天。
最后,我犯了几个错误,这些错误被贴在一起,互相隐藏。
我认为真正的原因是我应该
return inflater.inflate(R.layout.list_activity, container, false);而不是
return inflater.inflate(R.layout.list_activity, container, true);我在代码中撒了一点小谎,因为我并没有真正直接返回inflater.inflate的结果.我调用它(使用错误的参数),然后返回null。
我想这将教会我在这篇文章中更加诚实。
发布于 2019-04-04 09:58:35
您能否尝试将设置/更新recyclerView和letsGoButton的代码从onResume方法移到onViewCreated方法,如下所示:
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
recyclerView = view.findViewById(R.id.recycler_view);
letsGoButton = view.findViewById(R.id.lets_go_button);
letsGoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Activity activity = getActivity();
if (activity != null) {
getActivity().onBackPressed();
}
}
});
recyclerView.setItemAnimator(new DefaultItemAnimator());
DividerItemDecoration itemDecor = new DividerItemDecoration(context, HORIZONTAL);
recyclerView.addItemDecoration(itemDecor);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(context);
recyclerView.setLayoutManager(mLayoutManager);
setData(MyApplication.getAllStuffItems());
}https://stackoverflow.com/questions/55512729
复制相似问题