0

I am developing an app in which I have a listview in which 5 data are visible to user.when I scroll bottom of listview and a progress bar at the bottom of listview is visible to me and server request is send at this point and 5 more data are added in data set, Here I want to make this new data visible to user when added in data set. i.e. move scroll position on new data set.

code:-

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
    if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE && bBottomOfView) {
        Log.i("Listview", "scrolling stopped...");
        if (NetworkUtil.isConnected(getActivity())) {
            sz_RecordCount = String.valueOf(m_n_DefaultRecordCount);// convert int value to string
            sz_LastCount = String.valueOf(CLastCountData.getInstance().getS_szLastCount());// convert int value to string /////

            Log.e(TAG, "Last Count::" + sz_LastCount);
            Log.e(TAG, "Record count::" + sz_RecordCount);

            loadmoreData();


        } else {
            CSnackBar.getInstance().showSnackBarError(m_MainLayout, "No internet connection available", getActivity());
            m_ListView.removeFooterView(mFooter);
        }

    }

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, m_DealListingURL, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Log.i(TAG, "Server Response:-" + response);
                if (mSwipeRefresh.isRefreshing()) {
                    mSwipeRefresh.setRefreshing(false);
                }
                try {
                    int nResultCodeFromServer = Integer.parseInt(response.getString("resultcode"));

                    if (nResultCodeFromServer == CStaticVar.m_kTRANSACTION_SUCCESS) {
                        JSONArray posts = response.optJSONArray("dealList");// GETTING DEAL LIST
                        for (int i = 0; i < posts.length(); i++) {
                            JSONObject post = posts.getJSONObject(i);// GETTING DEAL AT POSITION AT I
                            item = new CDealAppDatastorage();// object create of DealAppdatastorage
                            item.setM_szHeaderText(post.getString("dealname"));//getting deal name
                            item.setM_szsubHeaderText(post.getString("dealcode"));// getting deal code
                            item.setM_szDealValue(post.getString("dealvalue"));

                            if (!s_oDataset.contains(item)) {

                                s_oDataset.add(item);
                            }
                        }
                        m_oAdapter.notifyDataSetChanged();
                        arrayCount = posts.length();
                        int add = CLastCountData.getInstance().getS_szLastCount() + arrayCount;
                        CLastCountData.getInstance().setS_szLastCount(add);
                        Log.e(TAG, "ArrayCount::" + arrayCount);
                        m_ListView.removeFooterView(mFooter);


                    }
earthw0rmjim
  • 17,394
  • 8
  • 45
  • 61
Satish
  • 121
  • 1
  • 6

2 Answers2

0

Following code will help you to maintain the scroll position of your listview:

First off all you have to get the current child position of listview from below code:

// this code should be written before updating adapter in your listview
int lastViewedPosition = listView.getFirstVisiblePosition();

//get offset of first visible view
View v = listView.getChildAt(0);
int  topOffset = (v == null) ? 0 : v.getTop();

then after updating your data in adapter you have to call this:

listView.setSelectionFromTop(lastViewedPosition, topOffset);

Like in your code:

    Log.i(TAG, "Server Response:-" + response);
                if (mSwipeRefresh.isRefreshing()) {
                    mSwipeRefresh.setRefreshing(false);
                }
                try {
                    int nResultCodeFromServer = Integer.parseInt(response.getString("resultcode"));

                    if (nResultCodeFromServer == CStaticVar.m_kTRANSACTION_SUCCESS) {

int lastViewedPosition = listView.getFirstVisiblePosition();

//get offset of first visible view
View v = listView.getChildAt(0);
int  topOffset = (v == null) ? 0 : v.getTop();



       JSONArray posts = response.optJSONArray("dealList");// GETTING DEAL LIST
                        for (int i = 0; i < posts.length(); i++) {
                            JSONObject post = posts.getJSONObject(i);// GETTING DEAL AT POSITION AT I
                            item = new CDealAppDatastorage();// object create of DealAppdatastorage
                            item.setM_szHeaderText(post.getString("dealname"));//getting deal name
                            item.setM_szsubHeaderText(post.getString("dealcode"));// getting deal code
                            item.setM_szDealValue(post.getString("dealvalue"));

                            if (!s_oDataset.contains(item)) {

                                s_oDataset.add(item);
                            }
                        }
                        m_oAdapter.notifyDataSetChanged();
listView.setSelectionFromTop(lastViewedPosition, topOffset);

                        arrayCount = posts.length();
                        int add = CLastCountData.getInstance().getS_szLastCount() + arrayCount;
                        CLastCountData.getInstance().setS_szLastCount(add);
                        Log.e(TAG, "ArrayCount::" + arrayCount);
                        m_ListView.removeFooterView(mFooter);
Anjali
  • 3,080
  • 1
  • 12
  • 22
  • where do I put that code before notifyseChanged or after – Satish Sep 15 '16 at 10:39
  • before adding additional items to your list just get the poistion of last scroll postion and after notifyseChanged listView.setSelectionFromTop(lastViewedPosition, topOffset); – Anjali Sep 15 '16 at 10:40
  • listview scroll does not moving – Satish Sep 15 '16 at 10:53
  • http://stackoverflow.com/questions/3014089/maintain-save-restore-scroll-position-when-returning-to-a-listview will help you – Anjali Sep 15 '16 at 10:55
0

Just use listView.setSelection(position of item you want to scroll)

vaibhav kumar
  • 415
  • 2
  • 11
  • but how to define position of item if data are coming from server .....may be it is 5 or 4 or 3 – Satish Sep 15 '16 at 11:11
  • you have posts.length(). I think this is giving the total new item load count. if it is not then you can add a globel parameter for previous count. – vaibhav kumar Sep 15 '16 at 11:22
  • if I implement listView.setSelection(position of item you want to scroll) if post.length() becomes 3 then what do I enter at position – Satish Sep 15 '16 at 11:24
  • m_ListView.removeFooterView(mFooter); listView.setSelection(adapter.getCount()-post.length()); – vaibhav kumar Sep 15 '16 at 11:38
  • if view not scroll then you can try with this mListView.post(new Runnable() { @Override public void run() { mListView.setSelection(index); } }); – vaibhav kumar Sep 15 '16 at 11:39