gpt4 book ai didi

android - 回收站 View 不显示任何内容

转载 作者:太空狗 更新时间:2023-10-29 16:13:55 24 4
gpt4 key购买 nike

我一直在制作一个使用 RecyclerView 的应用程序,但它没有显示任何东西..为什么没有显示回收器 View 的内容。我的代码如下所示

item_contact.xml

<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>

联系人适配器.java

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);
}
}
}

联系人.java

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;
}
}

主 Activity .java

@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()));

请帮帮我

最佳答案

我编写的代码非常好并且工作没有任何问题,在我的主要代码中 RecyclearView 高度为零,这就是我没有得到输出的原因。

关于android - 回收站 View 不显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35255166/

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