recyclerview滚动并保持在旧位置不要转到顶部 [英] recyclerview scroll and stay in old position dont go top

查看:95
本文介绍了recyclerview滚动并保持在旧位置不要转到顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是当我在回收视图中滚动数据时,所以加载了新数据,但问题是当新数据正在加载时,回收视图从第一个产品开始,

my problem is when I scroll my data in recycle view So new data loading but problem is when new data is loading my recycle view start from first product,

我需要保持旧的位置并只滚动新数据,而不是滚动所有新旧数据

I need to stay old position and scroll only new data not all data old and new





当前,我正在添加文件adeptoer文件和主文件的完整代码,

currently I am adding full code of my file adeptoer file and home file,


public class adepter extends RecyclerView.Adapter<adepter.viewholder> {

    private Context mCtx;
    private List<Product> productList;
        public adepter(Context mCtx, List<Product> productList) {
            this.mCtx = mCtx;
            this.productList = productList;
        }

    @NonNull
    @Override
    public adepter.viewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater=LayoutInflater.from(mCtx);
        View v =inflater.inflate(R.layout.product_list, parent, false);
        final viewholder vh = new viewholder(v);
        vh.itemView.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Product product = productList.get(vh.getAdapterPosition());
                //do the page opening here
                Intent intent = new Intent(mCtx, product_info.class);
                intent.putExtra("id",product.getId());

                mCtx.startActivity(intent);

            }
        });
        return vh;

    }

    @Override
    public void onBindViewHolder(@NonNull adepter.viewholder holder, int position) {
        Product product = productList.get(position);
        //loading the image
        Glide.with(mCtx)
                .load(product.getImage())
                .into(holder.imageView);
        holder.textViewTitle.setText(product.getTitle());
        holder.textViewShortDesc.setText(product.getShortdesc());
        holder.textViewtype.setText(String.valueOf(product.getType()));
        holder.textViewPrice.setText("Rs: "+String.valueOf(product.getPrice()));
    }

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





    public class viewholder extends RecyclerView.ViewHolder{
        TextView textViewTitle, textViewShortDesc, textViewtype, textViewPrice;
        ImageView imageView;
        public viewholder(@NonNull View itemView) {
            super(itemView);
            textViewTitle = itemView.findViewById(R.id.textViewTitle);
            textViewShortDesc = itemView.findViewById(R.id.textViewShortDesc);
            textViewtype = itemView.findViewById(R.id.textViewRating);
            textViewPrice = itemView.findViewById(R.id.textViewPrice);
            imageView = itemView.findViewById(R.id.imageView);
        }
    }
}


public class home extends AppCompatActivity {

  //a list to store all the products
    List<Product> productList;
    //the recyclerview
    RecyclerView recyclerView;
    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<>();





        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")
                                ));
                            }
                            //  Toast.makeText(getApplicationContext(),productList+ "volly ok ",Toast.LENGTH_LONG).show();

                          adepter a = new adepter(home.this, productList);
                            recyclerView.setAdapter(a);

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

}

推荐答案

尝试一下:

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

}

这篇关于recyclerview滚动并保持在旧位置不要转到顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