RecylerView项目折叠数据显示

时间:2018-05-02 06:29:03

标签: java android android-recyclerview

我有一个RecyclerView,当用户选择加载更多数据时,它包含从REST API响应中显示的项目。但是绑定到onBindViewHolder的数据会在项目上折叠。 折叠数据显示在下图中:

The orange and black text are come from different data list

当用户选择加载更多数据时,将数据列表设置为适配器的代码:

 mAdapter = new MaterialsRecyclerView(username, datePost, contentPost,
                        userPicturePost, contentType, titlePost, contentLabel, activityLabel, numberFavorite,
                        numberUpvote, numberDownvote, numberComments, upvoteStatus, downvoteStatus, favoriteStatus, getActivity(),
                        newsType, newsMedia, contentId, activityType
                );

                mRecyclerView.setAdapter(mAdapter);

我试过

mAdapter.notifyDataSetChanged();

将数据添加到适配器中的数据列表但数据仍然崩溃。

我的问题是为什么崩溃的数据发生了?以及如何使数据不崩溃?我的意思是刚从数据列表加载的数据适合索引。

修改

适配器类代码(Constructor)方法:

public MaterialsRecyclerView(List<String> usernameListParam, List<String> datePostListParam,
                             List<String> contentPostListParam, List<String> userPictureProfileListParam,
                             List<String> contentTypeListParam, List<String> titlePostListParam,
                             List<String> contentLabelListParam, List<String> activityLabelListParam,
                             List<Integer> textNumberFavoriteListParam, List<Integer> textNumberUpvoteListParam,
                             List<Integer> textNumberDownvoteListParam, List<Integer> textNumberCommentListParam,
                             List<Boolean> statusUpvotesParam, List<Boolean> statusDownvotesParam,
                             List<Boolean> statusFavoritesParam, Activity activity, List<Integer> newsTypeListParam,
                             List<String> newsMediaListParam, List<String> contentIdListParam,
                             List<Integer> activityCodeListParam) {

    this.activity = activity;
    usernameList = usernameListParam;
    datePostList = datePostListParam;
    contentPostList = contentPostListParam;
    userPictureProfileList = userPictureProfileListParam;
    contentTypeList = contentTypeListParam;
    titlePostList = titlePostListParam;
    contentTextList = contentLabelListParam;
    activityTextList = activityLabelListParam;
    textNumberDownvoteList = textNumberDownvoteListParam;
    textNumberUpvoteList = textNumberUpvoteListParam;
    textNumberFavoriteList = textNumberFavoriteListParam;
    textNumberCommentList = textNumberCommentListParam;
    statusUpvoteList = statusUpvotesParam;
    statusDownvoteList = statusDownvotesParam;
    statusFavoriteList = statusFavoritesParam;
    newsTypeList = newsTypeListParam;
    newsMediaList = newsMediaListParam;
    contentIdList = contentIdListParam;
    activityCodeList = activityCodeListParam;

    username = new String[usernameList.size()];
    datePost = new String[datePostList.size()];
    titlePost = new String[titlePostList.size()];
    contentPost = new String[contentPostList.size()];
    userPictureProfile = new String[userPictureProfileList.size()];
    contentType = new String[contentTypeList.size()];
    contentText = new String[contentTextList.size()];
    activityText = new String[activityTextList.size()];
    textNumberFavorite = new Integer[textNumberFavoriteList.size()];
    textNumberUpvote = new Integer[textNumberUpvoteList.size()];
    textNumberDownvote = new Integer[textNumberDownvoteList.size()];
    textNumberComment = new Integer[textNumberCommentList.size()];
    statusUpvote = new Boolean[statusUpvoteList.size()];
    statusDownvote = new Boolean[statusDownvoteList.size()];
    statusFavorite = new Boolean[statusFavoriteList.size()];
    newsType = new Integer[newsTypeList.size()];
    newsMedia = new String[newsMediaList.size()];
    contentId = new String[contentIdList.size()];
    activityCode = new Integer[activityCodeList.size()];

    username = usernameList.toArray(username);
    datePost = datePostList.toArray(datePost);
    titlePost = titlePostList.toArray(titlePost);
    contentPost = contentPostList.toArray(contentPost);
    userPictureProfile = userPictureProfileList.toArray(userPictureProfile);
    contentType = contentTypeList.toArray(contentType);
    contentText = contentTextList.toArray(contentText);
    activityText = activityTextList.toArray(activityText);
    textNumberFavorite = textNumberFavoriteList.toArray(textNumberFavorite);
    textNumberUpvote = textNumberUpvoteList.toArray(textNumberUpvote);
    textNumberDownvote = textNumberDownvoteList.toArray(textNumberDownvote);
    textNumberComment = textNumberCommentList.toArray(textNumberComment);
    statusUpvote = statusUpvoteList.toArray(statusUpvote);
    statusDownvote = statusDownvoteList.toArray(statusDownvote);
    statusFavorite = statusFavoriteList.toArray(statusFavorite);
    newsType = newsTypeList.toArray(newsType);
    newsMedia = newsMediaList.toArray(newsMedia);
    contentId = contentIdList.toArray(contentId);
    activityCode = activityCodeList.toArray(activityCode);

    //notifyDataSetChanged();

}

