RecyclerView + CardView:膨胀错误数据

RecyclerView + CardView: Inflating Wrong Data

我正在使用 CardView 创建带有时事通讯的卡片列表。基本元素是 3 个 TextView + 1 个 ImageView。我无法正确地膨胀 2 个元素:标题很好,但是 link 文本得到了错误的字符串(它得到了引用 body 文本的字符串)并且 body TextView 没有改变完全没有。

先上一张大意的图片:

preview on android studio (left) and app running on device(right)

这是我的 CardView XML 代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".News">

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card1_view"
    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">

        <TextView
            android:id="@+id/news_title"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:paddingLeft="8dp"
            android:text="articleTitle articleTitle articleTitle articleTitle articleTitle articleTitle articleTitle articleTitle articleTitle articleTitle articleTitle articleTitle articleTitle articleTitle "
            android:textColor="#000000"
            android:textSize="16sp"
            android:textStyle="bold" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="6"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/news_link"
                    android:layout_width="match_parent"
                    android:layout_height="25dp"
                    android:gravity="center_vertical"
                    android:paddingLeft="8dp"
                    android:text="http://www.articleLink.com/" />

                <TextView
                    android:id="@+id/news_body"
                    android:layout_width="match_parent"
                    android:layout_height="120dp"
                    android:paddingLeft="8dp"
                    android:text="Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body ..."
                    android:textColor="#000000" />
            </LinearLayout>

            <ImageView
                android:id="@+id/news_photoId"
                android:layout_width="0dp"
                android:layout_height="145dp"
                android:layout_weight="5"
                android:src="@drawable/enfnotum" />

        </LinearLayout>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:gravity="end"
            android:paddingEnd="6dp"
            android:paddingRight="6dp"
            android:text="Coments    Share" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#bab7b7" />

    </LinearLayout>


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

RecycleView 列表布局XML代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".News">

<android.support.v7.widget.RecyclerView
    android:id="@+id/rv"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

现在 java 类: 我在这里填充列表,调用适配器

 public class News extends AppCompatActivity {

    private List<Article> articles = new ArrayList<>();
    private RecyclerView rv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news);

        rv = (RecyclerView) findViewById(R.id.rv);
        LinearLayoutManager llm = new LinearLayoutManager(this);
        rv.setLayoutManager(llm);

        initializeData();
        initializeAdapter();
    }

    private void initializeData() {
        articles.add(new Article("0title", "0link", "0body", R.drawable.enfnotum));
        articles.add(new Article("1title", "1link", "1body", R.drawable.enfnoticias));
        articles.add(new Article("2title", "2link", "2body", R.drawable.image1));
        articles.add(new Article("3title", "3link", "3body", R.drawable.image2));
        articles.add(new Article("4title", "4link", "4body", R.drawable.image3));
        articles.add(new Article("5title", "5link", "5body", R.drawable.enfnotum));
        articles.add(new Article("6title", "6link", "6body", R.drawable.image3));
        articles.add(new Article("7title", "7link", "7body", R.drawable.enfnoticias));
        //TODO: database
    }

    private void initializeAdapter() {
        NewsRVAdapter adapter = new NewsRVAdapter(articles);
        rv.setAdapter(adapter);
    }
}

我的班级要创建列表object,几个吸气剂

    public class Article {

    private String articleTitle;
    private String articleLink;
    private String articleBody;
    private int articlePhotoId;

    public Article(String articleTitle, String articleLink, String articleBody, int articlePhotoId) {
        this.articleTitle = articleTitle;
        this.articleLink = articleLink;
        this.articleBody = articleBody;
        this.articlePhotoId = articlePhotoId;
    }

    public String getArticleTitle() {
        return articleTitle;
    }

    public String getArticleLink() {
        return articleLink;
    }

    public String getArticleBody() {
        return articleBody;
    }

    public int getArticlePhotoId() {
        return articlePhotoId;
    }
}

膨胀布局

    public class NewsRVAdapter extends RecyclerView.Adapter<NewsRVAdapter.ArticleViewHolder> {

    List<Article> articles;

    NewsRVAdapter(List<Article> articles) {
        this.articles = articles;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    @Override
    public ArticleViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.news_item_list, viewGroup, false);
        return new ArticleViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ArticleViewHolder ArticleViewHolder, int position) {
        ArticleViewHolder.articleTitle.setText(articles.get(position).getArticleTitle());
        ArticleViewHolder.articleLink.setText(articles.get(position).getArticleLink());
        ArticleViewHolder.articleLink.setText(articles.get(position).getArticleBody());
        ArticleViewHolder.articlePhotoID.setImageResource(articles.get(position).getArticlePhotoId());
    }

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

    public static class ArticleViewHolder extends RecyclerView.ViewHolder {

        CardView cv;
        TextView articleTitle;
        TextView articleLink;
        TextView articleBody;
        ImageView articlePhotoID;

        ArticleViewHolder(View itemView) {
            super(itemView);
            cv = (CardView) itemView.findViewById(R.id.card1_view);
            articleTitle = (TextView) itemView.findViewById(R.id.news_title);
            articleLink = (TextView) itemView.findViewById(R.id.news_link);
            articleBody = (TextView) itemView.findViewById(R.id.news_body);
            articlePhotoID = (ImageView) itemView.findViewById(R.id.news_photoId);
        }
    }
}

看起来像是打字错误。尝试将 NewsRVAdapteronBindViewHolder 替换为:

@Override
public void onBindViewHolder(ArticleViewHolder ArticleViewHolder, int position) {
ArticleViewHolder.articleTitle.setText(articles.get(position).getArticleTitle());
ArticleViewHolder.articleLink.setText(articles.get(position).getArticleLink());
ArticleViewHolder.articleBody.setText(articles.get(position).getArticleBody());
ArticleViewHolder.articlePhotoID.setImageResource(articles.get(position).getArticlePhotoId());
}

您在 onBindViewHolder 上有误。您引用了 articleLink 两次。

应该是

@Override
public void onBindViewHolder(ArticleViewHolder ArticleViewHolder, int position){
    ArticleViewHolder.articleTitle.setText(articles.get(position).getArticleTitle());
    ArticleViewHolder.articleLink.setText(articles.get(position).getArticleLink());
    ArticleViewHolder.articleBody.setText(articles.get(position).getArticleBody());
    ArticleViewHolder.articlePhotoID.setImageResource(articles.get(position).getArticlePhotoId());
}

原因是你在onBindViewHolder中的代码错误^^

@Override
public void onBindViewHolder(ArticleViewHolder ArticleViewHolder, int position) {
    ArticleViewHolder.articleTitle.setText(articles.get(position).getArticleTitle());
    ArticleViewHolder.**articleLink**.setText(articles.get(position).getArticleLink());
    ArticleViewHolder.**articleLink**.setText(articles.get(position).getArticleBody());
    ArticleViewHolder.articlePhotoID.setImageResource(articles.get(position).getArticlePhotoId());
}

ArticleViewHolder.articleLink 被分配了两次,因此它的值被覆盖了。

更正它,您的代码可以正常工作:)