Как установить видимость RecyclerView в другом элементе recyclerView - PullRequest
0 голосов
/ 16 июня 2020

У меня 2 recyclerViews. Первый (Days recyclerView) - это основной recyclerView в mainActivity. Второй (Hours recyclerView) - это recyclerView, который находится в itemView первого recyclerView.

Я хочу, чтобы при нажатии на элемент из первого recyclerView видимость секунд изменится на VISIBLE.

На данный момент, когда я нажимаю на элемент, поэтому recyclerView появляется под первым элементом первого представления recycler.

Вот мой основной код активности:

private void fillDaysRecyclerView() {
        daysRecyclerView = activityMainBinding.daysRecyclerView;
        adapter = new DailyRvAdapter(this, (ArrayList<Daily>) weatherResponseViewModel.getDailyWeatherData().getValue());
        daysRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        daysRecyclerView.setItemAnimator(new DefaultItemAnimator());
        daysRecyclerView.addItemDecoration(new DividerItemDecoration(daysRecyclerView.getContext(), DividerItemDecoration.VERTICAL));
        daysRecyclerView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        adapter.setOnDayItemClick(new DailyRvAdapter.OnDayItemClickListener() {
            @Override
            public void onDayItemClick(int position, Daily daily) {

                fillHoursRecyclerView(daily);

            }
        });
    }

    private void fillHoursRecyclerView(Daily daily) {
        selectedDay = new TimeUtils();
        hourToShow = new TimeUtils();
        selectedDay.getReadableDay(daily.getDt());
        ArrayList hourlies = new ArrayList<>();

        for (int i = 0; i < weatherResponseViewModel.getHourlyWeatherData().getValue().size(); i++) {
            if (hourToShow.getReadableDay(weatherResponseViewModel.getHourlyWeatherData().getValue().get(i).getDt()).equals(selectedDay.getReadableDay(daily.getDt()))) {
                hourlies.add(weatherResponseViewModel.getHourlyWeatherData().getValue().get(i));
            }
        }

        hoursRvAdapter = new HoursRvAdapter(getApplicationContext(), hourlies);
        hoursRecyclerView = findViewById(R.id.hoursRecyclerView);
        hoursRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        hoursRecyclerView.setItemAnimator(new DefaultItemAnimator());
        hoursRecyclerView.setAdapter(hoursRvAdapter);
        hoursRecyclerView.setVisibility(View.VISIBLE);
        hoursRvAdapter.notifyDataSetChanged();
    }

И адаптер DaysRV

@Override
    public void onBindViewHolder(@NonNull DayViewHolder holder, int position) {
        Daily daily = dailies.get(position);
        holder.dayItemViewBinding.setDaily(daily);
        holder.dayItemViewBinding.getRoot().setTag(position);



        holder.dayItemViewBinding.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = (int) v.getTag();
                if (onDayItemClick != null){
                    onDayItemClick.onDayItemClick(position, daily);
                }
            }
        });
    }


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

    public class DayViewHolder extends RecyclerView.ViewHolder {

        private DayItemViewBinding dayItemViewBinding;

        public DayViewHolder(@NonNull DayItemViewBinding dayItemViewBinding) {
            super(dayItemViewBinding.getRoot());
            this.dayItemViewBinding = dayItemViewBinding;

        }

    }

Адаптер часов rv

@Override
    public void onBindViewHolder(@NonNull HoursViewHolder holder, int position) {
        Hourly hourly = hourlies.get(position);
        holder.hourItemViewBinding.setHourly(hourly);
    }

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

    public class HoursViewHolder extends RecyclerView.ViewHolder {

        private HourItemViewBinding hourItemViewBinding;

        public HoursViewHolder(@NonNull HourItemViewBinding hourItemViewBinding) {
            super(hourItemViewBinding.getRoot());
            this.hourItemViewBinding = hourItemViewBinding;
        }
    }

Day item_view

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <variable
            name="daily"
            type="com.enjoyapps.weatherapp.pojo.Daily" />

    </data>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:id="@+id/rootView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="end"
                android:text="@{daily.readableDate}"
                android:textColor="@color/black"
                android:textSize="16sp"
                android:textStyle="bold"
                tools:text="שני" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="end"
                android:text="@{daily.temp.maxTemp}"
                android:textColor="@color/black"
                android:textSize="16sp"
                android:textStyle="bold"
                tools:text="29c" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="end"
                android:text="@{daily.temp.minTemp}"
                android:textColor="@color/black"
                android:textSize="16sp"
                android:textStyle="bold"
                tools:text="26c" />

        </LinearLayout>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/hoursRecyclerView"
            android:visibility="gone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/rootView" />

    </RelativeLayout>

</layout>

часы item_view

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>

        <variable
            name="hourly"
            type="com.enjoyapps.weatherapp.pojo.Hourly" />

    </data>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="@{hourly.readableTime}"
            android:textColor="@color/black"
            android:textSize="16sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="@{hourly.normalTemp}"
            android:textColor="@color/black"
            android:textSize="16sp"
            android:textStyle="bold" />

    </LinearLayout>
</layout>
...