Skip to content

Commit

Permalink
First commit of 2016 :-) Removed adapte recreation
Browse files Browse the repository at this point in the history
  • Loading branch information
fedepaol committed Jan 1, 2016
1 parent 16a5863 commit 2a7181e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
Expand Up @@ -49,6 +49,7 @@ public class CachedActivity extends AppCompatActivity implements SwipeRefreshLay

private Observable<List<Repo>> mObservable;
private Observable<String> mProgressObservable;
private RepoAdapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -62,15 +63,16 @@ protected void onCreate(Bundle savedInstanceState) {
mSwipeLayout.setOnRefreshListener(this);
mObservable = mRepo.getDbObservable();
mProgressObservable = mRepo.getProgressObservable();
mAdapter = new RepoAdapter();
mList.setAdapter(mAdapter);
}

@Override
protected void onResume() {
super.onResume();
mObservable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()).subscribe(l -> {
RepoAdapter a = new RepoAdapter(l);
mList.setAdapter(a);
mAdapter.updateData(l);
});

fetchUpdates();
Expand Down
Expand Up @@ -28,6 +28,7 @@ public class NonCachedActivity extends AppCompatActivity {
@Bind(R.id.pending_request_progress) ProgressBar mProgress;
@Bind(R.id.main_list) RecyclerView mList;
private Observable<List<Repo>> mObservable;
private RepoAdapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -38,6 +39,8 @@ protected void onCreate(Bundle savedInstanceState) {

LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
mList.setLayoutManager(layoutManager);
mAdapter = new RepoAdapter();
mList.setAdapter(mAdapter);
}

@Override
Expand All @@ -48,9 +51,8 @@ protected void onResume() {
mObservable.delay(3, TimeUnit.SECONDS) // delayed for demonstration purpouse
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()).subscribe(l -> {
RepoAdapter a = new RepoAdapter(l);
mList.setAdapter(a);
mProgress.setVisibility(View.INVISIBLE);
mAdapter.updateData(l);
},
e -> mProgress.setVisibility(View.INVISIBLE),
()-> mProgress.setVisibility(View.INVISIBLE));
Expand Down
Expand Up @@ -26,6 +26,7 @@
import com.whiterabbit.rxrestsample.R;
import com.whiterabbit.rxrestsample.data.Repo;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
Expand All @@ -34,8 +35,13 @@
public class RepoAdapter extends RecyclerView.Adapter<RepoAdapter.RepoViewHolder> {
private List<Repo> mRepos;

public RepoAdapter(List<Repo> repos) {
public RepoAdapter() {
mRepos = new ArrayList<>(0);
}

public void updateData(List<Repo> repos) {
mRepos = repos;
notifyDataSetChanged();
}

@Override
Expand Down

0 comments on commit 2a7181e

Please sign in to comment.