Json Pagination in Retrofit 2

时间:2016-04-30 08:31:49

标签: android json pagination retrofit2

这是我的json响应模式: Link

{
  "categoryvideos": [
    {
      "id": "4229959",
      "title": "\u0631\u067e \u062e\u0648\u0627\u0646\u06cc \u0628\u0627 \u0633\u0641\u0627\u0631\u0634 \u063a\u0630\u0627",
      "username": "aliroyalclip",
      "userid": "1167911",
      "visit_cnt": 29,
      "uid": "EgVDe",
      "process": "no_stat",
      "sender_name": "\u06cc\u0627\u0642\u06cc \u06a9\u0644\u06cc\u067e",
      "big_poster": "http:\/\/static.asset.aparat.com\/\/avt\/4229959-6634-b__852753156.jpg",
      "small_poster": "http:\/\/static.asset.aparat.com\/\/avt\/4229959-6634__8895.jpg",
      "profilePhoto": "http:\/\/static.aparat.com\/public\/user_data\/profile_photo\/390\/1167911-m.jpg?915",
      "duration": "82",
      "sdate": "\u06f1\u06f1 \u0627\u0631\u062f\u06cc\u0628\u0647\u0634\u062a \u06f1\u06f3\u06f9\u06f5",
      "sdate_timediff": 3808,
      "official": "no",
      "autoplay": false,
      "video_date_status": "notset"
    },
    ...
],
  "ui": {
 ==>"pagingForward":  "http://www.aparat.com//etc/api/categoryvideos/cat/7/curoffset/20/id/4229608/moretype/forward",
    "pagingBack": ""
  }
}

这是我的RecyclerView适配器:

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

import ir.dink.aparatretrofit.R;
import ir.dink.aparatretrofit.model.Categoryvideo;

/**
 * Created by majid on 30/04/2016.
 */
public class AparatAdapter extends RecyclerView.Adapter<AparatAdapter.ViewHolder> {

    private Context context;
    private ArrayList<Categoryvideo> categoryVideos;

    //=============================constructor
    public AparatAdapter(Context context, ArrayList<Categoryvideo> categoryVideos) {
        this.context = context;
        this.categoryVideos = categoryVideos;
    }
    //=============================viewholder
    public class ViewHolder extends RecyclerView.ViewHolder{
        private TextView txtTitle ,txtCount ,txtSender ,txtDate;
        private ImageView imgRow;
        public ViewHolder(View itemView) {
            super(itemView);
            txtTitle = (TextView)itemView.findViewById(R.id.txt_title_row);
            txtCount = (TextView)itemView.findViewById(R.id.txt_count_dynamic_row);
            txtSender = (TextView)itemView.findViewById(R.id.txt_sender_dynamic_row);
            txtDate = (TextView)itemView.findViewById(R.id.txt_date_dynamic_row);
            imgRow = (ImageView)itemView.findViewById(R.id.img_row);
        }
    }

    //================================oncreateviewholder

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout, parent,false);
        return new ViewHolder(view);
    }

    //================================= onbindviewholder

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        holder.txtTitle.setText(categoryVideos.get(position).getTitle());
        holder.txtCount.setText(categoryVideos.get(position).getVisitCnt().toString());
        holder.txtSender.setText(categoryVideos.get(position).getSenderName());
        holder.txtDate.setText(categoryVideos.get(position).getSdate().toString());
        Glide.with(holder.itemView.getContext()).load(categoryVideos.get(position).getSmallPoster())
                .into(holder.imgRow);
    }

    //==================================getitemcount

    @Override
    public int getItemCount() {
        return categoryVideos.size();
    }
}

我使用www.jsonschema2pojo.org

生成模型

这是我的活动我加载Json并将它们绑定到RecyclerView:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import java.util.ArrayList;

import ir.dink.aparatretrofit.R;
import ir.dink.aparatretrofit.adapter.AparatAdapter;
import ir.dink.aparatretrofit.adapter.EndlessRecyclerViewScrollListener;
import ir.dink.aparatretrofit.model.Categoryvideo;
import ir.dink.aparatretrofit.model.JSONResponse;
import ir.dink.aparatretrofit.service.ApiInterface;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class SargarmiActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private AparatAdapter adapter;
    private LinearLayoutManager layoutManager;
    private ArrayList<Categoryvideo> categoryvideos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sargarmi);
        initViews();
    }
    //=================================================

    private void initViews() {
        recyclerView = (RecyclerView) findViewById(R.id.recycler_sargarmi);
        recyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(SargarmiActivity.this);
        recyclerView.setLayoutManager(layoutManager);
        sendRequest();
    }

    //=================================================
    private void sendRequest() {
        recyclerView.addOnScrollListener(new EndlessRecyclerViewScrollListener(layoutManager) {
            @Override
            public void onLoadMore(int page, int totalItemsCount) {
                loadMore();
            }
        });
        getData();
    }



    //=======================================================
    private void getData() {
        //1
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://www.aparat.com/etc/api/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        //2
        ApiInterface request = retrofit.create(ApiInterface.class);

        //3
        Call<JSONResponse> call = request.getJsonResponse();

        //4
        call.enqueue(new Callback<JSONResponse>() {
            @Override
            public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {
                if (response.isSuccessful()) {
                    JSONResponse jsonResponse = response.body();
                    categoryvideos = new ArrayList<>(jsonResponse.getCategoryvideos());
                    adapter = new AparatAdapter(SargarmiActivity.this, categoryvideos);
                    recyclerView.setAdapter(adapter);

                } else {
                    Toast.makeText(SargarmiActivity.this, "Error darim else onresponse", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<JSONResponse> call, Throwable t) {
                Toast.makeText(SargarmiActivity.this, "Error darim onFailure()", Toast.LENGTH_SHORT).show();
            }
        });
    }

    //=============== Load more Endless =====================
    private void loadMore() {

    }
}

我使用EndlessRecyclerView方式并实现LoadMore()方法,但我不知道如何实现分页。

0 个答案:

没有答案