运行应用程序时,我的cardView布局没有显示

时间:2017-02-02 11:27:20

标签: android android-recyclerview android-cardview

您好我创建了一个使用cardview和recycler View的简单应用程序来显示图像。

现在,当我运行应用程序时,只显示一个带有应用程序名称的空白应用程序

这是代码

主要活动文件

public class MainActivity extends AppCompatActivity {

RecyclerView recyclerView;
List<listItem> item;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView = (RecyclerView) findViewById(R.id.recycler_Container);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    myAdapter adapter = new myAdapter(dataFile.getListData());
    recyclerView.setAdapter(adapter);



}}

适配器文件

public class myAdapter extends RecyclerView.Adapter<myAdapter.pageHolder> {

List<listItem> listData;
LayoutInflater inflater;

public myAdapter(List<listItem> listData) {
    this.listData = listData;
}

public class pageHolder extends RecyclerView.ViewHolder{

    ImageView image;
    TextView text;
    View container;

   public pageHolder(View itemView) {
       super(itemView);

       image = (ImageView) itemView.findViewById(R.id.thumbnail);
       text = (TextView) itemView.findViewById(R.id.card_text);
       container = itemView.findViewById(R.id.layout_container);

   }}

@Override
public myAdapter.pageHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card,parent,false);
    return new pageHolder(view);
}

@Override
public void onBindViewHolder(myAdapter.pageHolder holder, int position) {

    listItem item =listData.get(position);
    holder.text.setText(item.getTitle());
    holder.image.setImageResource(item.getIconId());

}

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

列出项目文件

public class listItem {

String title;
int iconId;

public listItem(int iconId, String title) {
    this.iconId = iconId;
    this.title = title;
}

public int getIconId() {
    return iconId;
}

public void setIconId(int iconId) {
    this.iconId = iconId;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}}

数据文件

public class dataFile {


public static int icons[]={R.drawable.album9,R.drawable.album1,R.drawable.album3,R.drawable.album4,R.drawable.album5,R.drawable.album6,
        R.drawable.album7,R.drawable.album8,R.drawable.album10,R.drawable.album11,R.drawable.album12,R.drawable.album13,R.drawable.album14,R.drawable.album15,
        R.drawable.album16};

public static String title[] = {"Album"};


public static List<listItem> getListData(){

    List<listItem> data = new ArrayList<>();
    for(int i =0;i<icons.length;i++){
        new listItem(icons[i],"ALBUM"+i);
    }
   return data;
}}

主XML文件

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.adi.customcardview.MainActivity">
<android.support.v7.widget.RecyclerView
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:padding="16dp"
    android:id="@+id/recycler_Container"/></RelativeLayout>

enter image description here

卡片布局XML文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/layout_container"
    app:cardCornerRadius="5dp"
    app:cardBackgroundColor="#efdada"
    app:cardElevation="5dp">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:id="@+id/thumbnail"
            android:padding="12dp"
            android:src="@drawable/album9"/>
        <TextView
            android:text="ALBUM NAME"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="55dp"
            android:id="@+id/card_text"
            android:textSize="18sp"
            android:layout_alignParentTop="true"
            android:layout_toEndOf="@+id/thumbnail"/>
    </RelativeLayout></android.support.v7.widget.CardView></LinearLayout>

1 个答案:

答案 0 :(得分:1)

据我所知,在getListData方法中,您没有将listItem元素添加到数据列表中。因此,请尝试按如下方式更改方法:

public static List<listItem> getListData(){

    List<listItem> data = new ArrayList<>();
    for(int i =0;i<icons.length;i++){
        data.add(new listItem(icons[i],"ALBUM"+i));
    }
   return data;
}