检测BaseAdapter.notifyDataSetChanged()何时完成

时间:2012-08-22 14:41:54

标签: android baseadapter

我想知道BaseAdapter.notifyDataSetChanged()什么时候结束,然后做出声音。我的代码:

BaseAdapter.notifyDataSetChanged();  
mScroller.startScroll(oldX, 0, dx, 0);

mScrollernotifyDataSetChanged()完成之前滚动。我希望在mScroller完成后oldX滚动到notifyDataSetChanged()

请帮帮我,谢谢。

4 个答案:

答案 0 :(得分:3)

BaseAdapter除了扩展之外,还支持注册DataSetObserver,您可以在其中监听onChanged事件。

notifyDataSetChanged方法将引发此事件。

adapter.registerDataSetObserver(new DataSetObserver() {
    @Override
    public void onChanged() {
        // ...
    }
});

...

adapter.notifyDataSetChanged();

答案 1 :(得分:1)

我有同样的问题,试试这个对我来说很完美。 您正在覆盖方法notifyDataSetChanged()。您可以在Adapter类中执行此操作。只需复制我发布的代码,然后用我们需要的东西替换我setSelection()的行。

@Override
public void notifyDataSetChanged()
{
    super.notifyDataSetChanged();

    parentGlobal.setSelection(this.getCount() - 1);  //This is how we start Activity fully scrolled to bottom
}

有了这个说,如果你想知道,“parentGlobal”是我在我的Activity中设置适配器的ListView。在Adapter的构造函数中,我传入了ListView,然后使其成为“全局”。如果“global”不是Java术语我很抱歉,我来自C ++世界。

//进入活动:

lv_main = (ListView) findViewById(R.id.listView_conversation);
    adapter_main = new Adapter_Conversation(ConversationActivity.this, R.layout.layout_list_conversation,
            list_main_UI, lv_main, main_list_item_margin_px);
    lv_main.setAdapter(adapter_main);

//进入适配器

public Adapter_Conversation(AppCompatActivity activity, @LayoutRes int resource,
                            ArrayList<Adapter_GetSet_Conversation> list_conversation, ListView lv_main,
                            int item_margin_px)

答案 2 :(得分:0)

我遇到了同样的问题,我最终还是这样做了 -

 BaseAdapter.notifyDataSetChanged();  
 new Handler().postDelayed(new Runnable() {
      @Override
              public void run() {
                 mScroller.startScroll(oldX, 0, dx, 0);
                //write scroll code here
               }
       },50);  //scroll after 50ms

答案 3 :(得分:0)

这很晚了,但可能对以后再来的人有帮助。

如果您熟悉RxJava2Completable,则可以检测notifyDataSetChanged()或UI上的任何操作是否完成。

        Completable.fromAction {
            // notifyDataSetChanged() here
        }
                .subscribe(object : DisposableCompletableObserver() {
                    override fun onComplete() {
                        recyclerView.scrollTo(position)
                    }

                    override fun onError(e: Throwable) {
                        // handle error here
                    }
                })
相关问题