N
N
Neonoviiwolf2019-09-22 14:26:53
Android
Neonoviiwolf, 2019-09-22 14:26:53

Why RecyclerView works fine in API 21 and doesn't work in API 28-29 (didn't check others)?

Good.
Strange thing, in the API 21 emulator RecyclerView behaves correctly, but in others it does not display the list on the screen, although notifyDataSetChanged is called in
the adapter code

package com.example.weather.app.adapter.recyclerView;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.weather.R;
import com.example.weather.eventBus.ClickItemRecyclerView;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

public class AdapterFindCity extends RecyclerView.Adapter<AdapterFindCity.ViewHolder> {

    public static class ViewHolder extends RecyclerView.ViewHolder {

        private TextView nameEnCity, country;
        private LinearLayout layout;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            layout = itemView.findViewById(R.id.find_city_layout);
            nameEnCity = itemView.findViewById(R.id.tv_name_en_city_adapter);
            country = itemView.findViewById(R.id.tv_country_adapter);
        }
    }

    private List<ItemAdapterFindCity> list;

    public AdapterFindCity(List<ItemAdapterFindCity> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.find_city_adapter, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ItemAdapterFindCity item = list.get(position);
        holder.country.setText(item.getCountry().trim());
        if (item.getNameCityEN() != null)
            holder.nameEnCity.setText(item.getNameCityEN().trim());
        holder.layout.setOnClickListener(view -> {
            EventBus.getDefault().post(new ClickItemRecyclerView(list.get(position).getIdWeather()));
            ((Activity) holder.itemView.getContext()).finish();
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
}

here is avity
package com.example.weather.app.activities.findCity.view;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.widget.SearchView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.arellomobile.mvp.MvpAppCompatActivity;
import com.arellomobile.mvp.presenter.InjectPresenter;
import com.example.weather.R;
import com.example.weather.app.activities.findCity.presenter.PresenterFindCity;
import com.example.weather.app.adapter.recyclerView.AdapterFindCity;
import com.example.weather.app.adapter.recyclerView.ItemAdapterFindCity;
import com.google.android.flexbox.FlexboxLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FindCity extends MvpAppCompatActivity implements ViewFindCity {

    @InjectPresenter
    PresenterFindCity presenter;

    @BindView(R.id.flex_box_container) FlexboxLayout flexboxContainer;

    @BindView(R.id.recycler_list_city) RecyclerView recyclerViewCity;

    @BindView(R.id.sw_city) SearchView searchView;
    @BindView(R.id.tv_popular_cities) TextView tvPopularCities;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.find_city);
        ButterKnife.bind(this);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerViewCity.setLayoutManager(layoutManager);
        recyclerViewCity.setHasFixedSize(false);

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
//                presenter.setTextSearch(query);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                presenter.setTextSearch(newText);
                return false;
            }
        });
    }

    private AdapterFindCity adapterFindCity;
/**сюда приходит ссылка на array*/
    @Override
    public void updateRecyclerView(List<ItemAdapterFindCity> cityList) {
        if (recyclerViewCity.getVisibility() == View.GONE) {
            flexboxContainer.setVisibility(View.GONE);
            tvPopularCities.setVisibility(View.GONE);
            recyclerViewCity.setVisibility(View.VISIBLE);
            adapterFindCity = new AdapterFindCity(cityList);
            recyclerViewCity.setAdapter(adapterFindCity);
        }
        adapterFindCity.notifyDataSetChanged();
    }

//    @OnClick(R.id.btn_search_city)
//    public void SearchCity() {
////        presenter.
//    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.bind(this).unbind();
        presenter.disposable();
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Neonoviiwolf, 2019-09-22
@Neonoviiwolf

Strange garbage, in general, if you write the markup like this

android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/flex_box_container"
        app:layout_constraintVertical_bias="0.0">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recycler_list_city"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="8dp"
                android:paddingTop="16dp"
                android:visibility="gone" />
        </LinearLayout>
    </androidx.core.widget.NestedScrollView>

then it works

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question