RecyclerView + CardView:膨胀错误的数据

时间:2016-06-14 03:45:32

标签: java android android-recyclerview android-cardview layout-inflater

我使用CardView创建一个带有简报的卡片列表。基本元素是3个TextViews + 1个ImageView。我不能正确地膨胀2个元素:标题很好,但链接文本得到错误的字符串(它获取指向正文的字符串)和正文TextView根本不会改变。

Aeres带有一般意识的照片:

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);
    }
}

我创建列表对象,很少有吸气剂

    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);
        }
    }
}

3 个答案:

答案 0 :(得分:1)

看起来像拼写错误。尝试将NewsRVAdapter onBindViewHolder替换为:

@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());
}

答案 1 :(得分:1)

原因是 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 已分配两次,因此其值已被覆盖。

纠正它并且您的代码正常工作:)

答案 2 :(得分:0)

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());
}