滚动的Android回收站视图

问题描述:

我想在滚动回收站视图的同时从我的适配器加载更多内容。我实现了它,但它不工作。我的回收视图完美地工作,但我无法在滚动时从列表中加载剩余的内容。目前在我的代码中没有错误。但滚动时我无法加载更多项目。请帮忙。 这是我的主要活动。滚动的Android回收站视图

public class Contact_school extends AppCompatActivity implements SearchView.OnQueryTextListener { 
private List<Contact> Listcontact = new ArrayList<>(); 
Contacts_Adapter cadapter; 
private RecyclerView rv; 
public String GET_CONTACTS = "http://xyzx.com/Publicpages_mob/brief_school_details"; 
    public Contact contact; 
public String school_id; 
String tag_json_obj = "json_obj_req"; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_contact_school); 
    rv = (RecyclerView) findViewById(R.id.recycler_view); 
    preparelist(); 
    cadapter = new Contacts_Adapter(Listcontact, rv); 
    final LinearLayoutManager layoutManager = new  LinearLayoutManager(Contact_school.this); 
    rv.setLayoutManager(layoutManager); 
    rv.setItemAnimator(new DefaultItemAnimator()); 
    rv.setAdapter(cadapter); 
    rv.setHasFixedSize(true); 

    cadapter.setOnLoadMoreListener(new OnLoadMoreListener() { 
     @Override 
     public void onLoadMore() { 

       Log.v(" KILILII ", "KITTTI"); 
     //add null , so the adapter will check view_type and show progress bar at bottom 
////    Listcontact.add(null); 
////    cadapter.notifyItemInserted(Listcontact.size() - 1); 

    // remove progress item 
////    Listcontact.remove(Listcontact.size() - 1); 
////    cadapter.notifyItemRemoved(Listcontact.size()); 
//    //add items one by one 
      int start = Listcontact.size(); 
      int end = start + 20; 
      for (int i = start + 1; i <= end; i++) { 
       Listcontact.add(contact); 
       cadapter.notifyItemInserted(Listcontact.size()); 
      } 
      cadapter.setLoaded(); 
       //or you can add all at once but do not forget to call mAdapter.notifyDataSetChanged(); 
      } 
    }); 
} 

private void preparelist() { 
    final StringRequest strReq = new StringRequest(Request.Method.POST, GET_CONTACTS,    new Response.Listener<String>() { 
     @Override 
     public void onResponse(String response) { 
      try { 
       JSONObject jObj = new JSONObject(response); 
       Log.e("RESPONSE TEST", "" + jObj); 
       JSONArray jsonArray = jObj.getJSONArray("school_details"); 
       Log.e("RESPONSE ARRAY", "" + jsonArray); 
        Log.d("SUCESS ", response); 
       for (int i = 0; i < jsonArray.length(); i++) { 
        JSONObject jsonobject = jsonArray.getJSONObject(i); 
         contact = new Contact(); 
         contact.setAddress(jsonobject.getString("school_address")); 
         contact.setRating(jsonobject.getString("school_rating")); 
     Listcontact.add(contact); 
       } 
       cadapter.notifyDataSetChanged(); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      Log.e("Error", "Registration Error: " + error.getMessage()); 
      Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show(); 
     } 
    }) 
    { 
     @Override 
     protected Map<String, String> getParams() { 
      // Posting params to register url 
      Map<String, String> params = new HashMap<String, String>(); 
      return params; 
     } 
    }; 
    AppController.getInstance().addToRequestQueue(strReq, tag_json_obj); 
} 

这是我的适配器类

public class Contacts_Adapter extends RecyclerView.Adapter<Contacts_Adapter.MyViewHolder> { 
private List<Contact> List1; 

private final int VIEW_ITEM = 1; 
private final int VIEW_PROG = 0; 
private int visibleThreshold = 5; 
private int lastVisibleItem, totalItemCount; 
private boolean loading; 
private OnLoadMoreListener onLoadMoreListener; 
public class MyViewHolder extends RecyclerView.ViewHolder { 
     public TextView text_address,text_rating; 

    public MyViewHolder(View view) { 
     super(view); 
     text_address = (TextView)view.findViewById(R.id.textaddress); 

     text_rating = (TextView)view.findViewById(R.id.textrating); 

    } 
} 
public Contacts_Adapter(List<Contact> List1,RecyclerView recyclerView) 
{ 
    this.List1 = List1; 
    if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) { 
     final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView 
       .getLayoutManager(); 
     recyclerView 
       .addOnScrollListener(new RecyclerView.OnScrollListener() { 
        @Override 
        public void onScrolled(RecyclerView recyclerView, 
              int dx, int dy) { 
         super.onScrolled(recyclerView, dx, dy); 
         totalItemCount = linearLayoutManager.getItemCount(); 
         lastVisibleItem = linearLayoutManager 
           .findLastVisibleItemPosition(); 
         if (!loading 
           && totalItemCount <= (lastVisibleItem + visibleThreshold)) { 
          // End has been reached 
          // Do something 
          if (onLoadMoreListener != null) { 
           onLoadMoreListener.onLoadMore(); 
          } 
          loading = true; 
         } 
        } 
       }); 
    } 
} 
@Override 
public int getItemViewType(int position) { 
    return List1.get(position) != null ? VIEW_ITEM : VIEW_PROG; 
} 
@Override 
public Contacts_Adapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View itemView = LayoutInflater.from(parent.getContext()) 
      .inflate(R.layout.recycler, parent, false); 
    return new MyViewHolder(itemView); 
} 
@Override 
public void onBindViewHolder(MyViewHolder holder, final int position) { 
    Contact c = List1.get(position); 

    holder.text_rating.setText("Rating "+c.getRating()); 

    holder.text_address.setText(c.getAddress()); 

} 
public void setLoaded() { 
    loading = false; 
} 
public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) { 
    this.onLoadMoreListener = onLoadMoreListener; 
} 

