This repository has been archived by the owner on Feb 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 371
/
Example6Fragment.java
85 lines (68 loc) · 3.69 KB
/
Example6Fragment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package io.github.luizgrp.sectionedrecyclerviewadapter.demo.example6;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import io.github.luizgrp.sectionedrecyclerviewadapter.SectionAdapter;
import io.github.luizgrp.sectionedrecyclerviewadapter.SectionedRecyclerViewAdapter;
import io.github.luizgrp.sectionedrecyclerviewadapter.demo.R;
import io.github.luizgrp.sectionedrecyclerviewadapter.demo.info.SectionInfoFactory;
import io.github.luizgrp.sectionedrecyclerviewadapter.demo.info.SectionItemInfoDialog;
import io.github.luizgrp.sectionedrecyclerviewadapter.demo.info.SectionItemInfoFactory;
public class Example6Fragment extends Fragment implements ExpandableMovieSection.ClickListener {
private static final String DIALOG_TAG = "SectionItemInfoDialogTag";
private SectionedRecyclerViewAdapter sectionedAdapter;
@Nullable
@Override
public View onCreateView(@NonNull final LayoutInflater inflater, @Nullable final ViewGroup container,
@Nullable final Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_ex6, container, false);
sectionedAdapter = new SectionedRecyclerViewAdapter();
final LoadMoviesUseCase loadMoviesUseCase = new LoadMoviesUseCase();
sectionedAdapter.addSection(new ExpandableMovieSection(getString(R.string.top_rated_movies_topic),
loadMoviesUseCase.execute(requireContext(), R.array.top_rated_movies), this));
sectionedAdapter.addSection(new ExpandableMovieSection(getString(R.string.most_popular_movies_topic),
loadMoviesUseCase.execute(requireContext(), R.array.most_popular_movies), this));
final RecyclerView recyclerView = view.findViewById(R.id.recyclerview);
final GridLayoutManager glm = new GridLayoutManager(getContext(), 2);
glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(final int position) {
if (sectionedAdapter.getSectionItemViewType(position) == SectionedRecyclerViewAdapter.VIEW_TYPE_HEADER) {
return 2;
}
return 1;
}
});
recyclerView.setLayoutManager(glm);
recyclerView.setAdapter(sectionedAdapter);
return view;
}
@Override
public void onHeaderRootViewClicked(@NonNull final ExpandableMovieSection section) {
final SectionAdapter sectionAdapter = sectionedAdapter.getAdapterForSection(section);
// store info of current section state before changing its state
final boolean wasExpanded = section.isExpanded();
final int previousItemsTotal = section.getContentItemsTotal();
section.setExpanded(!wasExpanded);
sectionAdapter.notifyHeaderChanged();
if (wasExpanded) {
sectionAdapter.notifyItemRangeRemoved(0, previousItemsTotal);
} else {
sectionAdapter.notifyAllItemsInserted();
}
}
@Override
public void onItemRootViewClicked(@NonNull final ExpandableMovieSection section, final int itemAdapterPosition) {
final SectionItemInfoDialog dialog = SectionItemInfoDialog.getInstance(
SectionItemInfoFactory.create(itemAdapterPosition, sectionedAdapter),
SectionInfoFactory.create(section, sectionedAdapter.getAdapterForSection(section))
);
dialog.show(getParentFragmentManager(), DIALOG_TAG);
}
}