Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.os.Bundle;
- import android.support.annotation.Nullable;
- import android.support.v4.app.Fragment;
- import android.support.v4.content.LocalBroadcastManager;
- import android.support.v7.widget.DefaultItemAnimator;
- import android.support.v7.widget.LinearLayoutManager;
- import android.support.v7.widget.RecyclerView;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.TextView;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Locale;
- import java.util.Map;
- import controllers.listeners.ProcessedResult;
- import retrofit2.Response;
- public class Past extends Fragment implements ProcessedResult,View.OnClickListener
- {
- private int skip = 0, totalSize = -1;
- private List<MBooking> orderData;
- private StatesRecyclerViewAdapter statesRecyclerViewAdapter;
- private Context context;
- @Override
- public void onAttach(Context context) {
- super.onAttach(context);
- this.context=context;
- }
- @Nullable
- @Override
- public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
- super.onCreateView(inflater, container, savedInstanceState);
- return inflater.inflate(R.layout.recyclerview,container,false);
- }
- @Override
- public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
- super.onViewCreated(view, savedInstanceState);
- RecyclerView mRecyclerView = GeneralFunction.findViewByIdAndCast(view, R.id.common_recyclerView);
- mRecyclerView.setLayoutManager(new LinearLayoutManager(context));
- mRecyclerView.addItemDecoration(new SimpleDividerDecoration(context,R.dimen.dimen_drawable_padding,R.color.color_tranparent));
- mRecyclerView.setItemAnimator(new DefaultItemAnimator());
- View loadingView = LayoutInflater.from(context).inflate(R.layout.progress_footer, mRecyclerView,false);
- View emptyView = LayoutInflater.from(context).inflate(R.layout.view_empty, mRecyclerView,false);
- View errorView = LayoutInflater.from(context).inflate(R.layout.view_error, mRecyclerView,false);
- Button btRetry=GeneralFunction.findViewByIdAndCast(errorView,R.id.retry_button);
- DrawableState.buttonSelector(btRetry);
- btRetry.setOnClickListener(this);
- TextView tvEmptyText=GeneralFunction.findViewByIdAndCast(emptyView,R.id.empty_text);
- tvEmptyText.setText(String.format(Locale.getDefault(),getString(R.string.s_empty_error_no),getString(R.string.s_empty_error_past)));
- orderData = new ArrayList<>();
- AOrders aOrdersAdapter = new AOrders(orderData,this);
- // aOrdersAdapter.setHasStableIds(true);
- statesRecyclerViewAdapter = new StatesRecyclerViewAdapter(aOrdersAdapter,loadingView,emptyView,errorView, this);
- mRecyclerView.setAdapter(statesRecyclerViewAdapter);
- IntentFilter filter = new IntentFilter(EnumStringConstants.LocalReceivers.RATING.toString());
- filter.addAction(EnumStringConstants.LocalReceivers.PUSH_DATA.toString());
- LocalBroadcastManager.getInstance(context).registerReceiver(mMessageReceiver, filter);
- }
- @Override
- public <IResponse, IMethod> void processedResult(IResponse iResponse, IMethod iMethod) {
- switch (iMethod.toString())
- {
- case CallBackConstants.LOAD_MORE_METHOD:
- {
- if (totalSize == skip)
- {
- statesRecyclerViewAdapter.onDataReady(false);
- return;
- }
- Map<String, String> data= new HashMap<>();
- data.put(RestConstants.KEY_BOOKING_TYPE,String.valueOf(1));
- data.put(RestConstants.LIMIT,String.valueOf(7));
- data.put(RestConstants.SKIP,String.valueOf(skip));
- RetrofitAdapter.createRetrofitService().getOrders(data).enqueue(new ResponseResolver<MOrders>(this,CallBackConstants.ParsingConstants.BOOKING_UPCOMING,true,true));
- }
- break;
- case CallBackConstants.ParsingConstants.BOOKING_UPCOMING:
- if (iResponse instanceof Response) {
- MOrders mOrders = (MOrders)((Response<?>) iResponse).body();
- totalSize = mOrders.getData().getCount();
- int size = mOrders.getData().getBookings().size();
- skip += size;
- if (size > 0) {
- statesRecyclerViewAdapter.onDataReady(true);
- int prevousSize = orderData.size();
- orderData.addAll(mOrders.getData().getBookings());
- getProductAdapter().notifyItemRangeInserted(prevousSize, size);
- }else if(orderData.isEmpty())
- statesRecyclerViewAdapter.setState(StatesRecyclerViewAdapter.STATE_EMPTY);
- }
- else
- statesRecyclerViewAdapter.setState(StatesRecyclerViewAdapter.STATE_ERROR);
- break;
- break;
- }
- }
- private AOrders getProductAdapter()
- {
- return (AOrders)statesRecyclerViewAdapter.getWrappedAdapter();
- }
- @Override
- public void onClick(View v) {
- switch (v.getId())
- {
- case R.id.retry_button: {
- statesRecyclerViewAdapter.setState(StatesRecyclerViewAdapter.STATE_ENDLESS);
- statesRecyclerViewAdapter.restartAppending();
- }
- break;
- }
- }
- }
Add Comment
Please, Sign In to add comment