为什么我的RecyclerView没有显示任何内容? /如果在notifyDataSetChanged()调用后再次设置Recycler Adapter,会发生什么?

时间:2016-02-04 10:22:20

标签: java android android-recyclerview

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

2 个答案:

答案 0 :(得分:1)

除了以下代码中的额外行friendList.setAdapter(friendListAdapter);外,其他所有内容都很正常:

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

用这个替换上面的3行,并参见:

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

答案 1 :(得分:1)

onViewCreated()方法

中删除以下行
friendList.setAdapter(friendListAdapter);  

其他代码没问题。