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
|
package org.citra.citra_emu.features.cheats.ui;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import org.citra.citra_emu.R;
import org.citra.citra_emu.features.cheats.model.Cheat;
import org.citra.citra_emu.features.cheats.model.CheatsViewModel;
public class CheatsAdapter extends RecyclerView.Adapter<CheatViewHolder> {
private final CheatsActivity mActivity;
private final CheatsViewModel mViewModel;
public CheatsAdapter(CheatsActivity activity, CheatsViewModel viewModel) {
mActivity = activity;
mViewModel = viewModel;
mViewModel.getCheatAddedEvent().observe(activity, (position) -> {
if (position != null) {
notifyItemInserted(position);
}
});
mViewModel.getCheatUpdatedEvent().observe(activity, (position) -> {
if (position != null) {
notifyItemChanged(position);
}
});
mViewModel.getCheatDeletedEvent().observe(activity, (position) -> {
if (position != null) {
notifyItemRemoved(position);
}
});
}
@NonNull
@Override
public CheatViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View cheatView = inflater.inflate(R.layout.list_item_cheat, parent, false);
addViewListeners(cheatView);
return new CheatViewHolder(cheatView);
}
@Override
public void onBindViewHolder(@NonNull CheatViewHolder holder, int position) {
holder.bind(mActivity, getItemAt(position), position);
}
@Override
public int getItemCount() {
return mViewModel.getCheats().length;
}
private void addViewListeners(View view) {
// On a portrait phone screen (or other narrow screen), only one of the two panes are shown
// at the same time. If the user is navigating using a d-pad and moves focus to an element
// in the currently hidden pane, we need to manually show that pane.
CheatsActivity.setOnFocusChangeListenerRecursively(view,
(v, hasFocus) -> mActivity.onListViewFocusChange(hasFocus));
}
private Cheat getItemAt(int position) {
return mViewModel.getCheats()[position];
}
}
|