簡體   English   中英

為什么我的RecyclerView沒有顯示任何內容? /如果在調用notifyDataSetChanged()之后再次設置了回收站適配器,會發生什么情況?

[英]Why is my RecyclerView not showing anything? / What happend if Recycler Adapter is set again after notifyDataSetChanged() called?

我是Android編程的新手,我一直在嘗試制作一個片段,該片段使用RecyclerView顯示項目列表。 一切似乎都很好,當我運行該應用程序時沒有錯誤。

這是我的代碼:

FriendsFragment.java

public class FriendsFragment extends android.support.v4.app.Fragment {

    @Bind(R.id.friendList)
    RecyclerView friendList;
    RecyclerView.LayoutManager friendsManager;
    FriendListAdapter friendListAdapter;

    public FriendsFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_friends, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        friendList.setHasFixedSize(true);
        friendsManager = new LinearLayoutManager(view.getContext());
        friendList.setLayoutManager(friendsManager);
        friendListAdapter  = new FriendListAdapter(new ArrayList<Friends>());
        friendList.setAdapter(friendListAdapter);
        test();
        friendListAdapter.notifyDataSetChanged();
        friendList.setAdapter(friendListAdapter);
        super.onViewCreated(view, savedInstanceState);
    }

    private void test() {       
        Friends friend = new Friends();
        friend.setName("Junjie Saitamaria");
        friendListAdapter.addNewFriend(friend);
    }
}

FriendListAdapter.java

public class FriendListAdapter extends RecyclerView.Adapter<FriendListAdapter.ViewHolderFriend> {

    private List<Friends> friendList;

    public FriendListAdapter(List<Friends> friends) {
        if(friends != null) {
            friendList = friends;
        }
    }

    public void addNewFriend(Friends friend) {
        friendList.add(0, friend);
    }

    @Override
    public ViewHolderFriend onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_item_friend, parent, false);
        ViewHolderFriend viewHolder = new ViewHolderFriend(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolderFriend holder, int position) {
        Friends currentFriend = friendList.get(position);
        holder.friendName.setText(currentFriend.getName());
    }

    @Override
        public int getItemCount() {
        return friendList.size();
    }

    static class ViewHolderFriend extends RecyclerView.ViewHolder {
        @Bind(R.id.friend_avatar)
        public ImageView friendThumbnail;
        @Bind(R.id.friend_name)
        public TextView friendName;

        Context currentContext = null;

        public ViewHolderFriend(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);            
            this.currentContext = itemView.getContext();
        }
    }
}

fragment_friends.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    tools:context="com.cebuinstituteoftechnology_university.citumessenger.FriendsFragment">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/friendList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true" >
    </android.support.v7.widget.RecyclerView>
</RelativeLayout>

list_item_friend.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:id="@+id/friends_card"
        android:elevation="2dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <!-- icon -->
            <ImageView
                android:id="@+id/friend_avatar"
                android:layout_width="64dp"
                android:layout_height="64dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="1dp"
                android:layout_marginBottom="1dp"
                android:contentDescription="icon"
                android:src="@drawable/avatar"
                />

            <!-- title -->
            <TextView
                android:id="@+id/friend_name"
                android:layout_width= "wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:layout_toRightOf="@+id/friend_avatar"
                android:layout_alignBaseline="@+id/friend_avatar"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp"
                android:text="Name" />
        </RelativeLayout>
    </android.support.v7.widget.CardView>

</LinearLayout>

看起來一切都很好,除了這多余的一行friendList.setAdapter(friendListAdapter); 在下面的代碼中:

friendList.setAdapter(friendListAdapter);
test();
friendListAdapter.notifyDataSetChanged();
friendList.setAdapter(friendListAdapter);  

替換上面的三行,然后看:

friendList.setAdapter(friendListAdapter);
test();
friendListAdapter.notifyDataSetChanged();

onViewCreated()方法中刪除以下行

friendList.setAdapter(friendListAdapter);  

其他代碼也可以。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM