片段上的RecyclerView分页无限滚动问题

时间:2018-07-14 10:23:28

标签: android json android-fragments android-recyclerview

我正在尝试使用RecyclerView在片段上实现无尽无限滚动,但是在onResponse方法的片段call.enqueue方法上遇到错误,而且我无法在上方初始化List<Post> onCreateView方法。

以下是错误的屏幕截图:

ScreenShot of the error

我指的是该视频> https://youtu.be/tiXP__iYtq4?t=2070

但是这个人正在Activity上实现,我希望在Fragment上实现

  

依赖项

implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.github.bumptech.glide:glide:4.3.1'
implementation 'com.android.support:recyclerview-v7:27.1.1'

这是我的DataResponse类, 我在其中使用JSON对值的键的地方

  

DataResponse.java

import com.google.gson.annotations.SerializedName;
import java.util.List;
import retrofit2.Retrofit;

public class DataResponse {

@SerializedName("status")
private String Status;

@SerializedName("posts")
List<Posts> Posts;

public String getStatus() {
    return Status;
}

public List<Posts> getPosts() {
    return Posts;
}
}


class Posts {

@SerializedName("id")
private int post_id;

@SerializedName("images")
private String image;

@SerializedName("video")
private String video;

@SerializedName("title")
private String title;

public int getPost_id() {
    return post_id;
}

public String getImage() {
    return image;
}

public String getVideo() {
    return video;
}

public String getTitle() {
    return title;
}
}

我的ApiInterface类使用GET方法从loadmore.php文件中获取JSON值

  

ApiInterface.java

import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;

public interface ApiInterface {

@GET("loadmore.php")
Call<List<DataResponse>> getPost(@Query("page_number") int page, @Query("item_count") int items);

}

这是我的ApiClient类文件,其中包含base_url和Retrofit构建器方法

  

ApiClient.java

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class Apiclient {

private static final String base_url = "http://10.0.2.2/php/techtutor/api/";
public static Retrofit retrofit = null;

public  static Retrofit getApiclient(){
    if (retrofit==null){
        retrofit = new Retrofit.Builder().baseUrl(base_url).addConverterFactory(GsonConverterFactory.create()).build();

    }
    return retrofit;
}

}

带有MyViewewHolder的RecyclerAdapter使用默认构造函数将JSON值绑定到我的item_list

  

RecyclerAdapter.java

import android.content.Context;
import android.media.Image;
import android.support.annotation.NonNull;
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.List;

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {

private List<Posts> postList;
private Context context;

public RecyclerAdapter(List<Posts> postList, Context context){
    this.postList = postList;
    this.context = context;
}

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

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    Posts posts  = postList.get(position);
    holder.title.setText(posts.getTitle());
    Glide.with(context).load(posts.getImage()).into(holder.post_image);
}

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

public static class MyViewHolder extends RecyclerView.ViewHolder{

    ImageView post_image;
    TextView title;

    public MyViewHolder(View itemView) {
        super(itemView);

        post_image = itemView.findViewById(R.id.rounded_image_view_item_article);
        title = itemView.findViewById(R.id.text_view_item_article_title);

    }
}


public  void addPosts(List<Posts> posts){

    for (Posts pst : posts){
        postList.add(pst);
    }
}

}

这是我用底部导航视图实现的主要片段

  

fragment_home.java

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
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.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.maqueensoft.techtutor.ApiInterface;
import com.maqueensoft.techtutor.Apiclient;
import com.maqueensoft.techtutor.DataResponse;
import com.maqueensoft.techtutor.R;
import com.maqueensoft.techtutor.RecyclerAdapter;
import com.maqueensoft.techtutor.slider.CustomVolleyRequest;
import com.maqueensoft.techtutor.slider.SliderUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Timer;
import java.util.TimerTask;

import com.maqueensoft.techtutor.slider.ViewPagerAdapter;

import retrofit2.Call;
import retrofit2.Callback;


public class fragment_home extends Fragment {

/**RecyclerView Article**/

private RecyclerView recyclerView;
private ProgressBar progressBar;
private LinearLayoutManager layoutManager;
private ApiInterface apiInterface;
private RecyclerAdapter adapter;
//private List<Posts> postList;
private int page_number = 1;
private int item_count = 4;

public fragment_home() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    //return inflater.inflate(R.layout.fragment_home, container, false);

    View v = inflater.inflate(R.layout.fragment_home, container, false);


    recyclerView = v.findViewById(R.id.recycler_view);
    layoutManager = new LinearLayoutManager(getActivity());
    layoutManager.setOrientation(LinearLayout.VERTICAL);
    //recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());

    apiInterface = Apiclient.getApiclient().create(ApiInterface.class);
    Call<List<DataResponse>> call = apiInterface.getPost(page_number, item_count);

    call.enqueue(new Callback<List<DataResponse>>() {
        @Override
        public void onResponse(@NonNull Call<List<DataResponse>> call, @NonNull retrofit2.Response<List<DataResponse>> response) {

            assert response.body() != null;
            List<Posts> posts = response.body().get(1).getPosts();
            adapter = new RecyclerAdapter(posts, getActivity());
            recyclerView.setAdapter(adapter);

        }

        @Override
        public void onFailure(Call<List<DataResponse>> call, Throwable t) {

        }
    });

 }

}

这是load_more.php文件,我在其中传递两个2参数页码和项目计数,以在每次滚动事件时加载到fragment_home上

  

JSON数据

[
{
    "status": "ok"
},
{
    "post": [
        {
            "id": "1",
            "images": "AQAHITYIZEVSMG05HW.png",
            "video": "null",
            "title": "MaQueenSoft Apps 33+ Countries"
        },
        {
            "id": "2",
            "images": "M6PWP961N0LHHDXI1R.png",
            "video": "null",
            "title": "New Projects In MaQueenSoft"
        },
        {
            "id": "5",
            "images": "CSTFUGW0OMVDWBS4VU.jpg",
            "video": "youtube",
            "title": "Kar Har Maidaan Fateh Lyrical | Sanju | Ranbir Kapoor | Rajkumar Hirani | Sukhwinder Singh | Shreya"
        },
        {
            "id": "6",
            "images": "UU6OC2WEOROAA8CW00.png",
            "video": "null",
            "title": "torrent search engine"
        }
    ]
}
]

0 个答案:

没有答案