EndlessRecyclerOnScrollListener无法在嵌套的RecyclerView

时间:2017-05-23 08:33:36

标签: java android

我有嵌套的RecyclerView,第一个父级是垂直,第二个是水平的孩子。

我正在尝试使用子级RecyclerView(使用Horizo​​ntal布局管理器)实现loadmore。

但它正在使用Parent和仅垂直滚动,使用以下代码时出现错误,或者它是EndlessRecyclerOnScrollListener中的错误。 我的意思是不可能使用嵌套的RecycleView和EndlessRecyclerOnScrollListener,如果没有请建议我另一个库。

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

    Context context;
    String BaseUrl;
    RestAdapter restAdapter;
    ServerApi serverApi;
    ArrayList<String> cataid_list=new ArrayList<String>();
    ArrayList<String> catageryname=new ArrayList<String>();
    int PageId=0;

    public class MyViewHolder extends RecyclerView.ViewHolder
    {
        public RecyclerView hori_recycler;
        public TextView catageryImg;
        LinearLayoutManager linearLayoutManager;

        public MyViewHolder(View itemView) {
            super(itemView);
            hori_recycler=(RecyclerView) itemView.findViewById(R.id.hori_recycler);
            catageryImg=(TextView) itemView.findViewById(R.id.catageryImg);
            linearLayoutManager=new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false);

        }
    }
    public HomeAdaptor_Mine(ArrayList<String> cataid_list, ArrayList<String> catageryname, Context context)
    {
    this.cataid_list=cataid_list;
        this.catageryname=catageryname;
        this.context=context;

        BaseUrl = context.getResources().getString(R.string.base_url);
        OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.setReadTimeout(5, TimeUnit.MINUTES.MINUTES);
        restAdapter = new RestAdapter.Builder().setEndpoint(BaseUrl).setClient(new OkClient(okHttpClient)).build();
        serverApi = restAdapter.create(ServerApi.class);
 }
    @Override
    public HomeAdaptor_Mine.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.horizontalcontentlayout,parent,false);

        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final HomeAdaptor_Mine.MyViewHolder holder, final int position) {

         holder.catageryImg.setText(catageryname.get(position));

        load_products(cataid_list.get(position),PageId,holder,position);


        holder.hori_recycler.addOnScrollListener(new EndlessRecyclerOnScrollListener(holder.linearLayoutManager) {
            @Override
            public void onLoadMore(int current_page) {

             //   PageId=PageId+1;
             //   load_products(cataid_list.get(position),PageId,holder,position);
                Toast.makeText(context, "ok", Toast.LENGTH_SHORT).show();
            }
        });

    }

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


  public  void load_products(final String catid, final int pageID, final MyViewHolder holder,final int position) {

        serverApi.fetch_home_products_by_cat(catid, "" + pageID, new Callback<Response>() {
            @Override
            public void success(Response response, Response response2) {
                String s = new String(((TypedByteArray) response.getBody()).getBytes());

                ArrayList<String> item_title=new ArrayList<String>();
                ArrayList<String> image_url=new ArrayList<String>();

                try {
                    JSONObject jsonObject = new JSONObject(s);
                    if (jsonObject != null) {
                        int success = jsonObject.getInt("success");

                        if (success == 1) {

                            JSONArray output= jsonObject.getJSONArray("output");

                            for (int i=0;i<output.length();i++)
                            {
                                JSONObject js=output.getJSONObject(i);

                                item_title.add(js.getString("item_title"));
                                image_url.add(js.getString("image_url"));

                            }

    holder.hori_recycler.setLayoutManager(holder.linearLayoutManager);
    holder.hori_recycler.setAdapter(new Test_Adaptor(context, item_title, image_url, holder.hori_recycler, holder.linearLayoutManager));
                            holder.hori_recycler.setHasFixedSize(true);

                        } else {
                            //         dialog.dismiss();
                            Toast.makeText(context, "No product found", Toast.LENGTH_SHORT).show();
                        }
                    }
                } catch (Exception e) {
                    //     dialog.dismiss();
                    Toast.makeText(context, "Something went wrong on server", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void failure(RetrofitError error) {
                //    dialog.dismiss();
                Toast.makeText(context, "Something went wrong on server", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

0 个答案:

没有答案