2015-11-03 18 views
0

我有從數據庫中填充的listview。Android ListView在就緒或初始化偵聽器上

listview需要一些時間(< 300ms)來填充列表。

如果我做一個smoothScrollToPositiononActivityCreated功能是什麼都不做。

在OnCreate中的功能,我不得不等待〜200毫秒之前,我可以打電話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

您可以發佈您的適配器代碼,以便我們可以有,爲什麼你的ListView是這麼長時間來加載一個想法。 – Nanoc

+0

你可能會開創了每次調用getView適配器的視圖,可以考慮使用視圖持有人模式或RecyclerView –

+0

@AlexGoja爲RecyclerView任何的例子嗎? –

回答

1

removeGlobalOnLayoutListerner預SDK 16,addOnGlobalLayout作品前16

+0

有任何替代的「removeGlobalOnLayoutListerner」 –

+0

是的,這是另類, removeGlobalOnLayoutListerner的SDK <16 removeOnGlobalLayoutListener的SDK> 16 –

0

OnCreate爲時尚早。

您應等待佈局被誇大。在的onResume平滑滾動應該做的伎倆:

@Override 
protected void onResume() { 
    mListView.smoothScrollToPosition(adapter.getCount()); 
} 
+0

: )用戶必須回到應用程序才能使用它? –

+0

開始/恢復活動時,始終調用onResume()。您可以在這裏閱讀更多關於活動生命週期的內容:http://www.vogella.com/tutorials/AndroidLifeCycle/article.html –