2

In firebase chat example application, when data updated(change, remove, move, add) listview scrolling to down. Source: https://github.com/firebase/AndroidChat

How to prevent scroll while 30 seconds after last scrollStateChanged?

        msgListView.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            scroll_idle_time = System.currentTimeMillis();
        }

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

        }
    });

    public static boolean canScroll() {
    return scroll_idle_time + 30000 < System.currentTimeMillis();
}
Suleiman
  • 913
  • 2
  • 13
  • 28

3 Answers3

2

put this code in first line of getView Function :

if (convertView != null) return convertView;
2

I have solved with following code

 public class CustomAdapter extends BaseAdapter {

    Activity activity;
    ArrayList<SectionsDetailsPOJO> alSectionDetails;

    public CustomAdapter(Activity a, ArrayList<SectionsDetailsPOJO> d) {
        activity = a;
        alSectionDetails = d;
    }


    @Override
    public int getViewTypeCount() {
        if(alSectionDetails.size()>0) {
            return alSectionDetails.size();
        }
        return 1;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getCount() {
        if (alSectionDetails.size() <= 0)
            return 1;
        return alSectionDetails.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;

        final LayoutInflater inflater = ( LayoutInflater )activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.custom_view, null);

            holder = new ViewHolder();
            holder.SectionNo = (TextView) convertView.findViewById(R.id.textView1);
            holder.SectionHeader=(TextView)convertView.findViewById(R.id.textView2);

            holder.SectionNo.setText(alSectionDetails.get(position).getSection_no());
            holder.SectionHeader.setText(alSectionDetails.get(position).getSection_header());

            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
            convertView.setTag(holder);
        }
        return convertView;
    }

    public  class ViewHolder {
        public TextView SectionNo;
        public TextView SectionHeader;
    }
}
Kishor N R
  • 1,371
  • 1
  • 14
  • 21
0

Solution found at: Retaining position in ListView after calling notifyDataSetChanged

//Save where you last were in the list.
int index = mList.getFirstVisiblePosition();
View v = mList.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();

// Call to notify, or updated(change, remove, move, add)
notifyDatasetChanged(); 

//Prevents the scroll to the new item at the new position
mList.setSelectionFromTop(index, top);
Community
  • 1
  • 1
Lucas Crawford
  • 2,970
  • 2
  • 12
  • 25
  • i don't want to disable scroll, i want stop autoscrolling whet call notifyDataSetChanged(). I want to give to User a time to read previous messages, but in my case, when i scroll up and want to read messages somebody add new message and listview scrolling to bottom – Suleiman May 15 '15 at 19:03
  • Ahh, your question made me read it as disabling scroll, in this case check my edit – Lucas Crawford May 15 '15 at 19:10
  • can you show code for https://github.com/firebase/AndroidChat ? My application identicial . I also try to override setDataSetChanged() and always select listView item at fixed position, but listView always scrolling to bottom – Suleiman May 15 '15 at 19:48