Reputation: 1023
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();
}
Upvotes: 2
Views: 6124
Reputation: 1591
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;
}
}
Upvotes: 2
Reputation: 31
put this code in first line of getView Function :
if (convertView != null) return convertView;
Upvotes: 2
Reputation: 3118
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);
Upvotes: 0