适配器xml代码:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:background="@color/putih"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:grid="http://schemas.android.com/tools"
android:gravity="center_vertical"
android:layout_margin="8dp">

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_gravity="center"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="4dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <RelativeLayout
            android:id="@+id/main_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:orientation="vertical">

            <View
                android:id="@+id/label_color_news"
                android:layout_width="match_parent"
                android:layout_height="3dp"
                android:background="@drawable/news_dashboard_mark"
                android:visibility="invisible"
                />
            <View
                android:id="@+id/label_color_material"
                android:layout_width="match_parent"
                android:layout_height="3dp"
                android:background="@drawable/material_dashboard_mark"
                android:visibility="invisible"
                />
            <ImageView
                android:id="@+id/user_picture"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_marginTop="10dp"
                android:foregroundGravity="right"
                android:layout_below="@+id/label_color_news"
                android:src="@drawable/recycleview_user" />
            <TextView
                android:id="@+id/username"
                android:layout_width="wrap_content"
                android:text="@string/username_label"
                android:layout_alignTop="@id/user_picture"
                android:layout_toRightOf="@id/user_picture"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:layout_marginLeft="5dp"
                android:textColor="@color/abu"/>
            <TextView
                android:id="@+id/activity_username"
                android:layout_width="wrap_content"
                android:text="@string/username_activity_label"
                android:layout_alignTop="@id/user_picture"
                android:layout_toRightOf="@id/username"
                android:layout_height="wrap_content"
                android:layout_marginLeft="4dp"
                android:textColor="@color/abu"/>
            <ImageView
                android:id="@+id/info_button_content_activity"
                android:layout_width="wrap_content"
                android:layout_alignTop="@id/username"

                android:layout_alignParentRight="true"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_info_outline_black_18dp"
                android:layout_marginLeft="5dp"
                android:visibility="invisible"
                />
            <TextView
                android:id="@+id/date_post"
                android:layout_width="120dp"
                android:text="@string/date_label"
                android:layout_below="@+id/username"
                android:layout_toRightOf="@+id/user_picture"
                android:layout_height="wrap_content"
                android:textSize="11sp"
                android:layout_marginLeft="5dp"
                android:textColor="@color/abu"/>
            <TextView
                android:id="@+id/post_label"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:gravity="center_horizontal"
                android:foregroundGravity="center_horizontal"
                android:layout_below="@+id/user_picture"
                android:layout_marginLeft="10dp"
                android:layout_alignTop="@+id/relativelayout_not_news_content"
                android:layout_alignParentRight="true"
                android:text="@string/material_author_label"
                android:visibility="invisible"
                />

            <TextView
                android:id="@+id/post_activity_desc"
                android:visibility="invisible"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/user_picture"
                android:layout_alignTop="@+id/relativelayout_not_news_content"
                android:layout_alignParentRight="true"
                android:text="@string/date_label"
                android:textStyle="bold"
                />
            <TextView
                android:id="@+id/post_content_desc"
                android:visibility="invisible"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/post_activity_desc"
                android:layout_alignParentRight="true"
                android:text="@string/material_author_label"
                android:textStyle="bold"
                />
            <RelativeLayout
                android:id="@+id/relativelayout_not_news_content"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/user_picture"
                android:visibility="gone">
            <TextView
                android:id="@+id/title_post"
                android:layout_marginTop="10dp"
                android:layout_width="match_parent"
                android:textStyle="bold"
                android:textSize="20sp"
                android:text="@string/post_title_label"

                android:layout_height="wrap_content"
                android:textColor="@color/oranyeTerang"/>
            <TextView
                android:id="@+id/content_post"
                android:layout_marginTop="10dp"
                android:layout_width="match_parent"
                android:textSize="15sp"
                android:text="@string/post_content_label"
                android:layout_below="@+id/title_post"
                android:textColor="@color/abu"
                android:layout_height="wrap_content"
                />
            </RelativeLayout>
            <RelativeLayout
                android:id="@+id/relativelayout_news_admin"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/user_picture"
                android:visibility="gone">
                <TextView
                    android:id="@+id/title_post_news_admin"
                    android:layout_marginTop="10dp"
                    android:layout_width="match_parent"
                    android:textStyle="bold"
                    android:textColor="@color/oranyeTerang"
                    android:textSize="20sp"
                    android:text="@string/post_title_label"

                    android:layout_height="wrap_content"
                    />
                <TextView
                    android:id="@+id/content_post_news_admin"
                    android:layout_marginTop="10dp"
                    android:layout_width="match_parent"
                    android:textSize="15sp"
                    android:text="@string/post_content_label"
                    android:layout_below="@+id/title_post_news_admin"
                    android:textColor="@color/abu"
                    android:layout_height="wrap_content"
                    />
            </RelativeLayout>
            <RelativeLayout
                android:id="@+id/relativelayout_news_nonadmin_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/user_picture"
                android:visibility="gone">
                <TextView
                    android:id="@+id/title_post_news_nonadmin_text"
                    android:layout_marginTop="10dp"
                    android:layout_width="match_parent"
                    android:textStyle="bold"
                    android:textSize="30sp"
                    android:text="@string/post_title_label"
                    android:layout_height="wrap_content"
                    android:textColor="@color/oranyeTerang"
                    android:visibility="gone"/>
                <TextView
                    android:id="@+id/content_post_nonadmin_text"
                    android:layout_marginTop="10dp"
                    android:layout_width="match_parent"
                    android:textSize="20sp"
                    android:text="@string/post_content_label"
                    android:layout_below="@+id/title_post_news_nonadmin_text"
                    android:textColor="@color/abu"
                    android:layout_height="wrap_content"
                    />
            </RelativeLayout>
            <RelativeLayout
                android:id="@+id/relativelayout_news_nonadmin_media"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/user_picture"
                android:visibility="gone">
                <ImageView
                    android:id="@+id/media_post_news_nonadmin_media"
                    android:layout_marginTop="10dp"
                    android:layout_width="match_parent"
                    android:layout_height="400dp"
                    android:textColor="@color/abu"
                    />
                <TextView
                    android:id="@+id/caption_post_news_nonadmin_media"
                    android:layout_marginTop="10dp"
                    android:layout_width="match_parent"
                    android:textSize="15sp"
                    android:text="@string/post_title_label"
                    android:layout_below="@+id/media_post_news_nonadmin_media"
                    android:layout_height="wrap_content"
                    android:textColor="@color/abu"/>
            </RelativeLayout>
        </RelativeLayout>
        <LinearLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:orientation="horizontal"
            android:padding="10dp"
            android:layout_marginTop="15dp">

            <ImageView
                android:id="@+id/icon_numbers_favorite"
                android:src="@drawable/ic_favorite_black_18dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <TextView
                android:id="@+id/text_number_favorite"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2"
                />
            <Space
                android:layout_width="5dp"
                android:layout_height="wrap_content" />
            <ImageView
                android:id="@+id/icon_numbers_upvote"
                android:src="@drawable/ic_keyboard_arrow_up_black_18dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <TextView
                android:id="@+id/text_numbers_upvote"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="1"
                />
            <Space
                android:layout_width="5dp"
                android:layout_height="wrap_content" />
            <ImageView
                android:id="@+id/icon_numbers_downvote"
                android:src="@drawable/ic_keyboard_arrow_down_black_18dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <TextView
                android:id="@+id/text_numbers_downvote"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="1"
                />

            <TextView
                android:id="@+id/text_comments"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="0 Komentar"
                android:gravity="right"
                android:layout_gravity="right"
                />
        </LinearLayout>
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/awan"></View>
        <LinearLayout
            android:id="@+id/secondary_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:padding="15dp"
            android:gravity="center_horizontal"
            >
                <ImageView
                    android:id="@+id/button_favorite"
                    android:src="@drawable/ic_favorite_border_black_18dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"/>

                <ImageView
                    android:id="@+id/button_comment"
                    android:src="@drawable/ic_chat_bubble_outline_black_18dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:visibility="gone"/>

                <ImageView
                    android:id="@+id/button_upvote"
                    android:src="@drawable/ic_keyboard_arrow_up_black_24dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"/>

                <ImageView
                    android:id="@+id/button_downvote"
                    android:src="@drawable/ic_keyboard_arrow_down_black_24dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"/>


        </LinearLayout>
    </LinearLayout>

