该活动包含一个编辑文本,提交按钮和一个从服务器获取其数据的列表视图。当我单击“编辑文本”时,它会按预期滚动到底部:

  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);
            }

        }
    });

}

最佳答案

首先-不要这样做。 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()

09-04 05:58