我的回收者视图没有显示我的问题,我想知道我是否忽略了某些东西。以下是相关课程。
SingleFragmentActivity
public abstract class SingleFragmentActivity extends AppCompatActivity {
protected abstract Fragment createFragment();
@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.activity_fragment);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragment_container);
if (fragment == null) {
fragment = createFragment();
fm.beginTransaction().add(R.id.fragment_container, fragment).commit();
}
}
}
ContactListActivity
import android.support.v4.app.Fragment;
import ...fragment.ContactListFragment;
public class ContactListActivity extends SingleFragmentActivity{
@Override
protected Fragment createFragment() {
return new ContactListFragment();
}
}
ContactListFragment
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class ContactListFragment extends Fragment {
private RecyclerView mContactRecyclerView;
private ContactAdapter mAdapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_contact_list, container, false);
mContactRecyclerView = (RecyclerView) v.findViewById(R.id.contact_recycler_view);
mContactRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
updateUI();
return v;
}
private void updateUI() {
ContactList contactList = ContactList.get(getActivity());
List<Contact> contacts = contactList.getContacts();
mAdapter = new ContactAdapter(contacts);
mContactRecyclerView.setAdapter(mAdapter);
}
private class ContactHolder extends RecyclerView.ViewHolder {
private TextView mNameTextView;
private TextView mPhoneNumberTextView;
private Contact mContact;
public ContactHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.list_item_contact, parent, false));
mNameTextView = (TextView) itemView.findViewById(R.id.contact_list_name);
mPhoneNumberTextView = (TextView) itemView.findViewById(R.id.contact_list_phone);
}
public void bind(Contact contact) {
mContact = contact;
mNameTextView.setText(mContact.getmName());
mPhoneNumberTextView.setText(mContact.getmPhone().getmWork());
}
}
private class ContactAdapter extends RecyclerView.Adapter<ContactHolder> {
private List<Contact> mContacts;
public ContactAdapter(List<Contact> contacts) {
mContacts = contacts;
}
@Override
public ContactHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
return new ContactHolder(layoutInflater, parent);
}
@Override
public void onBindViewHolder(ContactHolder holder, int position) {
Contact contact = mContacts.get(position);
holder.bind(contact);
}
@Override
public int getItemCount() {
return mContacts.size();
}
}
}
ContactList
public class ContactList {
private static ContactList sContactList;
private List<Contact> mContacts;
public static ContactList get(Context context) {
if (sContactList == null) {
sContactList = new ContactList(context);
}
return sContactList;
}
private ContactList(Context context) {
mContacts = new ArrayList<>();
for (int i = 0; i < 20; i++) {
Contact contact = new Contact();
Phone phone = new Phone();
phone.setmWork("222-333-7676");
contact.setmName("Andy Android");
contact.setmPhone(phone);
contact.setmSmallImageURL("www.purple.com");
mContacts.add(contact);
}
}
public List<Contact> getContacts() {
return mContacts;
}
@Nullable
public Contact getContact(Phone phone) {
for (Contact contact: mContacts) {
if (contact.getmPhone().equals(phone)) {
return contact;
}
}
return null;
}
}
fragment_contact_list
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/contact_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
list_item_contact
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="@dimen/contact_list_card_height"
android:background="@color/cardview_light_background"
android:layout_margin="@dimen/contact_list_card_margin"
card_view:cardCornerRadius="@dimen/contact_list_card_rounded_corner">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="4"
android:orientation="horizontal">
<ImageView
android:id="@+id/contact_list_image"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:layout_weight="1"
android:src="@drawable/favorite_not_selected"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="3">
<TextView
android:id="@+id/contact_list_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="@dimen/contact_list_name_size"
android:layout_marginStart="@dimen/contact_list_card_text_margin"
android:text=""/>
<TextView
android:id="@+id/contact_list_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/contact_list_card_text_margin"
android:text=""/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
注意省略了一些import语句。
谢谢! Otterman
答案 0 :(得分:0)
原来它与我的回收者视图无关。我的通用SingleFragmentActivity存在缺陷,应该是:
public abstract class SingleFragmentActivity extends AppCompatActivity {
protected abstract Fragment createFragment();
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragment_container);
if (fragment == null) {
fragment = createFragment();
fm.beginTransaction().add(R.id.fragment_container, fragment).commit();
}
}
}