【问题标题】:How to Create CardViews Dynamically in Android如何在 Android 中动态创建 CardViews
【发布时间】:2018-09-17 16:52:31
【问题描述】:

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

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

首先,如何动态创建 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?列表显示?我很迷茫,任何指导都会有所帮助。

【问题讨论】:

    标签: java android android-recyclerview android-cardview cardview


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2015-11-08
      • 2019-11-25
      • 1970-01-01
      • 2021-01-14
      • 1970-01-01
      • 1970-01-01
      • 2015-04-06
      • 2016-02-26
      • 2017-07-05
      相关资源
      最近更新 更多