可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
My RecyclerView
does not call onCreateViewHolder
, onBindViewHolder
even MenuViewHolder
constructor, therefore nothing appears in RecyclerView
. I put logs for debugging, and no log is shown. What might be the problem?
My adapter:
public class MenuAdapter extends RecyclerView.Adapter { private LayoutInflater inflater; List
My custom row XML:
and my Fragment:
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; }
回答1:
Your getItemCount
method returns 0
. So RecyclerView
never tries to instantiate a view. Make it return something greater than 0
.
回答2:
Another one is make sure you set layout manager to RecyclerView:
recycler.setLayoutManager(new LinearLayoutManager(this));
回答3:
FWIW, 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
回答4:
This does not apply for your particular case. But this might help someone else.
This reason could be careless usage of the following method
recyclerView.setHasFixedSize(true);
If not used with caution, this might cause the onBindView
and onCreateViewHolder
to not be called at all, with no error in the logs.
回答5:
i forgot to add below line after i adding it worked for me
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
回答6:
Setting the height of the TextView
in the custom.xml
or RecyclerView
. If height is wrap-content
, the RecyclerView
will not be visible.
回答7:
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.+' }
回答8:
I had the same problem, because I was using android.support.constraint.ConstraintLayout
in layout resource. Changing to FrameLayout
helped.
回答9:
Add this to Your Adapter Class
@Override public int getItemCount() { return mDataset.size(); }
回答10:
Please set layout manager to RecyclerView like below code:
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view_right); NavigationAdapter adapter = new NavigationAdapter(this, FragmentDrawer.getData()); recyclerView.setAdapter(adapter); recyclerView.setLayoutManager(new LinearLayoutManager(this));
回答11:
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.
回答12:
Other option is if you send this list objetc using get,verify if you have correct reference,for example
private list listObjetc; //Incorrect public void setListObjetcs(list listObjetc){ listObjetc = listObjetc; } //Correct public void setListObjetcs(list listObjetc){ this.listObjetc = listObjetc; }
回答13:
If using a custom adapter do not forget to set the ItemCount to the size of your collection.
回答14:
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(); } }
回答15:
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;
回答16:
Please do the following thing
@Override public int getItemCount() { return data.size(); }
回答17:
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.