Android 回收器视图没有显示任何内容
我一直在制作一个使用RecyclerView的应用程序,但它没有显示任何东西。为什么RecyclerView的内容没有显示出来。我的代码如下 item_contact.xmlAndroid 回收器视图没有显示任何内容,android,android-layout,android-recyclerview,Android,Android Layout,Android Recyclerview,我一直在制作一个使用RecyclerView的应用程序,但它没有显示任何东西。为什么RecyclerView的内容没有显示出来。我的代码如下 item_contact.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" a
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<TextView
android:id="@+id/contact_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="@+id/message_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textSize="10sp" />
</LinearLayout>
请帮助我我写的代码非常好,它的工作没有任何问题,在我的主代码中,RecyclearView高度为零,这就是我没有得到输出的原因。是否在
onCreateView()的末尾返回rootView
在您的代码中?您必须在调用setAdapter()
之前设置LayoutManager
,而不是在它之后设置。是的,我都在代码中设置了…但不知道为什么它没有显示#Rami我尝试过但它不工作我不确定,但请添加rvDraft.setHasFixedSize(true);setlayoutmanagerMy之后的问题是我忘记在适配器中实现getItemCount()。导致了相同的行为,什么也没有出现。谢谢,我已经写了包装内容NestedScrollView,现在我已经给了recyclerview 500dp,现在就显示出来。非常感谢你的回答,这节省了很多时间和压力!
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import java.util.List;
public class ContactsAdapter extends RecyclerView.Adapter<ContactsAdapter.ViewHolder> {
private List<Contact> mContacts;
public ContactsAdapter(List<Contact> contacts) {
mContacts = contacts;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View contactView = inflater.inflate(R.layout.item_contact, parent, false);
ViewHolder viewHolder = new ViewHolder(contactView);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
Contact contact = mContacts.get(position);
TextView textView = viewHolder.nameTextView;
textView.setText(contact.getName());
Button button = viewHolder.messageButton;
if (contact.isOnline()) {
button.setText("Message");
button.setEnabled(true);
} else {
button.setText("Offline");
button.setEnabled(false);
}
}
@Override
public int getItemCount() {
return mContacts.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView nameTextView;
public Button messageButton;
public ViewHolder(View itemView) {
super(itemView);
nameTextView = (TextView) itemView.findViewById(R.id.contact_name);
messageButton = (Button) itemView.findViewById(R.id.message_button);
}
}
}
import java.util.ArrayList;
import java.util.List;
public class Contact {
private String mName;
private boolean mOnline;
public Contact(String name, boolean online) {
mName = name;
mOnline = online;
}
public String getName() {
return mName;
}
public boolean isOnline() {
return mOnline;
}
private static int lastContactId = 0;
public static List<Contact> createContactsList(int numContacts) {
List<Contact> contacts = new ArrayList<Contact>();
for (int i = 1; i <= numContacts; i++) {
contacts.add(new Contact("Person " + ++lastContactId, i <= numContacts / 2));
}
return contacts;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
RecyclerView rvContacts = (RecyclerView) rootView.findViewById(R.id.view);
ContactsAdapter adapter = new ContactsAdapter(Contact.createContactsList(20));
rvContacts.setAdapter(adapter);
rvContacts.setLayoutManager(new LinearLayoutManager(getContext()));