如何在Android中动态创建CardViews

3
我一直在努力将我的应用想法付诸实践。这个想法是一个天气应用程序,用户可以选择他们想要看到的内容,并用卡片(CardViews)表示。例如,如果他们只想看当前天气和未来3天的预报,他们可以启用它,然后在MainActivity中以Google Now的方式只看到这两张卡片。
我遇到了一些问题,需要帮助。
首先,如何动态创建CardViews?我的第一次尝试完全在XML中定义视图,但这并不是很好,因为总是隐藏/显示它们似乎不是很直观。所以我决定将CardView类扩展到像CurrentWeatherCardView这样的东西,并完全通过编程创建卡片。
这是我从中创建的类。它是WeatherCardView类的子类,该类只是在这一点上扩展了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');
}



}

虽然这种方法有些混乱,但是否有更好的方式呢?通过实例化类,我可以创建这个CardView,但我在确定应该将这些视图放在哪种类型的视图中时遇到了问题。RecyclerView似乎并不是最合适的选择,因为据我所知,它是根据数据集创建卡片的。在我的情况下,CurrentWeatherCardView将始终相同。

这就引出了我的下一个问题。我该用什么方法来创建一个可滚动、可侧向滑动的卡片集合? RecyclerView?ListView?我很迷茫,希望得到任何指导。

1个回答

2

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);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接