gpt4 book ai didi

android - RecyclerView 在没有 fragment 的情况下工作。当我使用 fragment 时不起作用

转载 作者:行者123 更新时间:2023-11-29 20:49:30 25 4
gpt4 key购买 nike

我开始学习 Android 并且在使用 fragment 时遇到了一些困难。你能帮我弄清楚问题是什么吗?除了下面提到的内容之外,其他所有内容在两个项目中都是相同的

1. Code without fragments (works totally fine. Gives a simple list with some data):



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerList);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(llm);
recyclerView.setAdapter(new MyRecyclerAdapter(generatePalettes()));
}



2. Code with Fragment (doesnt work. Blank Screen):



MainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new MainActivityFragment()).commit();
}
}



MainActivityFragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerList);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(llm);
recyclerView.setAdapter(new PaletteAdapter(generatePalettes()));
return rootView;
}

--------------------------------更新------------ --

代码更改为以下内容:仍然不起作用。显示空白屏幕。尝试将 UI 更新放在 onActivityCreated 和 onViewCreated() 中。代码要点在这里:https://gist.github.com/adityarb88/12f5ccce665afbf58069

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerList);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(llm);
recyclerView.setAdapter(new PaletteAdapter(generatePalettes()));
}

最佳答案

在构建 View 后,对于 Fragments 设置适配器:

主要 Activity fragment

此方法应该只膨胀并返回您的 View :

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}

然后在onViewCreated(View, Bundle)onActivityCreated(Bundle)中:

   @Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);

// getView() returns your Fragments root view
RecyclerView recyclerView = (RecyclerView) getView().findViewById(R.id.recyclerList);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(llm);
recyclerView.setAdapter(new PaletteAdapter(generatePalettes()));

}

关于android - RecyclerView 在没有 fragment 的情况下工作。当我使用 fragment 时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29528998/

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