</android.support.v7.widget.CardView>

用作数据对象模型的POJO类:

public class DashboardPOJO {
@SerializedName("rc")
public String rc;

@SerializedName("rm")
public String rm;

@SerializedName("skip")
public int skip;

@SerializedName("access_token")
public String access_token;


public DashboardPOJO(int skip, String access_token) {
    this.skip = skip;
    this.access_token = access_token;
}

public String getRm() {
    return rm;
}

@SerializedName("results")
@Expose
private List<DashboardPOJO.Results> results = null;

public List<DashboardPOJO.Results> getResults() {
    return results;
}

public class Results{
    @SerializedName("dashboard")
    @Expose
    private Dashboard dashboard;

    public class Dashboard {
        @SerializedName("content_id")
        @Expose
        private String id;

        @SerializedName("title")
        @Expose
        private String title;

        @SerializedName("desc")
        @Expose
        private String desc;

        @SerializedName("synopsis")
        @Expose
        private String synopsis;

        @SerializedName("content_code")
        @Expose
        private Integer content_code;

        @SerializedName("activity_code")
        @Expose
        private Integer activityCode;

        @SerializedName("post_by")
        @Expose
        private PostBy postBy;

        @SerializedName("user_detail")
        @Expose
        private UserDetail userDetail;

        @SerializedName("files")
        @Expose
        private List<DashboardPOJO.Results.Dashboard.Files> files = null;

