其他分享
首页 > 其他分享> > Android Gallery视图侦听器会增加延迟吗?

Android Gallery视图侦听器会增加延迟吗?

作者:互联网

我已经实现了gallery和listview.
画廊有图像,当我单击使用setOnItemSelectedListener Listview的图像时,将更新有关选定图像的信息.

问题:当我快速浏览图库时,它会快速更新我的列表视图.但是据我说,listview应该等待图库停止在任何一张图像上移动,然后更新其内容.

任何人都知道该怎么做,或者在更新listview之间增加时间?请帮忙..

解决方法:

您可以改为尝试setOnItemClickListener.仅当您实际“点击”图像时才触发它,而不仅仅是选择它.

    gallery.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            // Do what you want
        }
    });

更新:

如果要延迟动作,而不是onItemClickListener实现,则有(至少)两个选择.一种是使用超时(Thread.sleep())运行单独的线程,完成后,可以使用Handler更新活动状态.另一个选择是使用AsyncTask,这在技术上是相同的方法,它仅将Runnable任务和Handler包装在一个内部类中.最简单的实现可能是这样的:

    ListUpdater listUpdater; 

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {

    // We have to run this thread to add delay before updating activity views
    if (listUpdater != null) { // Cancel previous task
        listUpdater.cancel(true);
    }
    if (listUpdater == null || position != listUpdater.getPosition()) {
        listUpdater = null;
        listUpdater = new ListUpdater(position);

        listUpdater.execute();
    }
}

class ListUpdater extends AsyncTask<String, String, String> {
    private int position;

    public int getPosition() {
        return position;
    }

    public ListUpdater(int position) {
        this.position = position;
    }

    @Override
    protected String doInBackground(String... params) { 
        try {
            Thread.sleep(600); // set delay time here
        } catch (InterruptedException e) {
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        if (!isCancelled()) {
            // If another thread is not running, 
            //  that is if another item was not selected,
            //  update the activity 

            // Add code here to update the activity 
            // This method works in UI Thread, so you don't need to use Handler
            // For example:
            Toast.makeText(YourActivity.this, "" + position, Toast.LENGTH_SHORT).show();
        }
        super.onPostExecute(result);
    }
}

这是实施延迟的一个例子.不一定要精确满足您的需求,但是您可以从中获得一些有关如何实现的想法.

标签:android-gallery,android
来源: https://codeday.me/bug/20191202/2086241.html