使用CardView和GridView实现RecyclerView

时间:2017-02-28 20:23:12

标签: android android-recyclerview android-gridview android-cardview

我试图实现一个包含一个包含GridView的CardView的RecyclingView。没有任何错误没有显示任何东西。当我尝试运行应用程序时只是一个白色的屏幕。

public class MainActivity extends AppCompatActivity {
    TextView mWeatherTemp, mWeatherDescript;
    RecyclerView.Adapter adapter;
    Context context;
    GridView gridView;
    String[] mTime = {
            "1:00",
            "2:00",
            "3:00",
            "4:00",
            "5:00",
            "6:00",
            "7:00",
            "8:00",
            "9:00",
            "10:00",
            "11:00",

    };

    String[] mDegreeTemp = {
            "8°",
            "12°",
            "43°",
            "100°",
            "32°",
            "12°",
            "58°",
            "39°",
            "29°",
            "86°",
            "70°",

    };

    int[] imageId = {
            R.mipmap.ic_launcher
    };

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = getApplicationContext();
        Toolbar mToolbar = (Toolbar) findViewById(R.id.mMainToolbar);
        setSupportActionBar(mToolbar);
        mToolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.weather_cool));

        mWeatherTemp = (TextView) findViewById(R.id.tvWeatherTemp);
        mWeatherDescript = (TextView) findViewById(R.id.tvWeatherText);

        Typeface robotDisplay3 = Typeface.createFromAsset(getApplicationContext().getAssets(),
                "font/Roboto-Regular.ttf");
        mWeatherTemp.setTypeface(robotDisplay3);

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.mHourlyRV);
        HourlyGridAdapter adapter = new HourlyGridAdapter(getApplicationContext(), mTime, mDegreeTemp, imageId);
        gridView = (GridView) findViewById(R.id.hourlyGridView);
        gridView.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.settings, menu);
        return super.onCreateOptionsMenu(menu);
    }
}


public class HourlyGridAdapter extends BaseAdapter {
    private Context mContext;
    private String[] mTime;
    private String[] mDegreeTemp;
    private final int[] imageId;

    public HourlyGridAdapter(Context context, String[] mTime, String[] mDegreeTemp, int[] imageId) {
        mContext = context;
        this.imageId = imageId;
        this.mDegreeTemp = mDegreeTemp;
        this.mTime = mTime;
    }

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

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.hourly_single, null, false);
            holder = new ViewHolder();
            holder.timeText = (TextView) convertView.findViewById(R.id.timePlaceHolder);
            holder.degreeText = (TextView) convertView.findViewById(R.id.degreePlaceHolder);
            holder.weatherImage = (ImageView) convertView.findViewById(R.id.weatherIconHolder);

            holder.timeText.setText(mTime[position]);
            holder.degreeText.setText(mDegreeTemp[position]);
            holder.weatherImage.setImageResource(imageId[position]);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        return convertView;
    }

    static class ViewHolder {
        TextView timeText, degreeText;
        ImageView weatherImage;
    }
}

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        card_view:cardCornerRadius="6dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tvDate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TODAY" />

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@color/forecast_card_divider" />

            <GridView
                android:id="@+id/hourlyGridView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:horizontalSpacing="12dp"
                android:numColumns="4"
                android:verticalSpacing="12dp" />

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

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

    <TextView
        android:id="@+id/timePlaceHolder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/timePlaceHolderText"
        android:layout_gravity="center"/>

    <ImageView
        android:id="@+id/weatherIconHolder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
        android:layout_gravity="center"/>

    <TextView
        android:id="@+id/degreePlaceHolder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="12°"
        android:layout_gravity="center"/>

</LinearLayout>

我甚至不确定是否可以在回收视图内部使用CardView内部的GridView。任何试图创建这个的帮助都会被创造出来。

PS。我知道我没有调用recyclerView声明它不起作用并且确实使应用程序崩溃并且GridView为null。不知道该怎么做。不确定是否需要为recyclerview创建适配器,因为所有textViews和ImageView都在网格中而不是cardview。

1 个答案:

答案 0 :(得分:0)

您正在为gridview初始化和设置适配器,该适配器位于recyclerview的每个项目内,而不是在回收器适配器中,而是在活动中。考虑将所有与gridview相关的代码删除到recyclerview的适配器。