Im trying to use the scrollBy method in my listview and its scrolling in the list but will make the list look weird by clipping items.
Im trying to refresh the list by using things like
l.invalidate();
l.requestLayout();
l.forceLayout();
l.invalidateViews();
l.postInvalidate();
l.refreshDrawableState();
But for no good results: see image after I scroll down 150 px
There are more entries below data5 but they have been clipped.
code:
public class TestListViewScrollActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ListView l = (ListView)findViewById(R.id.listView1);
final MyAdapter adapter = new MyAdapter(getApplicationContext());
l.setAdapter(adapter);
l.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
adapter.add();
l.scrollBy(0, 150);
l.invalidate();
l.requestLayout();
l.forceLayout();
l.invalidateViews();
l.postInvalidate();
l.refreshDrawableState();
}
});
}
}
class MyAdapter extends BaseAdapter {
ArrayList data = new ArrayList();
Context context;
public MyAdapter(Context context) {
data.add("data1");
data.add("data2");
data.add("data3");
data.add("data4");
data.add("data5");
data.add("data6");
data.add("data7");
data.add("data8");
data.add("data9");
this.context = context;
}
public void add() {
//data.add(0, "new data");
//notifyDataSetInvalidated();
notifyDataSetChanged();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if (convertView == null) {
LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.listitem, null);
} else {
v = convertView;
}
TextView text = (TextView) v.findViewById(R.id.text);
text.setText((String)data.get(position));
return v;
}
}