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:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user