Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ MVP ] Fix some DI issues: different instances; presenter in adapter…
… is null
- Loading branch information
Showing
13 changed files
with
184 additions
and
150 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
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
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
9 changes: 0 additions & 9 deletions
9
app/src/main/java/me/li2/android/architecture/ui/list/ArticleSelectListener.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
55 changes: 55 additions & 0 deletions
55
app/src/main/java/me/li2/android/architecture/ui/list/ArticlesAdapter.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,55 @@ | ||
package me.li2.android.architecture.ui.list; | ||
|
||
import android.content.Context; | ||
import android.support.v7.widget.RecyclerView; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
|
||
import javax.inject.Inject; | ||
|
||
import me.li2.android.architecture.R; | ||
|
||
/** | ||
* | ||
To use MVP for RecyclerView, | ||
Normally we create a Collection (let it be a List) field within the adapter, holding all the data that it needs to display. | ||
This sucks, because in MVP we typically manager data in presenter, | ||
this makes the list to be referenced (or worse: copied) in two different places, | ||
which doubles our effort to keep those two in sync when making changes. | ||
Instead, we should use presenter ! | ||
https://android.jlelse.eu/recyclerview-in-mvp-passive-views-approach-8dd74633158 | ||
* @author Weiyi Li on 6/5/18 | https://github.com/li2 | ||
*/ | ||
|
||
public class ArticlesAdapter extends RecyclerView.Adapter<ArticleViewHolder> { | ||
|
||
private Context mContext; | ||
|
||
@Inject | ||
ArticlesContract.Presenter mPresenter; | ||
|
||
@Inject | ||
public ArticlesAdapter(Context context) { | ||
mContext = context; | ||
} | ||
|
||
@Override | ||
public int getItemCount() { | ||
return mPresenter.getArticlesCount(); | ||
} | ||
|
||
@Override | ||
public ArticleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | ||
View view = LayoutInflater.from(mContext).inflate(R.layout.article_list_view_holder, parent, false); | ||
return new ArticleViewHolder(view, mPresenter); | ||
} | ||
|
||
@Override | ||
public void onBindViewHolder(ArticleViewHolder holder, int position) { | ||
holder.bindArticle(mPresenter.getArticle(position)); | ||
} | ||
} |
Oops, something went wrong.