这是我的模型类

public class Contact implements Serializable { 
private static final long serialVersionUID = 1L; 
    private String address,rating; 
public Contact(){ 
} 
public Contact (String address, String rating){ 
    this.address = address; 
    this.rating= rating; 

} 

public String getAddress(){ return address;} 
public String getRating(){return rating;} 
public void setAddress(String ad){ this.address= ad;} 
public void setRating(String ad){ this.rating= ad;} 
} 

,我有这个接口

public interface OnLoadMoreListener { 
void onLoadMore(); 
} 

请帮助

您无需跟踪滚动条即可知道何时搜索更多itens。

您甚至可以从onBindViewHolder凌空请求,例如:

int itemsBeforeUpdate = 10; 
public void onBindViewHolder(MyViewHolder holder, final int position) { 
    Contact c = List1.get(position); 
    holder.text_rating.setText("Rating "+c.getRating()); 
    holder.text_address.setText(c.getAddress()); 
    if(List1.size() - position <10){ 
    // Do your Volley Request, add response to the list and notify the adapter that the data changed 

} 


} 
+0

当我使用此代码时,我得到以下异常:“java.lang.IllegalStateException:在RecyclerView计算布局或滚动时无法调用此方法” – Bivin

+0

您无法执行的方法? –

而是在下面的块

if (!loading && totalItemCount <= (lastVisibleItem + visibleThreshold)) { 
     // End has been reached 
     // Do something 
     if (onLoadMoreListener != null) { 
      onLoadMoreListener.onLoadMore(); 
     } 
     loading = true; 
} 

调用OnLoadMoreListener就只有这一块执行你的负载更多要求。如:

if (!loading&& totalItemCount <= (lastVisibleItem + visibleThreshold)) { 
    // Load your volley request or load more request her 
    loading = true; 
} 

并在此之后通知适配器数据已更改。

除了将值添加到活动类中的列表中,您可以通过将其添加到适配器类来尝试它。比如说在下面的适配器中添加一个方法。

public void addContact(Contact contact) { 
list.add(contact); 
notifyItemInserted(list.size()); 
} 

而在活动,而不是这个,

代码:

cadapter.setOnLoadMoreListener(new OnLoadMoreListener() { 
     @Override 
     public void onLoadMore() { 

       Log.v(" KILILII ", "KITTTI"); 
     //add null , so the adapter will check view_type and show progress bar at bottom 
////    Listcontact.add(null); 
////    cadapter.notifyItemInserted(Listcontact.size() - 1); 

    // remove progress item 
////    Listcontact.remove(Listcontact.size() - 1); 
////    cadapter.notifyItemRemoved(Listcontact.size()); 
//    //add items one by one 
      int start = Listcontact.size(); 
      int end = start + 20; 
      for (int i = start + 1; i <= end; i++) { 
       Listcontact.add(contact); 
       cadapter.notifyItemInserted(Listcontact.size()); 
      } 
      cadapter.setLoaded(); 
       //or you can add all at once but do not forget to call mAdapter.notifyDataSetChanged(); 
      } 
    }); 

试试这个:

cadapter.setOnLoadMoreListener(new OnLoadMoreListener() { 
      @Override 
      public void onLoadMore() { 

        Log.v(" KILILII ", "KITTTI"); 

       int start = Listcontact.size(); 
       int end = start + 20; 
       for (int i = start + 1; i <= end; i++) { 
        cadapter.addContact(contact); 
       } 
       cadapter.setLoaded(); 
       } 
     }); 

希望这会有所帮助:)