栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

recyclerview滚动并保持在旧位置不要转到顶部

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

recyclerview滚动并保持在旧位置不要转到顶部

试试这个:

public class home extends AppCompatActivity {  //a list to store all the products    List<Product> productList;    //the recyclerview    RecyclerView recyclerView;    adepter a;    ProgressBar Pbar;    GridLayoutManager manager;    int token = 1;  Boolean isScrolling = false;    int currentItems, totalItems, scrollOutItems;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_home);        Pbar = findViewById(R.id.progressBar1);        //getting the recyclerview from xml        recyclerView = findViewById(R.id.recylcerView);        recyclerView.setHasFixedSize(true);         manager = new GridLayoutManager(this, 2);        recyclerView.setLayoutManager(manager);        //initializing the productlist        productList = new ArrayList<>(); a = new adepter(home.this, productList);     recyclerView.setAdapter(a);        recyclerView.addonScrollListener(new RecyclerView.onScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) {     super.onScrollStateChanged(recyclerView, newState);     if(newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL)     {         isScrolling = true;     } } @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) {     super.onScrolled(recyclerView, dx, dy);     currentItems = manager.getChildCount();     totalItems = manager.getItemCount();     scrollOutItems = manager.findFirstVisibleItemPosition();     if(isScrolling && (currentItems + scrollOutItems == totalItems))     {         isScrolling = false;         token++;         getData();     } }        });        getData();    }    private void   getData(){         String url = "https://shop.com/index.php";         url = url+ "&pg="+ String.valueOf(token);        Pbar.setVisibility(View.VISIBLE);        StringRequest stringRequest = new StringRequest(Request.Method.GET, url,     new Response.Listener<String>() {         @Override         public void onResponse(String response) {  //   Toast.makeText(getApplicationContext(),"volly ok ",Toast.LENGTH_LONG).show();  try {      //converting the string to json array object      JSonArray array = new JSonArray(response);      //traversing through all the object      for (int i = 0; i < array.length(); i++) {          //getting product object from json array          JSonObject product = array.getJSonObject(i);          //adding the product to product list          productList.add(new Product(       product.getInt("id"),       product.getString("title"),       product.getString("shortdesc"),       product.getString("type"),       product.getString("price"),       product.getString("image")          ));      }      a.notifyDataSetChanged();      Pbar.setVisibility(View.GONE);  } catch (JSonException e) {      e.printStackTrace();  }         }     },     new Response.ErrorListener() {         @Override         public void onErrorResponse(VolleyError error) {  Toast.makeText(getApplicationContext(), "Server Error" ,Toast.LENGTH_LONG).show();         }     });        //adding our stringrequest to queue        Volley.newRequestQueue(this).add(stringRequest);    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/407265.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号