我有一个listview,我从数据库填充。
列表视图需要一些时间(<300毫秒)来填充列表。
如果我在 onActivityCreated 函数上执行 smoothScrollToPosition ,它什么都不做。
在OnCreate函数上,我必须等待〜200ms才能调用smoothScrollToPosition,在此之前,即使ListView对象也没有初始化。
我可以使用如下的getViewTreeObserver运行正常,但它需要最小的sdk版本16.我试图让它适用于版本14~15。
mListView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mListView.smoothScrollToPosition(adapter.getCount());
// unregister listener (this is important)
mListView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
我正在使用的代码 -
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
list = new ArrayList<Chat>();
db = new DBAdapter(getContext());
adapter = new CustomChatAdapter(getContext(), list);
Log.d("ListView1", "Value of f: " + f);
thread= new Thread(){
@Override
public void run(){
try {
synchronized(this){
wait(200);
}
}
catch(InterruptedException ex){
Log.d("ListView1", "Interrupted Exception: " +ex.toString());
}
Log.d("ListView1", "Adapter Count: " +adapter.getCount());
mListView.smoothScrollToPosition(adapter.getCount());
}
};
getMsg("1", getActivity().getIntent().getStringExtra("key"));
thread.start();
Log.d("ListView1", "Adapter Count: " +adapter.getCount());
//mListView.smoothScrollToPosition(adapter.getCount());
}
public void getMsg(String myid, String friend_id) {
list.clear();
try {
Cursor cur = db.get_friend_shouts(myid,friend_id);
String img_id1 = "";
String img_id2 ="";
if (cur != null) {
if (cur.moveToFirst()) {
do {
img_id1 = cur.getString(cur.getColumnIndex("image_id"));
img_id1 = img_id1 == null ? "null" : img_id1;
img_id2 = cur.getString(cur.getColumnIndex("r_img_id"));
img_id2 = img_id2 == null ? "null" : img_id2;
list.add(new Chat( img_id1 , cur.getString(cur.getColumnIndex("firstname")) + " " + cur.getString(cur.getColumnIndex("lastname")), cur.getString(cur.getColumnIndex("user_id")),
cur.getString(cur.getColumnIndex("shout_msg")) ,
img_id2, cur.getString(cur.getColumnIndex("r_firstname")) + " " + cur.getString(cur.getColumnIndex("r_lastname")) , cur.getString(cur.getColumnIndex("r_user_id")) , cur.getString(cur.getColumnIndex("rec_time")) ));
//Log.d("ListView1", "User details: " + cur.getString(cur.getColumnIndex("user_id")) + " " + cur.getString(cur.getColumnIndex("firstname")) + cur.getString(cur.getColumnIndex("lastname")) + " " + cur.getString(cur.getColumnIndex("image_id")));
} while (cur.moveToNext());
}
}
adapter.notifyDataSetChanged();
if(mListView!=null)
mListView.smoothScrollToPosition(adapter.getCount());
} catch (Exception e) {
Log.d("ListView1", "getMsg Error: " + e.toString());
}
}
ArrayAdapter类 -
public class CustomChatAdapter extends ArrayAdapter<Chat> {
private final Context context;
private final List<Chat> list;
public CustomChatAdapter(Context context, ArrayList<Chat> presidents) {
super(context, R.layout.chatrowlayout, presidents);
this.context = context;
this.list = presidents;
}
static class ViewContainer { public ImageView imageView; public TextView txtMsg; public ImageView imageView1; public TextView txtTime; }
@Override
public View getView(int position, View view, ViewGroup parent) {
ViewContainer viewContainer;
View rowView = view;
//---print the index of the row to examine---
//Log.d("ListView1",String.valueOf(position));
//if(rowView == null) {
viewContainer = new ViewContainer();
LayoutInflater inflater = LayoutInflater.from(context);
rowView= inflater.inflate(R.layout.chatrowlayout, null, true);
viewContainer.txtMsg = (TextView) rowView.findViewById(R.id.msgText);
viewContainer.imageView = (ImageView) rowView.findViewById(R.id.icon1);
viewContainer.imageView1 = (ImageView) rowView.findViewById(R.id.icon2);
viewContainer.txtTime = (TextView) rowView.findViewById(R.id.timeText);
rowView.setTag(viewContainer);
/*} else {
viewContainer = (ViewContainer) rowView.getTag();
}*/
viewContainer.txtMsg.setText(list.get(position).getMsg());
viewContainer.txtTime.setText(list.get(position).getMsg_time());
if(list.get(position).getS_id().equals("1")) {
viewContainer.imageView.setImageBitmap(BitmapFactory.decodeFile(new Info().p + File.separator+ "image_" + list.get(position).getPic_id1() + ".jpeg" ));
viewContainer.imageView1.setAlpha(0);
viewContainer.txtMsg.setGravity(Gravity.LEFT);
} else {
viewContainer.imageView1.setImageBitmap(BitmapFactory.decodeFile(new Info().p + File.separator+ "image_" + list.get(position).getPic_id1() + ".jpeg" ));
viewContainer.imageView.setAlpha(0);
viewContainer.txtMsg.setGravity(Gravity.RIGHT);
}
return rowView;
}
}
答案 0 :(得分:1)
removeGlobalOnLayoutListerner
,addOnGlobalLayout
适用于16之前。
答案 1 :(得分:0)
OnCreate太早了。
您应该等待布局膨胀。 onResume中的平滑滚动应该可以解决问题:
@Override
protected void onResume() {
mListView.smoothScrollToPosition(adapter.getCount());
}