Android : My custom listview scrolls back to first element instantly when scrolling up. How to prevent that and make a smooth scrolling

on Saturday, October 11, 2014


I am trying to create a custom listview like this: http://i.stack.imgur.com/l8ZOc.png


Currently I managed to create it but there is a problem. For example when there are 8 items, it loads the first 4, then loads the remiaining items as you scroll down. When scrolling down, it works fine.


However, when scrolling back up from bottom, at the middle of list, it instantly moves to top, skipping 2-3-4. items. Then you can scroll down normally again, but when scrolling up, it instantly goes to top of the list.


How can i prevent this from happening?


Here is my code:



public class MyActivity extends Activity implements AdapterView.OnItemClickListener {

String headers[];
String image_urls[];

List<MyMenuItem> menuItems;
ListView mylistview;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);

menuItems = new ArrayList<MyMenuItem>();
headers = getResources().getStringArray(R.array.header_names);
image_urls = getResources().getStringArray(R.array.image_urls);


for (int i = 0; i < headers.length; i++) {
MyMenuItem item = new MyMenuItem(headers[i], image_urls[i]);
menuItems.add(item);
}

mylistview = (ListView) findViewById(R.id.list);
MenuAdapter adapter = new MenuAdapter(this, menuItems);
mylistview.setAdapter(adapter);
mylistview.setOnItemClickListener(this);
}
}





public class MyMenuItem {

private String item_header;
private String item_image_url;

public MyMenuItem(String item_header, String item_image_url){
this.item_header=item_header;
this.item_image_url=item_image_url;
}

public String getItem_header(){
return item_header;
}

public void setItem_header(String item_header){
this.item_header=item_header;
}

public String getItem_image_url(){
return item_image_url;
}

public void setItem_image_url(String item_image_url){
this.item_image_url=item_image_url;
}


}





public class MenuAdapter extends BaseAdapter{

Context context;
List<MyMenuItem> menuItems;

MenuAdapter(Context context, List<MyMenuItem> menuItems) {
this.context = context;
this.menuItems = menuItems;
}

@Override
public int getCount() {
return menuItems.size();
}

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

@Override
public long getItemId(int position) {
return menuItems.indexOf(getItem(position));
}

private class ViewHolder {
ImageView ivMenu;
TextView tvMenuHeader;
}



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

ViewHolder holder = null;

LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.menu_item, null);
holder = new ViewHolder();

holder.tvMenuHeader = (TextView) convertView.findViewById(R.id.tvMenuHeader);
holder.ivMenu = (ImageView) convertView.findViewById(R.id.ivMenuItem);

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

MyMenuItem row_pos = menuItems.get(position);

Picasso.with(context)
.load(row_pos.getItem_image_url())
// .placeholder(R.drawable.empty)
// .error(R.drawable.error)
.into(holder.ivMenu);

holder.tvMenuHeader.setText(row_pos.getItem_header());

Log.e("Test", "headers:" + row_pos.getItem_header());
return convertView;
}

}

0 comments:

Post a Comment