在滚动时在ListView中附加数据,但每次追加后屏幕的焦点都会发生变化

时间:2015-11-23 10:04:55

标签: android android-listview

我正在尝试从api加载数据并尝试一次只列出10个项目,当我到达列表的底部时,我应该在列表中加载下一个10个数据,依此类推。

但是当加载数据时,列表会滚动回到顶部,但我希望列表的焦点应该保持不变,只是下一个数据应该加载。

这是代码:

lvCategory = (ListView) myFragmentView.findViewById(R.id.lvCategory);
lvCategory.setOnScrollListener(new AbsListView.OnScrollListener() {

   private int currentVisibleItemCount;
   private int currentScrollState;
   private int currentFirstVisibleItem;
   private int totalItem;
   private LinearLayout lBelow;

   @Override
   public void onScrollStateChanged(AbsListView view, int scrollState) {

       this.currentScrollState = scrollState;
       this.isScrollCompleted();
   }

   @Override
   public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

       this.currentFirstVisibleItem = firstVisibleItem;
       this.currentVisibleItemCount = visibleItemCount;
       this.totalItem = totalItemCount;

   }

   private void isScrollCompleted() {
       if (totalItem - currentFirstVisibleItem == currentVisibleItemCount
               && this.currentScrollState == SCROLL_STATE_IDLE) {
           /** To do code here*/


           Toast.makeText(getContext(), "end of scroll", Toast.LENGTH_SHORT).show();
           pageno++;
           getListData(pageno);
           Toast.makeText(getContext(), "set focus before", Toast.LENGTH_SHORT).show();
           lvCategory.setSelection(5);
           Toast.makeText(getContext(), "set focus after", Toast.LENGTH_SHORT).show();
           lvCategory.smoothScrollToPosition(11);
   }
   }
});

getListData(pageno);

这是getlistData的代码

private void getListData(int pageNo) {


        //String url = "http://httpbin.org/get?site=code&network=tutsplus";

       // String url = "http://52.27.73.255/api/index.php/product/category/get/1/10";

        // Request a string response

        //List<Category> categoryList;
        String noOfEntry ="10";
        if(pageNo==1){

            categoryList = new ArrayList<>();
        }
        String url = "http://52.27.73.255/api/index.php/product/category/get/"+pageNo+"/"+noOfEntry;
        JsonObjectRequest jsonRequest = new JsonObjectRequest
                (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        // the response is already constructed as a JSONObject!
                        try {
                           // String def = response.getString("url");
                            //response = response.getJSONObject("args");

                           String code = response.getString("code");
                            if(code.equals("1")) {
                                jsonArray = response.getJSONArray("document");


                                StringBuffer finalBufferedData = new StringBuffer();

                                for (int i = 0; i < jsonArray.length(); i++) {
                                    JSONObject finalObject = jsonArray.getJSONObject(i);
                                    Category category = new Category();
                                    category.setCategoryID(finalObject.getString("CategoryID"));
                                    category.setCategoryCompanyID(finalObject.getString("CategoryCompanyID"));
                                    category.setCategoryName(finalObject.getString("CategoryName"));
                                    category.setCategoryLogo(finalObject.getString("CategoryLogo"));
                                    category.setCategoryIsActive(finalObject.getString("CategoryIsActive"));
                                    category.setCategoryDescription(finalObject.getString("CategoryDescription"));

                                    categoryList.add(category);

                               /* String catId = finalObject.getString("CategoryID");

                                String  catCompanyId = finalObject.getString("CategoryCompanyID");
                                String catName = finalObject.getString("CategoryName");
                                String catLogo = finalObject.getString("CategoryLogo");
                                String catIsActive =finalObject.getString("CategoryIsActive");

                                finalBufferedData.append("" + i + catId + "-" + catCompanyId + "-" + catName + "\n");
*/


                                }


                                Toast.makeText(getContext(), "setAdapter before", Toast.LENGTH_SHORT).show();
                                CateoryAdapter adapter = new CateoryAdapter(getActivity(), R.layout.custom_layout, categoryList);


                                Toast.makeText(getContext(), "setAdapter before", Toast.LENGTH_SHORT).show();
                                lvCategory.setAdapter(adapter);
                                Toast.makeText(getContext(), "after setAdapter", Toast.LENGTH_SHORT).show();
                            }

                            else {
                                Toast.makeText(getContext(), "staus code 0", Toast.LENGTH_SHORT).show();
                            }
                            //responsetextview.setText(finalBufferedData.toString());
                           // String site = response.getString("site"),
                             //       network = response.getString("network");
                            //System.out.println("Site: " + site + "\nNetwork: " + network);

                            //Toast.makeText(getContext(), "getListData is executing", Toast.LENGTH_SHORT).show();

                            //responsetextview.setText(def);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                });

        Volley.newRequestQueue(getContext()).add(jsonRequest);


    }

我该怎么做才能让listview的焦点不再回到listview的顶部,它应该保持原样。 请帮忙。

1 个答案:

答案 0 :(得分:1)

因为您创建了一个新适配器并将其设置为列表。

而不是:

#include <iostream>
#include <regex>
using namespace std;

int main() {
    std::vector<std::string> strings;
    strings.push_back("1.200e+12");
    strings.push_back("1.0200e+12");
    strings.push_back("1.0000e+12");
    strings.push_back("1.0e+12");
    strings.push_back("1.e+12");
    strings.push_back("100e");
    strings.push_back("100e+12");
    std::regex reg(R"((\d*)(?:(?=[.]0*e)[.]0*|(?![.]0+e)([.][0-9]*?)0*)(e(?:[+]\d+)?))");
    for (size_t k = 0; k < strings.size(); k++)
    {
        std::cout << "Next string: " << strings[k] << std::endl;
        std::cout << "Replace result: " 
                     << std::regex_replace(strings[k], reg, "$1$2$3") << std::endl;
    }
    return 0;
}

只需将新数据添加到适配器(旧版,不要创建新适配器),然后调用CateoryAdapter adapter = new CateoryAdapter(getActivity(), R.layout.custom_layout, categoryList); lvCategory.setAdapter(adapter); 刷新adapter.notifyDataSetChanged();