RecyclerView项目未显示

时间:2017-10-05 17:34:39

标签: android

我对此感到非常沮丧。我不明白为什么我的CardView

中没有显示RecyclerView网格项

以下是CardView网格项fragment_album_cardview_item.xml

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    card_view:cardUseCompatPadding="true"
    card_view:cardCornerRadius="8dp"
    android:layout_marginBottom="16dp"
    card_view:cardBackgroundColor="@android:color/white">

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

这里我设置了适配器

public void setGridViewAdapter(){

    GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 2);

    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setLayoutManager(layoutManager);

    mRecyclerViewAdapter = new RecyclerViewAdapter(App.mApp.mAlbums);
    mRecyclerView.setAdapter(mRecyclerViewAdapter);

}

这是我的适配器

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.Holder> {

    private List<Album> itemList;
    //private Context context;

    // TODO adapter may be initialized by the time some of the albums have been added to the list
    public RecyclerViewAdapter(List<Album> itemList) {
        this.itemList = itemList;
        Timber.d("itemList.size = "+String.valueOf(itemList.size()));
        Timber.d("itemList.get(0).name = "+itemList.get(0).name);
        //this.context = context;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {

        Timber.d("onCreateViewHolder");

        View cardView = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_album_cardview_item, null);
        Timber.d("cardView = "+String.valueOf(cardView));
        Holder rcv = new Holder(cardView);
        return rcv;
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {
        //holder.countryName.setText(itemList.get(position).getName());
        //holder.countryPhoto.setImageResource(itemList.get(position).getPhoto());
    }

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

    // HOLDER

    public class Holder extends RecyclerView.ViewHolder implements View.OnClickListener{

        public TextView countryName;
        public ImageView countryPhoto;

        public Holder(View itemView) {
            super(itemView);
            //itemView.setOnClickListener(this);
            //countryName = (TextView)itemView.findViewById(R.id.country_name);
            //countryPhoto = (ImageView)itemView.findViewById(R.id.country_photo);
        }

        @Override
        public void onClick(View view) {
            Toast.makeText(view.getContext(), "Clicked Country Position = " + getPosition(), Toast.LENGTH_SHORT).show();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

显然,只需在我的CardView中添加某种小部件,例如TextView,就会显示一些网格项

<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="10dp"
    android:layout_height="100dp"
    card_view:cardUseCompatPadding="true"
    card_view:cardCornerRadius="8dp"
    android:layout_marginBottom="16dp"
    card_view:cardBackgroundColor="@android:color/white">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Bleh"/>

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