Created
March 6, 2015 14:07
-
-
Save BartoszJarocki/ae28cbd457498f58588b to your computer and use it in GitHub Desktop.
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
abstract public class ListFragment<ViewHolderType extends RecyclerView.ViewHolder, ModelType> extends BaseFragment { | |
/** | |
* Views | |
*/ | |
@InjectView(R.id.list) RecyclerView mRecyclerView; | |
@InjectView(R.id.progress_view) ProgressBar mProgressView; | |
@InjectView(R.id.empty) ViewStub mEmpty; | |
@InjectView(R.id.error_view) ErrorView mErrorView; | |
/** | |
* Injected data | |
*/ | |
@Inject Picasso mPicasso; | |
@Inject ApiManager mApiManager; | |
/** | |
* Data | |
*/ | |
Subscription mSubscription = Subscriptions.empty(); | |
ListAdapter<ViewHolderType, ModelType> mAdapter; | |
LinearLayoutManager mLinearLayoutManager; | |
public ListFragment() { | |
setRetainInstance(true); | |
} | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
View view = inflater.inflate(R.layout.fragment_recycler_view, container, false); | |
ButterKnife.inject(this, view); | |
return view; | |
} | |
@Override | |
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { | |
super.onViewCreated(view, savedInstanceState); | |
setupData(); | |
} | |
@Override | |
public void onDestroyView() { | |
mSubscription.unsubscribe(); | |
ButterKnife.reset(this); | |
super.onDestroyView(); | |
} | |
private void setupData() { | |
showProgress(); | |
mAdapter = getAdapter(); | |
mRecyclerView.setHasFixedSize(true); | |
mLinearLayoutManager = new LinearLayoutManager(getActivity()); | |
mRecyclerView.setLayoutManager(mLinearLayoutManager); | |
mRecyclerView.setAdapter(mAdapter); | |
fetchData(); | |
mAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener<ModelType>() { | |
@Override public void onItemClick(View view, ModelType dataItem, int position) { | |
onDataItemClicked(dataItem); | |
} | |
}); | |
mErrorView.setOnRetryListener(new RetryListener() { | |
@Override | |
public void onRetry() { | |
showProgress(); | |
fetchData(); | |
} | |
}); | |
} | |
protected abstract void onDataItemClicked(ModelType dataItem); | |
protected abstract ListAdapter<ViewHolderType, ModelType> getAdapter(); | |
protected abstract void fetchData(); | |
private void showProgress() { | |
hideEmptyView(); | |
hideErrorView(); | |
mRecyclerView.setVisibility(View.GONE); | |
mProgressView.setVisibility(View.VISIBLE); | |
} | |
private void hideProgress() { | |
mRecyclerView.setVisibility(View.VISIBLE); | |
mProgressView.setVisibility(View.GONE); | |
} | |
private void showEmptyView() { | |
mEmpty.setVisibility(View.VISIBLE); | |
} | |
private void hideEmptyView() { | |
if (mEmpty.getVisibility() == View.VISIBLE) { | |
mEmpty.setVisibility(View.GONE); | |
} | |
} | |
private void showErrorView() { | |
mErrorView.setVisibility(View.VISIBLE); | |
} | |
private void hideErrorView() { | |
if (mErrorView.getVisibility() == View.VISIBLE) { | |
mErrorView.setVisibility(View.GONE); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment