android - 为什么在自定义 ArrayAdapter 中实例化新的自定义处理程序有效?

标签 android listview android-arrayadapter

我已经使用它一段时间了,但我不确定我是在示例中发现了这种技术,还是只是尝试了很多东西直到它最终起作用。我的问题是这行代码如何,

new ObjectInterfaceHandler(position, o, v);

实际到达此 View 监听来自 ListReadyObject 的回调的位置。

package com.scs.stuff;

import java.util.ArrayList;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ListReadyObjectAdapter extends ArrayAdapter<ListReadyObject> {

    public ListReadyObjectAdapter(Context context, int textViewResourceId,
            ArrayList<ListReadyObject> lro) {
        super(context, textViewResourceId, lro);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LinearLayout v;
        v = new LinearLayout(getContext());
        LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        vi.inflate(R.layout.list_row, v, true);

        ListReadyObject o = getItem(position);

        if (o != null) {

            TextView tt = (TextView) v.findViewById(R.id.toptext);
            TextView bt = (TextView) v.findViewById(R.id.bottomtext);

            if (tt != null) {
                tt.setText(o.getDisplayText());
            }

            if (o.isLiving()) {
                if (bt != null) {
                    // set default text to be shown
                    // until the status thread completes
                    bt.setText("---");
                }
                // start a background thread to update Display State
                o.updateStatus();
                // why does this work
                new ObjectInterfaceHandler(position, o, v);

            } else {
                if (bt != null) {
                    bt.setText("Not Living");
                }
            }
        }
        return v;
    }

    // provides a way for the Object to call back to the list without
    // blocking the UI
    public class ObjectInterfaceHandler implements ListReadyObjectStatusListener {
        int position;
        ListReadyObject o;
        View v;

        private final Handler handler = new Handler();

        public ObjectInterfaceHandler(int position, ListReadyObject o, View v) {
            this.position = position;
            this.o = o;
            this.v = v;
            // register to observe an update from the Object
            o.registerObserver(this);
        }
        @Override
        public void objectInterfaceUpdate() {
            // called from the Object's observer pattern
            handler.post(updateBottomText);
        }
        // runnable to put the update on the UI Thread
        private Runnable updateBottomText = new Runnable() {
            @Override
            public void run() {
                TextView bt = (TextView) v.findViewById(R.id.bottomtext);
                if (bt != null) {
                    bt.setText(o.getStatusText());
                }
            }
        };
    }
}

ListReadyObject 接口(interface):

package com.scs.stuff;

public interface ListReadyObject {

    public void registerObserver(ListReadyObjectStatusListener o);

    public void removeObserver(ListReadyObjectStatusListener o);

    public void notifyObservers();

    public String getDisplayText();

    public String getStatusText();

    public boolean isLiving();

    public void updateStatus();

}

ListReadyObjectStatusListener 接口(interface):

package com.scs.stuff;

public interface ListReadyObjectStatusListener {

    public void objectInterfaceUpdate();

}

最佳答案

它不会(直接)让 View 监听回调。

这里您看到 ListReadyObject 被传递到 ObjectInterfaceHandler 的构造函数中:

ListReadyObject o = getItem(position);
...
new ObjectInterfaceHandler(position, o, v);

在该构造函数中,您会发现:

o.registerObserver(this);

所以 ObjectInterfaceHandler 实际上是监听 ListReadyObject 的。然后当它收到更新通知时:

@Override
public void objectInterfaceUpdate() {
    // called from the Object's observer pattern
    handler.post(updateBottomText);
}

它向自己的处理程序发送消息以调用更新 View 的 Runnable:

private Runnable updateBottomText = new Runnable() {
    @Override
    public void run() {
        TextView bt = (TextView) v.findViewById(R.id.bottomtext);
        if (bt != null) {
            bt.setText(o.getStatusText());
        }
    }
};

这是一个非常常见的模式,称为 Observer Pattern .

关于android - 为什么在自定义 ArrayAdapter 中实例化新的自定义处理程序有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8994064/

相关文章:

android - 无法在android中将矢量可绘制对象转换为位图可绘制对象

java - USB 通信,Monodroid/Android 中的端点

android - android中如何以表格形式显示ListView

c# - 如何在 ListView 中创建可点击的链接?

android - 模拟器总是启动而不是设备

java - android studio,无法识别按钮对象,

java - ArrayAdapter刷新数据

android - 具有 20 个项目的 ListView 滚动时滞后

android - OnitemClickListener 不工作

android - ArrayAdapter 错误