2016-12-20 37 views
1

活動包含編輯文本,提交按鈕和從服務器獲取其數據的列表視圖。當我點擊,編輯文本,它滾動至底部,它應該是:添加項目後,ListView不會滾動到底部

editText.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 

      getWindow().setSoftInputMode(
        WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); 
      scrollMyListViewToBottom(); 
      Log.i("yoyo","ListSize Before: " + size); 
    } 
}); 

然而,當我點擊提交按鈕,列表視圖更新後,當notifydatasetchanged();被調用,它不滾動到底部。如果您想知道CommentQuery();會發生什麼,我可以提供更多代碼。

submitComment.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      commentItem = new ParseObject("CommentItem"); 
      commentItem.put("parentUser", feedUserName); 
      commentItem.put("parentFeed", feedItem); 
      // commentItem.put("parentObjectId", objectId); 
      commentItem.put("commentText", String.valueOf(commentText.getText())); 
      commentItem.put("username", ParseUser.getCurrentUser().getUsername()); 
      commentItem.put("country", ParseUser.getCurrentUser().getInt("country")); 
      commentItem.put((ParseUser.getCurrentUser().getUsername() + "globalPoints"), 0); 
      commentItem.put("parentObjectId", objectId); 
      replies +=1; 


      commentItem.saveInBackground(new SaveCallback() { 
       @Override 
       public void done(ParseException e) { 
        if (e == null) { 
         arrayCommentList.clear(); 
         comments.clearCachedResult(); 
         CommentQuery(); 
        customCommentListViewAdapter.notifyDataSetChanged(); 

         Log.i("yoyo","ListSize After: " + size); //size changes like it is supposed to 

         scrollMyListViewToBottom(); //doesn't do anything 
        } 
       } 
      }); 

     } 
    }); 

scrollMyListViewToBottom()函數:

public void scrollMyListViewToBottom() { 
    commentList.post(new Runnable() { 
     @Override 
     public void run() { 
      // Select the last row so it will scroll into view... 

      commentList.setSelection(size); 
     } 
    }); 

} 

CommentQuery功能:

public void CommentQuery(){ 

    comments = new ParseQuery<>("CommentItem"); 
    comments.setLimit(99); 
    comments.whereEqualTo("parentObjectId", objectId); 
    comments.findInBackground(new FindCallback<ParseObject>() { 
     @Override 
     public void done(List<ParseObject> mobjects, ParseException e) { 

      if(e == null){ 

       for(ParseObject object : mobjects){ 

        parseObs = mobjects; 

        size = parseObs.getsize(); 

        commentData = new HashMap<>(); 
        commentData.put("username", object.getString("username")); 
        commentData.put("feed", object.getString("commentText")); 
        commentData.put("likes", String.valueOf(object.getInt("likes"))); 
        commentData.put("country", String.valueOf(object.getInt("country"))); 
        commentData.put("replies", String.valueOf(0)); 
        commentData.put("global", String.valueOf(object.getInt(usernameText+"globalPoints"))); 

        arrayCommentList.add(commentData); 

       } 
       customCommentListViewAdapter = new CustomCommentListViewAdapter(getApplicationContext(), arrayCommentList); 
       commentList.setAdapter(customCommentListViewAdapter); 
      } 

     } 
    }); 

} 
+0

您似乎不會增加尺寸,所以我希望滾動保持不變。 –

+0

如果你讀了日誌,它說'尺寸'就像它應該改變一樣。 @MattClark – grant

+0

如果您發佈了日誌,或許我可以閱讀它並且知道它;) –

回答

1

首先 - 不要這樣做。 parseObs = mobjects;無論您剛使用parseObs設置的適配器是否丟失了對該列表的引用。

你反而需要這樣做。

parseObs.clear(); 
parseObs.addAll(mobjects); 

接下來,size = parseObs.getsize();完全不必是內環路。在迭代時,列表的大小不應改變。


最後,這已經是你想要的解析回調

customCommentListViewAdapter = new CustomCommentListViewAdapter(getApplicationContext(), arrayCommentList); 
commentList.setAdapter(customCommentListViewAdapter); 

你並不需要再次通知適配器裏面是什麼。

CommentQuery(); 
// customCommentListViewAdapter.notifyDataSetChanged(); // Not necessary 

然後,CommentQuery異步

arrayCommentList.clear(); // List is now empty 
... 
CommentQuery(); // doing stuff ... in the background 
.. 
scrollMyListViewToBottom(); // there isn't anything to scroll to yet! 

基本上,解決方案是scrollMyListViewToBottom()CommentQuery()done { }方法塊,這將是ListView中包含的數據後內。

而且,正如我在評論中所說的,size作爲一個變量是沒有必要的。只需使用customCommentListViewAdap‌​ter.getCount()

+0

將'scrollMyListViewToBottom()'放入'done {}'是需要完成的!另外,我刪除了'size'並且還原爲'customCommentListViewAdap ter.getCount()'。奇蹟般有效。 – grant

+0

很高興聽到。我不確定你在看到你的評論時發現「尺寸變化就像它應該」 –

+0

日誌告訴我列表更新後列表視圖的大小增加 – grant

相關問題