Recyclerview가 onCreateViewHolder를 호출하지 않습니다
내는 RecyclerView
호출하지 않습니다 onCreateViewHolder
, onBindViewHolder
도 MenuViewHolder
에 따라서 아무것도 나타납니다, 생성자를 RecyclerView
. 디버깅을 위해 로그를 넣고 로그가 표시되지 않습니다. 무엇이 문제 일 수 있습니까?
내 어댑터 :
public class MenuAdapter extends RecyclerView.Adapter<MenuAdapter.MenuViewHolder> {
private LayoutInflater inflater;
List<Menu> data = Collections.emptyList();
public MenuAdapter(Context context, List<Menu> data) {
Log.i("DEBUG", "Constructor");
inflater = LayoutInflater.from(context);
Log.i("DEBUG MENU - CONSTRUCTOR", inflater.toString());
this.data = data;
for(Menu menu: this.data){
Log.i("DEBUG MENU - CONSTRUCTOR", menu.menu);
}
}
@Override
public MenuViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.row_menu, parent, false);
MenuViewHolder holder = new MenuViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MenuViewHolder holder, int position) {
Log.i("DEBUG MENU", "onBindViewHolder");
Menu current = data.get(position);
holder.title.setText(current.menu);
}
@Override
public int getItemCount() {
return 0;
}
class MenuViewHolder extends RecyclerView.ViewHolder {
TextView title;
ImageView icon;
public MenuViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.menuText);
}
}
내 사용자 지정 행 XML :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/menuText"
android:text="Dummy Text"
android:layout_gravity="center_vertical"
android:textColor="#222"/>
그리고 내 조각 :
public NavigationFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mUserLearnedDrawer = Boolean.valueOf(readFromPreferences(getActivity(), KEY_USER_LEARNED_DRAWER, "false"));
if (savedInstanceState != null) {
mFromSavedInstaceState = true;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_navigation, container, false);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.drawer_list);
MenuAdapter adapter = new MenuAdapter(getActivity(), getData());
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(adapter);
return view;
}
당신의 getItemCount
방법 returns 0
. 따라서 RecyclerView
뷰를 인스턴스화하려고 시도하지 마십시오. 무언가를 돌려주십시오 greater than 0
.
예를 들어
@Override
public int getItemCount() {
return data.size();
}
다른 하나는 레이아웃 관리자를 RecyclerView로 설정했는지 확인하는 것입니다.
recycler.setLayoutManager(new LinearLayoutManager(this));
추가 한 후에 아래 줄을 추가하는 것을 잊었습니다.
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
특정 경우에는 적용되지 않습니다. 그러나 이것은 다른 사람을 도울 수 있습니다.
이 이유는 다음 방법을 부주의하게 사용했을 수 있습니다.
recyclerView.setHasFixedSize(true);
주의 사용되지 않은 경우,이는 발생할 수 있습니다 onBindView
과 onCreateViewHolder
에 로그에 오류없이, 전혀 호출 할 수 없습니다.
For what it's worth, I observed it when I set the recycler view adapter before the adapter was actually initialized. Solution was to make sure recyclerView.setAdapter(adapter)
was called with a non-null adapter
Please set layout manager to RecyclerView
like below code:
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view_right);
//set your recycler view adapter here
NavigationAdapter adapter = new NavigationAdapter(this, FragmentDrawer.getData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
Setting the height of the TextView
in the custom.xml
or RecyclerView
. If height is wrap-content
, the RecyclerView
will not be visible.
This happened when my RecyclerView
was inside a ScrollView
and I was using Android Support Library 23.0. To fix, I updated to Android Support Library 23.2:
In build.gradle:
dependencies {
...
compile 'com.android.support:appcompat-v7:23.2.+'
compile 'com.android.support:cardview-v7:23.2.+'
}
I had the same problem, because I was using android.support.constraint.ConstraintLayout
in layout resource. Changing to FrameLayout
helped.
Add this to Your Adapter Class
@Override
public int getItemCount() {
return mDataset.size();
}
Maybe it helps someone but if you set your RecycleView's visibility to GONE the adapter methods won't be called at all... Took me some time to figure it out.
I really really hope this helps someone someday. I just spend over an hour on this one going nuts!
I had this:
projects_recycler_view.apply {
this.layoutManager = linearLayoutManager
this.adapter = adapter
}
When I should have had this:
projects_recycler_view.apply {
this.layoutManager = linearLayoutManager
this.adapter = projectAdapter
}
Because I foolishly called my adapter adapter
, it was being shadowed by the recycler view's adapter in the apply block! I renamed the adapter to projectAdapter to differentiate it and problem solved!
Other option is if you send this list objetc using get,verify if you have correct reference,for example
private list<objetc> listObjetc;
//Incorrect
public void setListObjetcs(list<objetc> listObjetc){
listObjetc = listObjetc;
}
//Correct
public void setListObjetcs(list<objetc> listObjetc){
this.listObjetc = listObjetc;
}
I struggled with this issue for many hours. I was using a fragment. And the issue was not returning the view
. Below is my code:
ProductGridBinding binding = DataBindingUtil.inflate( inflater, R.layout.product_grid, container, false);
mLinearLayoutManager = new LinearLayoutManager(getActivity());
mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
binding.rvNumbers.setHasFixedSize(true);
binding.rvNumbers.setLayoutManager(mLinearLayoutManager);
binding.rvNumbers.setAdapter(adapter);
View view = binding.getRoot();
return view;
See my answer if you are using android data binding library - Make sure you are setting layout for recyclerview and item count must be greater than 0
@BindingAdapter({"entries", "layout"})
public static void setEntries(RecyclerView view, ArrayList<LoginResponse.User> listOfUsers, int layoutId) {
if (view.getAdapter() == null) {
view.setLayoutManager(new LinearLayoutManager(view.getContext()));
SingleLayoutAdapter adapter = new SingleLayoutAdapter(layoutId) {
@Override
protected Object getObjForPosition(int position) {
return listOfUsers.get(position);
}
@Override
public int getItemCount() {
return listOfUsers.size();
}
};
view.setAdapter(adapter);
}
}
Happy coding :-)
In my case I set the ID of my RecyclerView to "recyclerView". It seems that this ID was already defined somewhere, because when I changed that to different ID, the methods from the adapter were called properly.
If using a custom adapter do not forget to set the ItemCount to the size of your collection.
In my case, my list size was 0 and it was not calling the onCreateViewHolder method. I needed to display a message at center for the empty list as a placeholder so I did it like this and it worked like charm. Answer by @Konstantin Burov helped.
@Override
public int getItemCount() {
if (contactList.size() == 0) {
return 1;
} else {
return contactList.size();
}
}
My encouter was the onBindViewHolder( holder , position) overrided method of the RecyclerView.adaptor not being called. Method onCreateViewHolder was called, getItemCount was called. If you read the specs for Recyclerview Adapter, you will learn that if you have overrided onBindViewHolder( holder, position, List payloads) will be called in favour. So please be careful.
If you put wrap_content as height of your list as well as the height of your item list, nothing will show up and none of your recyclerView functions will be called. Fix the height of the item or put match_parent for the list height. That solved my problem.
My mistake was inflating the wrong view in Fragment onCreateView.
go to your xml. And change your layout to wrap content. The one element is hiding the next one, so the methods are not working!
Please do the following thing
@Override
public int getItemCount() {
return data.size();
}
In my case after changing Activity to use ViewModel and DataBinding, I had forgotten to remove setContentView
method calling from onCreate
method. By removing it my problem solved.
For me, it was because the setHasStableIds(true); method was set. Remove that and it will work
recyclerView.setAdapter(adapter); called The recyclerView list is populated by calling the method.
참고URL : https://stackoverflow.com/questions/27809524/recyclerview-not-call-oncreateviewholder
'IT story' 카테고리의 다른 글
UITableViewCell 내부에서 버튼 클릭 가져 오기 (0) | 2020.06.27 |
---|---|
Swift의 펜촉에서 UIView로드 (0) | 2020.06.27 |
디버거에서 .NET 언어 단계를 올바르게 만들기 (0) | 2020.06.27 |
Visual Studio가 아닌 웹 브라우저에서 Visual Studio의 링크를 어떻게 열 수 있습니까? (0) | 2020.06.27 |
CMake는 정확히 어떻게 작동합니까? (0) | 2020.06.27 |