Adds list of monsters to Library screen.
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
package com.majinnaibu.monstercards.ui;
|
||||
|
||||
import android.content.Context;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.majinnaibu.monstercards.R;
|
||||
import com.majinnaibu.monstercards.models.Monster;
|
||||
import com.majinnaibu.monstercards.ui.library.LibraryFragment;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.rxjava3.core.Flowable;
|
||||
import io.reactivex.rxjava3.schedulers.Schedulers;
|
||||
|
||||
public class MonsterListRecyclerViewAdapter extends RecyclerView.Adapter<MonsterListRecyclerViewAdapter.ViewHolder> {
|
||||
public interface ItemCallback {
|
||||
void onItem(Monster monster);
|
||||
}
|
||||
|
||||
// TODO: Replace SimpleItemRecyclerViewAdapter with something better like MonsterListRecyclerViewAdapter that can be reused in search
|
||||
|
||||
private final LibraryFragment mParentActivity;
|
||||
private List<Monster> mValues;
|
||||
private final boolean mTwoPane;
|
||||
private final Context mContext;
|
||||
private final ItemCallback mOnDelete;
|
||||
private final View.OnClickListener mOnClickListener = new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
}
|
||||
};
|
||||
|
||||
public MonsterListRecyclerViewAdapter(LibraryFragment parent,
|
||||
Flowable<List<Monster>> itemsObservable,
|
||||
ItemCallback onDelete,
|
||||
boolean twoPane) {
|
||||
mValues = new ArrayList<>();
|
||||
mParentActivity = parent;
|
||||
mTwoPane = twoPane;
|
||||
mContext = parent.getContext();
|
||||
mOnDelete = onDelete;
|
||||
|
||||
itemsObservable
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(monsters -> {
|
||||
mValues = monsters;
|
||||
notifyDataSetChanged();
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||
View view = LayoutInflater.from(parent.getContext())
|
||||
.inflate(R.layout.monster_list_content, parent, false);
|
||||
return new ViewHolder(view);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(final ViewHolder holder, int position) {
|
||||
holder.mIdView.setText(mValues.get(position).id.toString().substring(0, 6));
|
||||
holder.mContentView.setText(mValues.get(position).name);
|
||||
|
||||
holder.itemView.setTag(mValues.get(position));
|
||||
holder.itemView.setOnClickListener(mOnClickListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return mValues.size();
|
||||
}
|
||||
|
||||
public Context getContext() {
|
||||
return mContext;
|
||||
}
|
||||
|
||||
class ViewHolder extends RecyclerView.ViewHolder {
|
||||
final TextView mIdView;
|
||||
final TextView mContentView;
|
||||
|
||||
ViewHolder(View view) {
|
||||
super(view);
|
||||
mIdView = view.findViewById(R.id.id_text);
|
||||
mContentView = view.findViewById(R.id.content);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,15 +4,15 @@ import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.lifecycle.Observer;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.majinnaibu.monstercards.R;
|
||||
import com.majinnaibu.monstercards.data.MonsterRepository;
|
||||
import com.majinnaibu.monstercards.ui.MCFragment;
|
||||
import com.majinnaibu.monstercards.ui.MonsterListRecyclerViewAdapter;
|
||||
|
||||
public class LibraryFragment extends MCFragment {
|
||||
|
||||
@@ -20,15 +20,26 @@ public class LibraryFragment extends MCFragment {
|
||||
|
||||
public View onCreateView(@NonNull LayoutInflater inflater,
|
||||
ViewGroup container, Bundle savedInstanceState) {
|
||||
libraryViewModel = new ViewModelProvider(this).get(LibraryViewModel.class);
|
||||
View root = inflater.inflate(R.layout.fragment_library, container, false);
|
||||
final TextView textView = root.findViewById(R.id.text_library);
|
||||
libraryViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
|
||||
@Override
|
||||
public void onChanged(@Nullable String s) {
|
||||
textView.setText(s);
|
||||
}
|
||||
});
|
||||
|
||||
RecyclerView recyclerView = root.findViewById(R.id.monster_list);
|
||||
assert recyclerView != null;
|
||||
setupRecyclerView(recyclerView);
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
|
||||
MonsterRepository repository = this.getMonsterRepository();
|
||||
boolean mTwoPane = false;
|
||||
MonsterListRecyclerViewAdapter adapter = new MonsterListRecyclerViewAdapter(
|
||||
this,
|
||||
repository.getMonsters(),
|
||||
(monster) -> {
|
||||
},
|
||||
mTwoPane);
|
||||
recyclerView.setAdapter(adapter);
|
||||
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,17 +6,25 @@
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".ui.library.LibraryFragment">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/text_library"
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/monster_list"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_marginLeft="16dp"
|
||||
android:layout_marginRight="16dp"
|
||||
app:layoutManager="LinearLayoutManager"
|
||||
tools:context=".MonsterListFragment"
|
||||
tools:listitem="@layout/monster_list_content" />
|
||||
|
||||
<com.google.android.material.floatingactionbutton.FloatingActionButton
|
||||
android:id="@+id/fab"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="8dp"
|
||||
android:layout_marginTop="8dp"
|
||||
android:layout_marginEnd="8dp"
|
||||
android:textAlignment="center"
|
||||
android:textSize="20sp"
|
||||
android:layout_gravity="bottom|end"
|
||||
android:layout_margin="@dimen/fab_margin"
|
||||
app:srcCompat="@android:drawable/ic_input_add"
|
||||
app:tint="@android:color/white"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
app:layout_constraintEnd_toEndOf="parent" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
@@ -49,6 +49,10 @@
|
||||
android:id="@+id/navigation_monster"
|
||||
android:name="com.majinnaibu.monstercards.ui.monster.MonsterFragment"
|
||||
android:label="Monster"
|
||||
tools:layout="@layout/fragment_monster" />
|
||||
tools:layout="@layout/fragment_monster">
|
||||
<argument
|
||||
android:name="monster_id"
|
||||
app:argType="string" />
|
||||
</fragment>
|
||||
|
||||
</navigation>
|
||||
Reference in New Issue
Block a user