我正在实施MVP架构,代码工作正常,如果我可以进行改进,或者在我没有正确遵循模式的任何领域,我想要你的建议。
使用RecyclerViews列出片段中的游戏是一个简单的应用程序。
FGames - 显示游戏列表的片段
public class FGames extends Fragment implements GamesView {
@BindView(R.id.rv_games)
RecyclerView rvGames;
private GamesAdapter gamesAdapter;
private GamesPresenterImpl presenter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.games_layout, container, false);
ButterKnife.bind(this, view);
presenter = new GamesPresenterImpl(this);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
presenter.initUi();
}
@Override
public void initRecyclerView(List<GameEntity> gameEntities) {
gamesAdapter = new GamesAdapter(getActivity(), gameEntities);
rvGames.setAdapter(gamesAdapter);
rvGames.setLayoutManager(new LinearLayoutManager(getActivity()));
}
}
界面 - GamesView
public interface GamesView {
/**
* Initialise the recycler view to list Games data
* @param gameEntities
*/
void initRecyclerView(List<GameEntity> gameEntities);
}
GamesPresenterImpl - 演示者实现
public class GamesPresenterImpl implements GamesPresenter {
GamesView gamesView;
public GamesPresenterImpl(GamesView gamesView) {
this.gamesView = gamesView;
}
@Override
public void initUi() {
gamesView.initRecyclerView(getGamesData());
}
public List<GameEntity> getGamesData() {
List<GameEntity> gameEntities = new ArrayList<>();
int counter = 0;
for(int i = 0; i<10; i++){
GameEntity gameEntity = new GameEntity();
counter ++;
gameEntity.setName("Test name " + counter);
gameEntity.setJackpot(100000055);
gameEntities.add(gameEntity);
}
return gameEntities;
}
}
界面 - GamesPresenter
public interface GamesPresenter {
void initUi();
}
提前致谢 [R
答案 0 :(得分:0)
您可以拥有第3层,您可以在其中添加所有数据获取(API /数据库)逻辑。 请参阅http://www.vogella.com/tutorials/AndroidArchitecture/article.html
因此,您的数据填充部分将移至第3层
for(int i = 0; i<10; i++){
GameEntity gameEntity = new GameEntity();
counter ++;
gameEntity.setName("Test name " + counter);
gameEntity.setJackpot(100000055);
gameEntities.add(gameEntity);
}