我在我的活動中有RecyclerView的片段,但每當我開始片段時,RecyclerView已經滾動到底部。要加載更多的項目,用戶必須向上滾動一些,然後滾動回到底部,並且當更多的項目加載時,RecyclerView再次滾動到新的底部。當活動開始時,RecyclerView已經滾動到底部
這裏是片段中的代碼。
public class FavouriteFragment extends Fragment{
public RecyclerView recyclerView ;
public RecyclerView.Adapter adapter ;
public LinearLayoutManager layoutManager ;
int pastVisiblesItems, visibleItemCount, totalItemCount;
Constants constants;
ArrayList<CardSetterGetter> arrayList;
private List<CardSetterGetter> imagelist;
int start ;
public int end ;
boolean loading = true ;
int totalcount ;
ArrayList<String> imageNameArray ;
String androidId;
String categoryName ;
String cateGoryFlag = "false";
String url;
String tagname = "";
ArrayList<String> title ;
ProgressBar progressBar ;
TextView errormsg ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//to hide search
setHasOptionsMenu(true);
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
MenuItem item = menu.findItem(R.id.search);
item.setVisible(false);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.favourite_fragment,container,false);
arrayList = new ArrayList<CardSetterGetter>();
imageNameArray = new ArrayList<String>();
start = 0;
end = start + 2 ;
imagelist = new ArrayList<CardSetterGetter>();
recyclerView = (RecyclerView)view.findViewById(R.id.recyclerview);
progressBar = (ProgressBar)view.findViewById(R.id.progressBar);
errormsg = (TextView) view.findViewById(R.id.error);
getdata();
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if(dy > 0)
{
visibleItemCount = layoutManager.getChildCount();
totalItemCount = layoutManager.getItemCount();
pastVisiblesItems = layoutManager.findFirstVisibleItemPosition();
int i = visibleItemCount + pastVisiblesItems ;
System.out.println(i);
System.out.println(totalItemCount);
if(loading) {
if (i == totalItemCount) {
loading = false ;
start = arrayList.size() ;
end = arrayList.size() + 2 ;
requestWithSomeHttpHeaders2(start,end);
new Handler().postDelayed(new Runnable() { // This thread runs in the UI
@Override
public void run() {
loading = true;
}
},2000);
}
}
}
}
});
requestWithSomeHttpHeaders2(start, end);
return view ;
}
public void requestWithSomeHttpHeaders2(int s , int e) {
RequestQueue queue = Volley.newRequestQueue(getActivity());
url = "my url";
StringRequest postRequest = new StringRequest(Request.Method.GET, url ,
new Response.Listener<String>()
{
@Override
public void onResponse(String response) {
try {
progressBar.setVisibility(View.GONE);
JSONObject jsonObject = new JSONObject(response);
JSONArray payload = jsonObject.getJSONArray("Payload");
JSONObject MetaData = jsonObject.getJSONObject("MetaData");
System.out.println(response);
totalcount = MetaData.getInt("TotalCount");
System.out.println("200" + " " + totalcount);
System.out.println("payload length"+payload.length());
int i ;
for(i =0 ; i < payload.length() ; i++)
{
System.out.println(payload.getJSONObject(i).getString("image_title"));
System.out.println(payload.getJSONObject(i).getString("image_title"));
System.out.println(payload.getJSONObject(i).getString("image_file"));
System.out.println(payload.getJSONObject(i).getString("image_description"));
System.out.println(payload.getJSONObject(i).getString("image_category"));
System.out.println(payload.getJSONObject(i).getString("image_tags"));
System.out.println(payload.getJSONObject(i).getString("id"));
System.out.println(payload.getJSONObject(i).getString("favourite"));
String newString = payload.getJSONObject(i).getString("image_file").replace("original_image", "thumbnail");
CardSetterGetter all = new CardSetterGetter(payload.getJSONObject(i).getString("image_file"),
payload.getJSONObject(i).getString("image_title"),
payload.getJSONObject(i).getString("image_description"),
payload.getJSONObject(i).getString("image_category"),
payload.getJSONObject(i).getString("image_tags"),
payload.getJSONObject(i).getString("id"),
payload.getJSONObject(i).getString("favourite"));
System.out.println("i value is " + i);
arrayList.add(all);
Handler handler = new Handler();
final Runnable r = new Runnable() {
public void run() {
adapter = new RecyclerViewAdapter(getActivity(),arrayList,imageNameArray,totalItemCount);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
//adapter.notifyItemInserted(arrayList.size() - 1);
if (end >= totalcount)
{
int index = totalcount -1 ;
recyclerView.scrollToPosition(index);
}
else {
recyclerView.scrollToPosition(end);
}
}
};
handler.post(r);
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("error" + e);
}
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
Log.d("ERROR","error => "+error.toString());
// definitions.setText("check your internet connection");
NetworkResponse networkResponse = error.networkResponse;
// Log.e("Volley", "Error. HTTP Status Code:"+networkResponse.statusCode);
if (networkResponse != null) {
Log.e("Volley", "Error. HTTP Status Code:"+networkResponse.statusCode);
}
if (error instanceof TimeoutError) {
Log.e("Volley", "TimeoutError");
}else if(error instanceof NoConnectionError){
Log.e("Volley", "NoConnectionError");
} else if (error instanceof AuthFailureError) {
Log.e("Volley", "AuthFailureError");
} else if (error instanceof ServerError) {
Log.e("Volley", "ServerError");
} else if (error instanceof NetworkError) {
Log.e("Volley", "NetworkError");
} else if (error instanceof ParseError) {
Log.e("Volley", "ParseError");
}
}
}
);
queue.add(postRequest);
}
public void getdata(){
try {
SharedPreferences sharedPref = getActivity().getSharedPreferences("mobileId" , Context.MODE_PRIVATE);
androidId = sharedPref.getString("mobileId" , "");
sharedPref = getActivity().getSharedPreferences("categoryInfo" , Context.MODE_PRIVATE);
categoryName = sharedPref.getString("categoryName" , "");
cateGoryFlag = sharedPref.getString("categoryFlag" , "");
tagname = sharedPref.getString("tagname" , "");
}catch (Exception e){
}
}
}
我試着刪除,如果代碼的else部分,但取得了recyclerView滾動每當用戶到達recyclerView底部的頂部。
請告訴我,如果您需要更多的代碼或代碼RecyclerViewAdapter
。
您是否嘗試過使用其他經理構造函數:LinearLayoutManager(activity,orientation,reverseLayout)?如果我沒有記錯,我有一個類似的問題與RecyclerView,我通過設置佈局方向(垂直或水平,無論您需要)來解決它。 –
@EdoardoArnaudo不,我沒有嘗試過。我不知道該怎麼做。你能發表一個答案解釋更多關於它 –