Advertisement
Guest User

Fragment

a guest
May 4th, 2015
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.64 KB | None | 0 0
  1. package ru.myapp.test.brands;
  2.  
  3. import android.os.Bundle;
  4. import android.support.v4.app.Fragment;
  5. import android.support.v7.widget.RecyclerView;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9.  
  10. import com.google.gson.Gson;
  11. import com.tonicartos.superslim.LayoutManager;
  12.  
  13. import java.io.Reader;
  14.  
  15. import customcomponents.progressdialog.ProgressWheel;
  16. import ru.myapp.test.PostRequest;
  17. import ru.myapp.test.R;
  18.  
  19. /**
  20.  * Created by Lucifer on 29.04.2015.
  21.  */
  22. public class BrandsFragment extends Fragment implements PostRequest.OnReplyServerListener,
  23.         BrandsAdapter.OnLikeBrandListner {
  24.  
  25.     public static final String ARG_TYPE_BRAND = "type_brand";
  26.     private int mHeaderDisplay;
  27.     private RecyclerBrandsAdapter mAdapter;
  28.     BrandModel brandsItems;
  29.     PostRequest postRequest;
  30.     private boolean mAreMarginsFixed;
  31.     private RecyclerView mRecyclerView;
  32.  
  33.     public BrandsFragment() {
  34.     }
  35.  
  36.     int page = 1;
  37.     ProgressWheel wheel;
  38.  
  39.     public static Fragment newInstance(int brand) {
  40.         Fragment fragment = new BrandsFragment();
  41.         Bundle args = new Bundle();
  42.         args.putInt(BrandsFragment.ARG_TYPE_BRAND, brand);
  43.         fragment.setArguments(args);
  44.         return fragment;
  45.     }
  46.  
  47.     @Override
  48.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  49.         return inflater.inflate(R.layout.brands_fragment, container, false);
  50.     }
  51.  
  52.     @Override
  53.     public void onViewCreated(View view, Bundle savedInstanceState) {
  54.  
  55.  
  56.         mHeaderDisplay = 18;
  57.         mAreMarginsFixed = true;
  58.  
  59.         mRecyclerView = (RecyclerView) view.findViewById(R.id.brands_list);
  60.         mRecyclerView.setLayoutManager(new LayoutManager(getActivity()));
  61.  
  62.  
  63.         postRequest = new PostRequest(getActivity(), this);
  64.  
  65.  
  66.         if (getArguments().getInt(ARG_TYPE_BRAND) == 0) {
  67.             postRequest.getBrands(page);
  68.         } else {
  69.             postRequest.getBrands(page);
  70.         }
  71.     }
  72.  
  73.     @Override
  74.     public void onLikeBrandListner(final int position) {
  75.         final PostRequest postRequestLike = new PostRequest(getActivity(), new PostRequest.OnReplyServerListener() {
  76.             @Override
  77.             public void onReplyServer(Reader reader) {
  78.                 Gson gson = new Gson();
  79.                 BrandModel brandslove = gson.fromJson(reader, BrandModel.class);
  80.                 if (brandslove.code == 0) {
  81.                     brandsItems.brands.get(position).loveCurrentCustomer = "1235";
  82.                 }
  83.             }
  84.         });
  85.         postRequestLike.LoveBrand(brandsItems.brands.get(position).brandswelove_id);
  86.  
  87.     }
  88.  
  89.     private void DownloadOther() {
  90.         wheel.spin();
  91.         postRequest.getBrands(page);
  92.     }
  93.  
  94.     @Override
  95.     public void onReplyServer(Reader reader) {
  96.         Gson gson = new Gson();
  97.         if (brandsItems == null) {
  98.             brandsItems = gson.fromJson(reader, BrandModel.class);
  99.             page++;
  100.  
  101.         } else {
  102.             BrandModel tempbrend = gson.fromJson(reader, BrandModel.class);
  103.             for (BrandModel.Brand brand : tempbrend.brands) {
  104.                 brandsItems.brands.add(brand);
  105.             }
  106.  
  107.             page++;
  108.         }
  109.  
  110.         if (mAdapter == null) {
  111.  
  112.             mAdapter = new RecyclerBrandsAdapter(getActivity(), LayoutManager.LayoutParams.HEADER_ALIGN_START, brandsItems);
  113.         //    mAdapter.setMarginsFixed(mAreMarginsFixed);
  114.             // mAdapter.setHeaderDisplay(LayoutManager.LayoutParams.HEADER_ALIGN_START);
  115.             mRecyclerView.setAdapter(mAdapter);
  116.  
  117.         } else {
  118.  
  119.  
  120.         }
  121.     }
  122.  
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement