如何在 Android 中动态创建 CardView

How to Create CardViews Dynamically in Android

一段时间以来,我一直在努力实现我的应用创意。这个想法是一个天气应用程序,用户可以在其中选择他们想要查看的内容,并以卡片(CardViews)表示。例如,如果他们只想查看当前天气和 3 天预报,他们可以启用它,并且他们将在 MainActivity 中以 Google 现在的时尚方式只看到这两张卡片。

我 运行 遇到了一些问题,我需要一些帮助。

首先,如何动态创建 CardView? 我的第一次尝试是在 XML 中完全定义了视图,但效果不是很好,因为它hide/show 他们似乎一直都不是很直观。所以我决定将 CardView class 扩展为 CurrentWeatherCardView 之类的东西,并完全以编程方式创建卡片。

这是我从中创建的 class。它是 WeatherCardView class 的子项,它只是扩展了 CardView,此时并没有做很多其他事情。

package com.photonfighterlabs.particleweather.weathercardviews;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.photonfighterlabs.particleweather.MainActivity;
import com.photonfighterlabs.particleweather.weatherobjects.CurrentWeather;
import com.photonfighterlabs.particleweather.weatherobjects.Weather;

import java.io.File;

import static android.widget.LinearLayout.HORIZONTAL;
import static android.widget.LinearLayout.VERTICAL;


public class CurrentWeatherCardView extends WeatherCardView {

private ViewGroup viewGroup;

private LinearLayout linearLayout_600, linearLayout_604, linearLayout_434;
private TextView cw_title, temp_text_view;
private ImageView icon_image_view;
private int is_day;
private CurrentWeather currentWeather;

public CurrentWeatherCardView(CurrentWeather currentWeather, Context context, Activity activity, ViewGroup viewGroup) {
    super(context, activity);
    this.currentWeather = currentWeather;
    this.currentWeather.initialize(context, activity, MainActivity.API_KEY);

    this.context = currentWeather.getContext();
    this.viewGroup = viewGroup;




    this.currentWeather.doOnResponse(() -> {
        setupLayout();
        setCw_title(currentWeather.getName(), currentWeather.getText());
        Weather.setIconDrawable(currentWeather.getIcon(), icon_image_view, currentWeather.getIs_day());
        setTemperature(currentWeather.getTemp_f());

    });

}

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

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

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





@Override
protected void setupLayout() {
    LayoutParams cardParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, pixels(200));
    cardParams.setMargins(
            pixels(15),
            pixels(15),
            pixels(15),
            0
    );
    super.setLayoutParams(cardParams);
    super.setRadius(pixels(4));
    super.setUseCompatPadding(true);



    linearLayout_604 = new LinearLayout(context);
    linearLayout_604.setBaselineAligned(false);
    linearLayout_604.setOrientation(VERTICAL);
    LayoutParams layout_170 = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    linearLayout_604.setLayoutParams(layout_170);
    this.addView(linearLayout_604);

    linearLayout_600 = new LinearLayout(context);
    linearLayout_600.setOrientation(LinearLayout.HORIZONTAL);
    LayoutParams layout_676 = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, pixels(50));
    linearLayout_600.setLayoutParams(layout_676);
    linearLayout_604.addView(linearLayout_600);

    cw_title = new TextView(context);
    cw_title.setTypeface(Typeface.SANS_SERIF);
    cw_title.setTextSize(pixels(5));
    LayoutParams layout_914 = new LayoutParams(pixels(250), pixels(25));
    layout_914.setMarginStart(pixels(10));
    layout_914.topMargin = pixels(10);
    cw_title.setLayoutParams(layout_914);
    linearLayout_600.addView(cw_title);


    icon_image_view = new ImageView(context);
    LayoutParams layout_501 = new LayoutParams(pixels(75), pixels(40));
    layout_501.setMarginStart(pixels(45));
    layout_501.topMargin = pixels(10);
    icon_image_view.setLayoutParams(layout_501);
    linearLayout_600.addView(icon_image_view);

    linearLayout_434 = new LinearLayout(context);
    linearLayout_434.setOrientation(HORIZONTAL);
    LayoutParams layout_204 = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    linearLayout_434.setLayoutParams(layout_204);

    temp_text_view = new TextView(context);
    temp_text_view.setTextAlignment(TEXT_ALIGNMENT_TEXT_START);
    temp_text_view.setTextSize(pixels(8));
    LayoutParams layout_725 = new LayoutParams(pixels(100), pixels(100));
    layout_725.setMarginStart(pixels(25));
    layout_725.topMargin = pixels(10);
    temp_text_view.setLayoutParams(layout_725);
    linearLayout_434.addView(temp_text_view);
    linearLayout_604.addView(linearLayout_434);

    viewGroup.addView(this);
}


private void setCw_title(String title, String condition) {
    cw_title.setText(title + " - " + condition);
}

private void setTemperature(double temp) {
    temp_text_view.setText(String.valueOf((int) temp) + '\u00b0');
}



}

很乱,但是有没有更好的办法呢?这样做允许我通过实例化 class 来创建这个 CardView,但是我 运行 正在考虑我应该把它们放在什么样的视图中。 RecyclerView这似乎不是正确的选择,因为据我所知,它是从数据集中创建卡片。就我而言,CurrentWeatherCardView 将始终相同。

这引出了我的下一个问题。 我可以通过什么方式获得一组可滚动、可侧滑的卡片?RecyclerView?列表显示?我很迷茫,任何指导都会有所帮助。

CardView 是从 FrameLayout 扩展而来的,因此您可以只使用 xml 来设计您的卡片外观,并在此 CurrentWeatherCardView 中使用 findViewById。例如:

    public CurrentWeatherCardView(Context context) {
    super(context);
    init();
}

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

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

private void init() { 
    LayoutInflater.from(this).inflate(R.layout.xxx, this, true);
    xxx = findViewById(R.id.xxxxx);
    ...
    xxx.setText(xxxx)

    // if you want to change visibility, just call:
    setVisibility(View.GONE/View.VISIBILE);
}