-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
L10: RecyclerView with multiple view types
- Loading branch information
Showing
19 changed files
with
517 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 0 additions & 26 deletions
26
lesson10/src/main/java/com/orobator/helloandroid/lesson10/answers/AnswersActivity.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
lesson10/src/main/java/com/orobator/helloandroid/lesson10/answers/view/AnswersActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.orobator.helloandroid.lesson10.answers.view; | ||
|
||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.os.Bundle; | ||
import androidx.appcompat.app.AppCompatActivity; | ||
import androidx.databinding.DataBindingUtil; | ||
import androidx.lifecycle.Observer; | ||
import androidx.lifecycle.ViewModelProviders; | ||
import com.orobator.helloandroid.lesson10.R; | ||
import com.orobator.helloandroid.lesson10.answers.viewmodel.AnswerViewModel; | ||
import com.orobator.helloandroid.lesson10.answers.viewmodel.AnswersViewModel; | ||
import com.orobator.helloandroid.lesson10.answers.viewmodel.AnswersViewModelFactory; | ||
import com.orobator.helloandroid.lesson10.databinding.ActivityAnswersBinding; | ||
import com.orobator.helloandroid.lesson10.questions.viewmodel.QuestionViewModel; | ||
import com.orobator.helloandroid.stackoverflow.questions.Question; | ||
import dagger.android.AndroidInjection; | ||
import java.util.List; | ||
import javax.inject.Inject; | ||
|
||
public class AnswersActivity extends AppCompatActivity implements Observer<List<AnswerViewModel>> { | ||
private static final String KEY_QUESTION = "question"; | ||
|
||
public static Intent getIntent(Context context, Question question) { | ||
Intent intent = new Intent(context, AnswersActivity.class); | ||
intent.putExtra(KEY_QUESTION, question); | ||
return intent; | ||
} | ||
|
||
private AnswersRecyclerAdapter adapter; | ||
|
||
@Inject AnswersViewModelFactory viewModelFactory; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
AndroidInjection.inject(this); | ||
|
||
ActivityAnswersBinding binding = | ||
DataBindingUtil.setContentView(this, R.layout.activity_answers); | ||
|
||
AnswersViewModel viewModel = | ||
ViewModelProviders | ||
.of(this, viewModelFactory) | ||
.get(AnswersViewModel.class); | ||
|
||
Question question = getIntent().getParcelableExtra(KEY_QUESTION); | ||
viewModel.setQuestion(question); | ||
|
||
viewModel.getAnswerViewModelsLiveData().observe(this, this); | ||
|
||
adapter = new AnswersRecyclerAdapter(new QuestionViewModel(question)); | ||
binding.answersRecyclerView.setAdapter(adapter); | ||
} | ||
|
||
@Override public void onChanged(List<AnswerViewModel> answerViewModels) { | ||
adapter.updateList(answerViewModels); | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
...src/main/java/com/orobator/helloandroid/lesson10/answers/view/AnswersRecyclerAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package com.orobator.helloandroid.lesson10.answers.view; | ||
|
||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import androidx.annotation.NonNull; | ||
import androidx.databinding.DataBindingUtil; | ||
import androidx.recyclerview.widget.RecyclerView; | ||
import com.orobator.helloandroid.lesson10.R; | ||
import com.orobator.helloandroid.lesson10.answers.view.AnswersRecyclerAdapter.QuestionDetailViewHolder; | ||
import com.orobator.helloandroid.lesson10.answers.viewmodel.AnswerViewModel; | ||
import com.orobator.helloandroid.lesson10.databinding.ListItemAnswerBinding; | ||
import com.orobator.helloandroid.lesson10.databinding.ListItemQuestionDetailBinding; | ||
import com.orobator.helloandroid.lesson10.questions.viewmodel.QuestionViewModel; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class AnswersRecyclerAdapter extends RecyclerView.Adapter<QuestionDetailViewHolder> { | ||
private static final int VIEW_TYPE_QUESTION = 0; | ||
private static final int VIEW_TYPE_ANSWER = 1; | ||
|
||
private List<AnswerViewModel> answerViewModels = Collections.emptyList(); | ||
private final QuestionViewModel questionViewModel; | ||
|
||
public AnswersRecyclerAdapter(QuestionViewModel questionViewModel) { | ||
this.questionViewModel = questionViewModel; | ||
} | ||
|
||
@Override public int getItemViewType(int position) { | ||
if (position == 0) { | ||
return VIEW_TYPE_QUESTION; | ||
} else { | ||
return VIEW_TYPE_ANSWER; | ||
} | ||
} | ||
|
||
@Override public int getItemCount() { | ||
return 1 + answerViewModels.size(); | ||
} | ||
|
||
@NonNull @Override | ||
public QuestionDetailViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { | ||
if (viewType == VIEW_TYPE_QUESTION) { | ||
return onCreateQuestionViewHolder(parent); | ||
} else { | ||
return onCreateAnswerViewHolder(parent); | ||
} | ||
} | ||
|
||
private QuestionViewHolder onCreateQuestionViewHolder(ViewGroup parent) { | ||
LayoutInflater inflater = LayoutInflater.from(parent.getContext()); | ||
ListItemQuestionDetailBinding binding = | ||
DataBindingUtil.inflate(inflater, R.layout.list_item_question_detail, parent, false); | ||
return new QuestionViewHolder(binding); | ||
} | ||
|
||
private QuestionDetailViewHolder onCreateAnswerViewHolder(ViewGroup parent) { | ||
LayoutInflater inflater = LayoutInflater.from(parent.getContext()); | ||
ListItemAnswerBinding binding = | ||
DataBindingUtil.inflate(inflater, R.layout.list_item_answer, parent, false); | ||
return new AnswerViewHolder(binding); | ||
} | ||
|
||
@Override public void onBindViewHolder(@NonNull QuestionDetailViewHolder holder, int position) { | ||
if (getItemViewType(position) == VIEW_TYPE_QUESTION) { | ||
((QuestionViewHolder) holder).bind(questionViewModel); | ||
} else { | ||
((AnswerViewHolder) holder).bind(answerViewModels.get(position - 1)); | ||
} | ||
} | ||
|
||
public void updateList(List<AnswerViewModel> answerViewModels) { | ||
this.answerViewModels = answerViewModels; | ||
notifyDataSetChanged(); | ||
} | ||
|
||
static abstract class QuestionDetailViewHolder extends RecyclerView.ViewHolder { | ||
public QuestionDetailViewHolder(@NonNull View itemView) { | ||
super(itemView); | ||
} | ||
} | ||
|
||
static class QuestionViewHolder extends QuestionDetailViewHolder { | ||
private final ListItemQuestionDetailBinding binding; | ||
|
||
public QuestionViewHolder(ListItemQuestionDetailBinding binding) { | ||
super(binding.getRoot()); | ||
|
||
this.binding = binding; | ||
} | ||
|
||
public void bind(QuestionViewModel questionViewModel) { | ||
binding.setVm(questionViewModel); | ||
} | ||
} | ||
|
||
static class AnswerViewHolder extends QuestionDetailViewHolder { | ||
private final ListItemAnswerBinding binding; | ||
|
||
public AnswerViewHolder(ListItemAnswerBinding binding) { | ||
super(binding.getRoot()); | ||
|
||
this.binding = binding; | ||
} | ||
|
||
public void bind(AnswerViewModel answerViewModel) { | ||
binding.setVm(answerViewModel); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...0/src/main/java/com/orobator/helloandroid/lesson10/answers/viewmodel/AnswerViewModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.orobator.helloandroid.lesson10.answers.viewmodel; | ||
|
||
import android.text.Spanned; | ||
import androidx.core.text.HtmlCompat; | ||
import com.orobator.helloandroid.stackoverflow.answers.Answer; | ||
|
||
import static androidx.core.text.HtmlCompat.FROM_HTML_MODE_LEGACY; | ||
|
||
public class AnswerViewModel { | ||
public final Spanned body; | ||
public final String voteCount; | ||
public final String userName; | ||
|
||
public AnswerViewModel(Answer answer) { | ||
body = HtmlCompat.fromHtml(answer.body, FROM_HTML_MODE_LEGACY); | ||
voteCount = Integer.toString(answer.score); | ||
userName = answer.owner.getDisplayName(); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
.../src/main/java/com/orobator/helloandroid/lesson10/answers/viewmodel/AnswersViewModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.orobator.helloandroid.lesson10.answers.viewmodel; | ||
|
||
import androidx.lifecycle.LiveData; | ||
import androidx.lifecycle.MutableLiveData; | ||
import com.orobator.helloandroid.common.AppSchedulers; | ||
import com.orobator.helloandroid.observableviewmodel.ObservableViewModel; | ||
import com.orobator.helloandroid.stackoverflow.answers.Answer; | ||
import com.orobator.helloandroid.stackoverflow.answers.AnswersRepository; | ||
import com.orobator.helloandroid.stackoverflow.answers.AnswersResponse; | ||
import com.orobator.helloandroid.stackoverflow.questions.Question; | ||
import io.reactivex.disposables.Disposable; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class AnswersViewModel extends ObservableViewModel { | ||
private final AnswersRepository answersRepository; | ||
private final AppSchedulers schedulers; | ||
private Disposable disposable; | ||
private MutableLiveData<List<AnswerViewModel>> answerViewModelsLiveData = new MutableLiveData<>(); | ||
|
||
public AnswersViewModel( | ||
AnswersRepository answersRepository, | ||
AppSchedulers schedulers) { | ||
this.answersRepository = answersRepository; | ||
this.schedulers = schedulers; | ||
} | ||
|
||
public void setQuestion(Question question) { | ||
disposable = answersRepository | ||
.getAnswersForQuestion(question.getQuestionId()) | ||
.subscribeOn(schedulers.io) | ||
.observeOn(schedulers.main) | ||
.subscribe(this::onGetAnswersSuccess, this::onGetAnswersError); | ||
} | ||
|
||
public LiveData<List<AnswerViewModel>> getAnswerViewModelsLiveData() { | ||
return answerViewModelsLiveData; | ||
} | ||
|
||
private void onGetAnswersSuccess(AnswersResponse response) { | ||
List<AnswerViewModel> answerViewModels = new ArrayList<>(response.answers.size()); | ||
|
||
for (Answer answer : response.answers) { | ||
answerViewModels.add(new AnswerViewModel(answer)); | ||
} | ||
|
||
answerViewModelsLiveData.setValue(answerViewModels); | ||
} | ||
|
||
private void onGetAnswersError(Throwable throwable) { | ||
// Ignored for simplicity. See Lesson 9 for error handling | ||
} | ||
|
||
@Override protected void onCleared() { | ||
super.onCleared(); | ||
|
||
if (disposable != null) { | ||
disposable.dispose(); | ||
disposable = null; | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...in/java/com/orobator/helloandroid/lesson10/answers/viewmodel/AnswersViewModelFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.orobator.helloandroid.lesson10.answers.viewmodel; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.lifecycle.ViewModel; | ||
import androidx.lifecycle.ViewModelProvider; | ||
import com.orobator.helloandroid.common.AppSchedulers; | ||
import com.orobator.helloandroid.stackoverflow.answers.AnswersRepository; | ||
|
||
public class AnswersViewModelFactory implements ViewModelProvider.Factory { | ||
private final AnswersRepository answersRepository; | ||
private final AppSchedulers schedulers; | ||
|
||
public AnswersViewModelFactory( | ||
AnswersRepository answersRepository, | ||
AppSchedulers schedulers) { | ||
this.answersRepository = answersRepository; | ||
this.schedulers = schedulers; | ||
} | ||
|
||
@NonNull @Override public <T extends ViewModel> T create(@NonNull Class<T> modelClass) { | ||
return (T) new AnswersViewModel(answersRepository, schedulers); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
lesson10/src/main/java/com/orobator/helloandroid/lesson10/di/ActivityBindingModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.