如何从android

时间:2015-10-31 05:47:43

标签: android android-listview focus listitem

我正在实现一个listview,当焦点超出该listitem时,它将保存一个listitem。我怎样才能做到这一点?我试图使用onFocusChangeListener,但它不起作用。有人有想法吗?下面是我在ListAdapter中的代码。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.listitem, parent, false);
    Spinner elem1 = (Spinner)rowView.findViewById(R.id.elem1);
    Spinner elem2 = (Spinner)rowView.findViewById(R.id.elem2);
    TextView elem3 = (TextView)rowView.findViewById(R.id.elem3);

    rowView.setOnFocusChangeListener(this);

    return rowView;
}

@Override
public void onFocusChange(View view, boolean hasFocus) {
    if(!hasFocus) {
        // todo : save current list item.
        Logger.debug(TAG, ">>> Focus out");
    }
}

1 个答案:

答案 0 :(得分:0)

一种方法是使用另一个标志,例如boolean focused;

然后:

@Override
public void onFocusChange(View view, boolean hasFocus) {
    if(hasFocus) {
        focused = true;
    }
    if(!hasFocus) {
        if (focused == true) {
            // todo : save current list item.
            Logger.debug(TAG, ">>> Focus out");
            focused = false;
        }
    }
}