Adds Monster fragment to view a monster's card.

Adds query box and search button to search fragment.
Makes the search button show a monster card.
This commit is contained in:
2020-09-01 00:07:18 -07:00
parent 9a9ad05436
commit 724989b8b0
8 changed files with 119 additions and 16 deletions

View File

@@ -0,0 +1,35 @@
package com.majinnaibu.monstercards.ui.monster;
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.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import com.majinnaibu.monstercards.R;
public class MonsterFragment extends Fragment {
private MonsterViewModel monsterViewModel;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
monsterViewModel =
ViewModelProviders.of(this).get(MonsterViewModel.class);
View root = inflater.inflate(R.layout.fragment_monster, container, false);
final TextView textView = root.findViewById(R.id.text_monster);
monsterViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
textView.setText(s);
}
});
return root;
}
}

View File

@@ -0,0 +1,19 @@
package com.majinnaibu.monstercards.ui.monster;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
public class MonsterViewModel extends ViewModel {
private MutableLiveData<String> mText;
public MonsterViewModel() {
mText = new MutableLiveData<>();
mText.setValue("This is monster fragment");
}
public LiveData<String> getText() {
return mText;
}
}

View File

@@ -4,6 +4,7 @@ import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
@@ -11,6 +12,8 @@ import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.navigation.NavDirections;
import androidx.navigation.Navigation;
import com.majinnaibu.monstercards.R;
@@ -23,13 +26,22 @@ public class SearchFragment extends Fragment {
searchViewModel =
ViewModelProviders.of(this).get(SearchViewModel.class);
View root = inflater.inflate(R.layout.fragment_search, container, false);
final TextView textView = root.findViewById(R.id.text_search);
searchViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
final TextView textView = root.findViewById(R.id.search_query);
searchViewModel.getSearchQuery().observe(getViewLifecycleOwner(), new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
textView.setText(s);
}
});
final Button btnSearch = root.findViewById(R.id.button_search);
btnSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
NavDirections action = SearchFragmentDirections.actionNavigationSearchToNavigationMonster();
Navigation.findNavController(view).navigate(action);
}
});
return root;
}
}

View File

@@ -6,14 +6,14 @@ import androidx.lifecycle.ViewModel;
public class SearchViewModel extends ViewModel {
private MutableLiveData<String> mText;
private MutableLiveData<String> mSearchQuery;
public SearchViewModel() {
mText = new MutableLiveData<>();
mText.setValue("This is search fragment");
mSearchQuery = new MutableLiveData<>();
mSearchQuery.setValue("");
}
public LiveData<String> getText() {
return mText;
public LiveData<String> getSearchQuery() {
return mSearchQuery;
}
}

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.monster.MonsterFragment">
<TextView
android:id="@+id/text_monster"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@@ -4,20 +4,30 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.search.SearchFragment">
tools:context=".ui.search.SearchFragment"
tools:targetApi="o">
<TextView
android:id="@+id/text_search"
android:layout_width="match_parent"
<Button
android:id="@+id/button_search"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginEnd="8dp"
android:text="@string/action_search"
app:layout_constraintBaseline_toBaselineOf="@+id/search_query"
app:layout_constraintEnd_toEndOf="parent" />
<EditText
android:id="@+id/search_query"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:importantForAutofill="no"
android:ems="10"
android:inputType="textPersonName"
android:hint="@string/label_search_query"
app:layout_constraintEnd_toStartOf="@+id/button_search"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@@ -4,4 +4,6 @@
<string name="title_search">Search</string>
<string name="title_collections">Collections</string>
<string name="title_library">Library</string>
<string name="action_search">Search</string>
<string name="label_search_query">Query</string>
</resources>