Макет, Android - WeatherApp - PullRequest
       7

Макет, Android - WeatherApp

0 голосов
/ 15 ноября 2018

Я новичок в разработке для Android, и я изо всех сил пытаюсь сделать приложение, которое мне нужно завершить как можно скорее, но если это неважно для вас, моя проблема очень проста - все, что я положил в макет наложение и повторение "из-за функциональности, но я не знаю, как этого избежать.
Смена предмета, по крайней мере, это простые вопросы для эксперта.
Результатом должно быть это EditText для поиска города и кнопка выбора для выбора города. Если я забыл что-то добавить, скажите, пожалуйста.

Screen shot

Activity_Main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.danie.weather.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/forecastRV"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

forecast_item_layout.xml

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="18dp"
    android:gravity="center_vertical"
    android:background="?attr/selectableItemBackground">



<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <EditText
        android:id="@+id/searchCity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/sendBtn"
        android:text="Select"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/forecastIV"
        tools:src="@mipmap/ic_launcher"
        android:layout_width="40dp"
        android:layout_height="40dp" />

</LinearLayout>

    <TextView
        android:id="@+id/descriptionTV"
        android:textSize="20sp"
        android:layout_marginLeft="20dp"
        tools:text="Text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />




</LinearLayout>

MainActivity

public class MainActivity extends AppCompatActivity implements ForecastAdapter.OnForecastItemClickListener {

    private static final int REQ_LOCATION_PERMISSION = 100;
    private LocationManager locationManager;

    private WeatherService service;
    private RecyclerView forecastRV;
    private ForecastAdapter forecastAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setupRecyclerView();

        service = ApiService.getService();

        getTestWeather();

        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        requestLocation();

    }

    private void requestLocation() {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(
                    this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
                    REQ_LOCATION_PERMISSION
            );
            return;
        }
        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER,
                0,
                0,
                new LocationListener() {
                    @Override
                    public void onLocationChanged(Location location) {
                        Log.d("Location received", location.getLatitude() + "");
                        getCurrentWeatherByCoords(location.getLatitude(), location.getLongitude());
                        locationManager.removeUpdates(this);
                    }

                    @Override
                    public void onStatusChanged(String provider, int status, Bundle extras) {
                        Log.d("Location status", "" + status);
                    }

                    @Override
                    public void onProviderEnabled(String provider) {

                    }

                    @Override
                    public void onProviderDisabled(String provider) {

                    }
                }
        );
    }

    private void getTestWeather() {
        service.getWeather("Zagreb", Config.WEATHER_API_KEY).enqueue(new Callback<CurrentWeather>() {
            @Override
            public void onResponse(Call<CurrentWeather> call, Response<CurrentWeather> response) {
                Log.d("Weather response", response.body().getMain().getTemp().toString());
            }

            @Override
            public void onFailure(Call<CurrentWeather> call, Throwable t) {
                t.printStackTrace();
            }
        });

        service.getForecast("zagreb", Config.WEATHER_API_KEY).enqueue(new Callback<WeatherData>() {
            @Override
            public void onResponse(Call<WeatherData> call, Response<WeatherData> response) {
                Log.d("Forecast response", response.body().getForecastItem().toString());
                forecastAdapter.setForecastItems(response.body().getForecastItem());

            }

            @Override
            public void onFailure(Call<WeatherData> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

    private void getCurrentWeatherByCoords(double lat, double lon) {
        service.getWeatherByCoords(lat, lon, Config.WEATHER_API_KEY).enqueue(new Callback<CurrentWeather>() {
            @Override
            public void onResponse(Call<CurrentWeather> call, Response<CurrentWeather> response) {
                Toast.makeText(MainActivity.this, "Current location " + response.body().getMain().getTemp() + "", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(Call<CurrentWeather> call, Throwable t) {

            }
        });
    }


    private void setupRecyclerView() {
        forecastRV = (RecyclerView) findViewById(R.id.forecastRV);
        forecastRV.setLayoutManager(new LinearLayoutManager(this));
        forecastAdapter = new ForecastAdapter();
        forecastRV.setAdapter(forecastAdapter);
        forecastAdapter.setListener(this);
    }

    @Override
    public void onForecastItemClick(ForecastItem forecastItem) {
        Toast.makeText(this, forecastItem.getMain().getHumidity() + "", Toast.LENGTH_SHORT).show();
    }
}

1 Ответ

0 голосов
/ 16 ноября 2018

Почему вы положили Button и EditText внутрь forecast_item_layout.xml, если вы не хотите, чтобы они там были?Измените Activity_Main.xml на это:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.danie.weather.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/forecastRV"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <EditText
        android:id="@+id/searchCity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/sendBtn"/>

    <Button
        android:id="@+id/sendBtn"
        android:text="Select"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"/>

</RelativeLayout>

и forecast_item_layout.xml на это:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/selectableItemBackground"
    android:gravity="center_vertical"
    android:orientation="vertical"
    android:padding="18dp">

    <ImageView
        android:id="@+id/forecastIV"
        android:layout_width="40dp"
        android:layout_height="40dp"
        tools:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/descriptionTV"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        tools:text="Text" />
</LinearLayout>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...