片段中的CardView是不可见的

时间:2015-08-22 01:06:42

标签: java android android-cardview

我有一个主要活动(ActivityMain.java),我想用它在四个片段之间导航。在其中一个片段中,我尝试将CardView与RecyclerView结合使用以创建垂直列表。但是,到目前为止,我在运行应用程序时无法显示任何显卡。 CardView将在Android Studio的设计预览中完美显示,但是当使用实际的设备/模拟器时,它会消失。

我尝试通过Java手动设置CardView的可见性,但它仍然保持隐身状态。我相信片段的布局可以覆盖CardView的布局,但我对Android开发还是一个新手,所以我不能完全确定问题是什么。

下面我粘贴了可能与我的问题相关的类和XML文件。

AdapterMainFeed.java

public class AdapterMainFeed extends RecyclerView.Adapter<AdapterMainFeed.ViewHolderMainFeed> {
    private ArrayList<Article> listArticlesMain = new ArrayList<>();
    private LayoutInflater inflater;

    public AdapterMainFeed(Context context) {
        inflater = LayoutInflater.from(context);
    }

    public void setArticlesMain(ArrayList<Article> listArticlesMain) {
        this.listArticlesMain = listArticlesMain;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolderMainFeed onCreateViewHolder(ViewGroup container, int i) {
        View layout = inflater.inflate(R.layout.view_main_feed, container, false);
        Article article1 = new Article("asdf", "ghj", "klm", new Date(0));
        Article article2 = new Article("sdfg", "hjk", "lmn", new Date(0));
        Article article3 = new Article("dfgh", "jkl", "mno", new Date(0));
        listArticlesMain.add(article1);
        listArticlesMain.add(article2);
        listArticlesMain.add(article3);
        return new ViewHolderMainFeed(layout);
    }

    @Override
    public void onBindViewHolder(ViewHolderMainFeed viewHolderMainFeed, int i) {
        Article currentArticle = listArticlesMain.get(i);
        viewHolderMainFeed.articleTitle.setText(currentArticle.getTitle());
        viewHolderMainFeed.articleAuthor.setText(currentArticle.getAuthor());
        viewHolderMainFeed.articleWebsite.setText(currentArticle.getWebsite());
        DateFormat formatter = DateFormat.getDateTimeInstance();
        final String timePosted = formatter.format(currentArticle.getTimePosted());
        viewHolderMainFeed.articleTime.setText(timePosted);
    }

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

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

    static class ViewHolderMainFeed extends RecyclerView.ViewHolder {
        TextView articleTitle;
        TextView articleAuthor;
        TextView articleWebsite;
        TextView articleTime;

        public ViewHolderMainFeed(View itemView) {
            super(itemView);
            articleTitle = (TextView) itemView.findViewById(R.id.mainArticleTitle);
            articleAuthor = (TextView) itemView.findViewById(R.id.mainArticleAuthor);
            articleWebsite = (TextView) itemView.findViewById(R.id.mainArticleWebsite);
            articleTime = (TextView) itemView.findViewById(R.id.mainArticleTime);
        }
    }  
}

view_main_feed.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8dp"
    tools:context="com.convergeapp.converge.ActivityMain">

   <android.support.v7.widget.CardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        card_view:cardCornerRadius="7dp"
        card_view:cardBackgroundColor="@color/colorPurpleSeance"
        android:id="@+id/mainArticleCard"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:padding="8dp"
        android:clickable="true">

        <RelativeLayout
            android:id="@+id/mainArticleLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="16dp">

            <TextView
                android:id="@+id/mainArticleTitle"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"/>

            <TextView
                android:id="@+id/mainArticleAuthor"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_below="@id/mainArticleTitle"/>

            <TextView
                android:id="@+id/mainArticleWebsite"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_below="@+id/mainArticleTime"/>

            <TextView
                android:id="@+id/mainArticleTime"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"/>
        </RelativeLayout>
    </android.support.v7.widget.CardView>
</RelativeLayout>

2 个答案:

答案 0 :(得分:2)

在玩了一些变化后,我终于找到了解决方案。我所做的是将Article对象添加到主片段的onCreateView方法的列表中。另外,我在ViewHolder中实例化了CardView,虽然这最初没有帮助我看到这些卡片。

所以,如果你遇到类似的困境并且无法找到解决方案,请在设置之前尝试实际创建对象。

答案 1 :(得分:1)

你需要调用notifyDataSetChanged();在添加新文章之后在onCreateViewHolder中,以便更新项目计数