无法在RecyclerView中显示项目

时间:2016-05-20 16:57:49

标签: java android android-studio android-recyclerview

我在回收站视图中动态显示卡片时出现问题。这是我的代码。

CardActivity.java

public class CardActivity extends AppCompatActivity {

CardAdapter mAdapter;
RecyclerView mRecyclerView;

ArrayList<CardModel> data = new ArrayList<>();

public static String IMGS[] = {
        "https://scontent.fmnl4-2.fna.fbcdn.net/v/t1.0-9/12540788_486126334923939_641652950626105372_n.jpg?oh=520090fa887ded912ddb7086fc69fc93&oe=57A04969",
        "https://scontent.fmnl4-2.fna.fbcdn.net/t31.0-8/12973436_521081094761796_6679453535369186441_o.jpg",
        "https://scontent.fmnl4-2.fna.fbcdn.net/v/t1.0-9/12191632_465602330309673_5460380145671805117_n.jpg?oh=51811b46395fee6e4bdb5394e6725591&oe=57D35DC3",
        "https://scontent.fmnl4-2.fna.fbcdn.net/v/t1.0-9/10628472_397420133794560_5446805358922772021_n.jpg?oh=f23ab8761e05b2a50d0d0c9dec4d365b&oe=57DBF1CC",
        "https://scontent.fmnl4-2.fna.fbcdn.net/t31.0-8/10697227_314766035393304_2937143993369666506_o.jpg",
        "https://scontent.fmnl4-2.fna.fbcdn.net/v/t1.0-9/13133194_10154674163886840_3764712620850385571_n.jpg?oh=252cedf88040188f12ce99c29f3dd47e&oe=57DD7474"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_card);

    for (int i = 0; i < IMGS.length; i++) {

        CardModel cardModel = new CardModel();
        cardModel.setTitle("Card Title: " + i);
        cardModel.setDescription("This is a card description");
        cardModel.setUrl(IMGS[i]);
        data.add(cardModel);
    }

    Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setTitle("Cards");


    mRecyclerView = (RecyclerView) findViewById(R.id.cards);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(CardActivity.this);
    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    mRecyclerView.setLayoutManager(linearLayoutManager);
    mRecyclerView.setHasFixedSize(true);


    mAdapter = new CardAdapter(CardActivity.this, data);
    mRecyclerView.setAdapter(mAdapter);
}

public boolean onOptionsItemSelected(MenuItem item) {
    onBackPressed();
    return true;
}
}

CardModel.java

public class CardModel {

String url,title,description;
public CardModel() {

}

public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
}

public String getTitle() {
    return title;
}

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

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}
}

CardAdapter.java

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.CardHolder> {

private LayoutInflater inflater;
Context context;
List<CardModel> data = new ArrayList<>();

public CardAdapter(Context context, List<CardModel> data){
    this.context = context;
    this.data = data;
    inflater = LayoutInflater.from(context);
}

@Override
public CardHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.card_layout,parent,false);
    CardHolder cardHolder = new CardHolder(view);
    return cardHolder;
}

@Override
public void onBindViewHolder(CardHolder holder, int position) {
    holder.cardTitle.setText(data.get(position).getTitle());
    holder.cardDescription.setText(data.get(position).getDescription());
    Glide.with(context).load(data.get(position).getUrl())
            .fitCenter()
            .into(holder.cardImage);
}


@Override
public int getItemCount() {
    return 0;
}

public static class CardHolder extends RecyclerView.ViewHolder {
    ImageView cardImage;
    TextView cardTitle;
    TextView cardDescription;


    public CardHolder(View cardView) {
        super(cardView);

        cardImage = (ImageView) cardView.findViewById(R.id.card_image);

        cardTitle = (TextView) cardView.findViewById(R.id.card_title);

        cardDescription = (TextView) cardView.findViewById(R.id.card_description);
    }

}
}

activity_card.xml

<FrameLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.braudy.android.mesasixprofiler.CardActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <include layout="@layout/toolbar"/>

</android.support.design.widget.AppBarLayout>

<android.support.v7.widget.RecyclerView
    android:id="@+id/cards"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</FrameLayout>

card_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#FFFFFF"
android:padding="10dp">


<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:background="#ffffff">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:alpha=".7"
            android:id="@+id/card_image"
            android:background="#ffff66"
            android:src="@drawable/img1"
            android:scaleType="centerCrop"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Braudy"
            android:paddingLeft="15dp"
            android:textColor="#FFFFFF"
            android:background="#707070"
            android:alpha=".8"
            android:textSize="25sp"
            android:id="@+id/card_title"
            android:layout_alignBottom="@+id/card_image"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:text="This is a description"
            android:paddingTop="5dp"
            android:paddingLeft="15dp"
            android:textColor="#A9A9AF"
            android:textSize="15sp"
            android:id="@+id/card_description"
            android:layout_below="@+id/card_image"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

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

2 个答案:

答案 0 :(得分:2)

更改此

@Override
public int getItemCount() {
    return 0;
}

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

答案 1 :(得分:1)

在这里改变 -

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

您必须在视图中声明要扩充的视图数量。