简体   繁体   English

无法在RecyclerView中显示项目

[英]Can't display items in RecyclerView

I am having problems in displaying cards dynamically from a recycler view. 我在回收站视图中动态显示卡片时遇到问题。 Here's my code. 这是我的代码。

CardActivity.java 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 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 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 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 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>

Change this 改变这个

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

to

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

change here- 在这里改变 -

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

You have to declare how much views you want to inflate in your view. 您必须在视图中声明要扩展的视图数量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM