如何在Android

时间:2018-04-08 01:14:01

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

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

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

首先,如何动态创建CardView?我的第一次尝试是在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?列表显示?我很丢失,任何指导都会有所帮助。

1 个答案:

答案 0 :(得分:0)

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