2016-08-11 1 views
0

리사이틀보기를 스크롤하는 동안 어댑터에서 더 많은 콘텐츠를로드하려고합니다. 구현했지만 작동하지 않습니다. 내 리사이클 러 뷰는 완벽하게 작동하지만 스크롤하는 동안 내 목록에서 나머지 내용을로드 할 수는 없습니다. 현재 내 코드에는 오류가 없습니다. 스크롤하는 동안 더 많은 항목을로드 할 수 없습니다. 도와주세요. 이것은 내 주요 활동입니다.스크롤에서 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 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 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 interface OnLoadMoreListener { 
void onLoadMore(); 
} 

도와주세요,이 인터페이스를 가지고 0

답변

0

더 많은 검색 시간을 알기 위해서는 스크롤을 추적 할 필요가 없습니다.

당신은 예를 들어, 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

실행할 수없는 메서드는 무엇입니까? –

0

을 대신에만이 블록에 부하에게 더 많은 요청을 수행 다음 블록

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

OnLoadMoreListener를 호출. Like :

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

그런 다음 데이터가 변경되었음을 어댑터에 알립니다.

0

활동 클래스의 목록에 값을 추가하는 대신 어댑터 클래스에 값을 추가하여 시도하십시오. 예를 들어 아래와 같이 어댑터에 메소드를 추가한다고 가정 해보십시오.

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(); 
      } 
    }); 

가 본 TRY :

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(); 
       } 
     }); 

희망이 도움이 :)

관련 문제