Cardview未在recyclerview

时间:2017-09-14 15:16:44

标签: android android-fragments android-recyclerview android-cardview

在我问之前,我可能会有一个奇怪的句子,因为我不会说英语。请事先确认。

我试图通过在片段中放置一个循环视图来显示卡片视图。但是,卡视图根本不可见。如果你能告诉我这是什么问题,我将不胜感激。

Home.java

package com.rudolphriding.giftfriend;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

public class Home extends Fragment
{
    private RecyclerView recyclerView;
    private LinearLayoutManager layoutManager;
    private RecyclerAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        View hView = inflater.inflate(R.layout.fragment_home, container, false);

        //recyclerview
        recyclerView = (RecyclerView)hView.findViewById(R.id.home_recyclerview);

        initData();

        layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
        recyclerView.scrollToPosition(0);



        return hView;
    }
/*
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        initData();
    }
*/

    private void initData()
    {
        List<RecyclerItem> items = new ArrayList<>();
        RecyclerItem[] item = new RecyclerItem[2];
        item[0] = new RecyclerItem(R.drawable.rudingworld_start1, "TEST#1", "SUBTEST#1");
        item[1] = new RecyclerItem(R.drawable.rudingworld_start2, "TEST#2", "SUBTEST#2");

        for(int i = 0; i < 2; i++)
        {
            items.add(item[i]);
        }
    }
}

RecyclerAdapter.java

package com.rudolphriding.giftfriend;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder>
{
    Context context;
    List<RecyclerItem> items;
    int itemLayout;

    public RecyclerAdapter(Context context, List<RecyclerItem> items, int itemLayout)
    {
        this.context = context;
        this.items = items;
        this.itemLayout = itemLayout;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_trend_cardview, parent, false);
        return new ViewHolder(v);
    }

    //@TargetApi(Build.VERSION_CODES.N)
    @Override
    public void onBindViewHolder(ViewHolder holder, int position)
    {
        final RecyclerItem item = items.get(position);

        holder.image.setImageResource(item.getBackground());
        holder.title.setText(item.getTitle());
        holder.subtitle.setText(item.getSubtitle());

        holder.cardview.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                    Toast.makeText(context,item.getTitle(),Toast.LENGTH_SHORT).show();
                    }
                }
                );
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView image;
        TextView title;
        TextView subtitle;
        CardView cardview;

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

            image = (ImageView)itemView.findViewById(R.id.cardview_image_title);
            title = (TextView)itemView.findViewById(R.id.cardview_text_title);
            subtitle = (TextView)itemView.findViewById(R.id.cardview_text_subtitle) ;
            cardview = (CardView)itemView.findViewById(R.id.cardview);
        }
    }
}

* @TargetApi

没有注释注释

RecyclerItem.java

public class RecyclerItem
{
    int background;
    String title;
    String subtitle;


    public int getBackground() { return this.background; }

    public String getTitle() { return title; }

    public String getSubtitle() { return subtitle; }



    public RecyclerItem(int background, String title, String subtitle)
    {
        this.background = background;
        this.title = title;
        this.subtitle = subtitle;
    }
}

fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/home_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


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

</RelativeLayout>

item_trend_cardview.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.rudolphriding.giftfriend.SquareCardView
        android:id="@+id/cardview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="2dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/cardview_image_title"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/rudingworld_start1"/>


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#80000000"
                android:orientation="vertical"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:padding="16dp">

                    <TextView
                        android:id="@+id/cardview_text_title"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:paddingBottom="8dp"
                        android:paddingTop="8dp"
                        android:text="Title goes here"
                        android:textColor="#FFFFFF"
                        android:textSize="24sp"
                        android:textStyle="bold"/>

                    <TextView
                        android:id="@+id/cardview_text_subtitle"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Subtitle here"
                        android:textColor="#FFFFFF"
                        android:textSize="14sp"/>

                </LinearLayout>


                <!--LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="8dp">

                    <Button
                        android:id="@+id/cardview_button_01"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingRight="8dp"
                        android:background="#00000000"
                        android:text="Action1"
                        android:textColor="#FFFFFF"/>

                    <Button
                        android:id="@+id/cardview_button_02"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="#00000000"
                        android:text="Action2"
                        android:textColor="#FFFFFF"/>


                </LinearLayout-->

            </LinearLayout>
        </RelativeLayout>




    </com.rudolphriding.giftfriend.SquareCardView>


</RelativeLayout>

*有定制的图像视图,可将方形图像放入卡片视图中。

*我还注释了按钮,因为我不知道如何让它们出现在卡片视图中。

SquareImageView.java

package com.rudolphriding.giftfriend;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;

public class SquareImageView extends android.support.v7.widget.AppCompatImageView {

    public SquareImageView(Context context) {
        super(context);
    }

    public SquareImageView(Context context, AttributeSet attrs) { super(context, attrs); }

    public SquareImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        int size = width > height ? height : width;
        setMeasuredDimension(size, size); // make it square
    }

}

花了很多天,但我还不知道还有什么不对。欢迎任何建议。

2 个答案:

答案 0 :(得分:0)

您没有创建适配器。试试这个:

    private void initData()
    {
        List<RecyclerItem> items = new ArrayList<>();
        RecyclerItem[] item = new RecyclerItem[2];
        item[0] = new RecyclerItem(R.drawable.rudingworld_start1, "TEST#1", "SUBTEST#1");
        item[1] = new RecyclerItem(R.drawable.rudingworld_start2, "TEST#2", "SUBTEST#2");

        for(int i = 0; i < 2; i++)
        {
            items.add(item[i]);
        }
        adapter= new RecyclerAdapter(getActivity(), items, 
            R.layout.item_trend_cardview);
    }

答案 1 :(得分:0)

从我所看到的,您需要初始化适配器并为其分配生成的项目。现在,您的适配器从未初始化,只是保持null

可能的解决方案:

private void initData()
    {
        // ...
        this.adapter = new RecyclerAdapter(getActivity(), items, R.layout.item_trend_cardview); 
    }

更新:将R.layout.item_trend_cardview作为实际布局ID,而不是之前的示范性0。