2016-12-20 39 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

相关问题