        @SerializedName("activity_text")
        @Expose
        private String activityText;

        @SerializedName("content_text")
        @Expose
        private String contentText;

        @SerializedName("news_type")
        @Expose
        private int newsType;

        @SerializedName("upvote")
        @Expose
        private int upvote;

        @SerializedName("downvote")
        @Expose
        private int downvote;

        @SerializedName("favorite")
        @Expose
        private int favorite;

        @SerializedName("upvoted")
        @Expose
        private boolean upvoted;

        @SerializedName("downvoted")
        @Expose
        private boolean downvoted;

        @SerializedName("favorited")
        @Expose
        private boolean favorited;

        @SerializedName("comment")
        @Expose
        private int comment;

        @SerializedName("created_at_from_now")
        @Expose
        private String createdAt;

        public String getId(){
            return id;
        }
        public String getTitle() {
            return title;
        }

        public String getDesc() {
            return desc;
        }

        public String getSynopsis() {
            return synopsis;
        }

        public Integer getContent_code() {
            return content_code;
        }

        public Integer getActivityCode() {
            return activityCode;
        }

        public String getActivityText() {
            return activityText;
        }

        public String getContentText() {
            return contentText;
        }

        public int getNewsType() {
            return newsType;
        }

        public int getUpvote() {
            return upvote;
        }

        public int getDownvote() {
            return downvote;
        }

        public int getFavorite() {
            return favorite;
        }

        //
        public boolean getUpvoted() {
            return upvoted;
        }

        public boolean getDownvoted() {
            return downvoted;
        }

        public boolean getFavorited() {
            return favorited;
        }

        public int getComment() {
            return comment;
        }

        public String getCreatedAt() {
            return createdAt;
        }

        public PostBy getPostBy() {
            return postBy;
        }

        public UserDetail getUserDetail() {
            return userDetail;
        }

        public void setPostBy(PostBy postBy) {
            this.postBy = postBy;
        }

        /*@SerializedName("results")
        @Expose
        private List<DashboardPOJO.Results> results = null;

        public List<DashboardPOJO.Results> getResults() {
            return results;
        }*/

        public List<DashboardPOJO.Results.Dashboard.Files> getFiles() {
            return files;
        }

        public class Files {
            @SerializedName("originalname")
            @Expose
            private String originalName;

            @SerializedName("filename")
            @Expose
            private String filename;

            @SerializedName("http_path")
            @Expose
            private String httpPath;

            public String getHttpPath() {
                return httpPath;
            }

            public String getOriginalName() {
                return originalName;
            }

            public String getFilename() {
                return filename;
            }
        }

        public class UserDetail {
            @SerializedName("display_picture")
            @Expose
            private String displayPicture;

            public String getDisplayPicture() {
                return displayPicture;
            }
        }

        public class PostBy {

            @SerializedName("user_id")
            @Expose
            private String userId;
            @SerializedName("username")
            @Expose
            private String username;

            public String getUserId() {
                return userId;
            }

            public void setUserId(String userId) {
                this.userId = userId;
            }

            public String getUsername() {
                return username;
            }

            public void setUsername(String username) {
                this.username = username;
            }

        }
    }

    public Dashboard getDashboard() {
        return dashboard;
    }
}}

1 个答案:

答案 0 :(得分:0)

在您的布局中,您可以使用

创建多个视图
android:layout_below="@+id/user_picture"

所以他们被吸引到彼此之上