ListView项中的setOnFocusChangeListener

时间:2014-11-10 22:49:53

标签: android listview android-listview

我想要做的是在ActionBar中显示动作,如果选择了ListView的项目。 所以我创建: answer_item.xml:

   <LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:weightSum="1">

<EditText   android:id="@+id/simple_answer_item"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:singleLine="true"
            android:layout_weight="1.0"
            android:focusableInTouchMode="true"
            android:focusable="true"
            />
<ImageView
            android:id="@+id/file_loaded_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:visibility="gone"
            />
</LinearLayout>

和AnswerAdapter类:

public class AnswerAdapter extends ArrayAdapter<Answer>{
    private final LayoutInflater mInflater;
    private final Fragment parentFragment;
    public AnswerAdapter(Context context, int textViewResourceId,Fragment parentFragment) {
        super(context, textViewResourceId);
        mInflater = LayoutInflater.from(getContext());
        this.parentFragment = parentFragment;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        final ViewHolder holder;
        if(convertView == null) {
            convertView = mInflater.inflate(R.layout.answer_item, null);
            holder = new ViewHolder();
            holder.answer = (EditText) convertView.findViewById(R.id.simple_answer_item);
            holder.loadObjectImageView = (ImageView) convertView.findViewById(R.id.file_loaded_view);
            convertView.setTag(holder);

        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        final Answer answer = getItem(position);
        holder.answer.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (hasFocus) {
                    Toast.makeText(getContext(), "click " + position, Toast.LENGTH_SHORT).show();
                    if (parentFragment != null && parentFragment instanceof onAnswerListItemFocusChange)
                        ((onAnswerListItemFocusChange) parentFragment).onListItemFocusChange(true, answer);
                }
                else {
                    Toast.makeText(getContext(), "unclick "+ position, Toast.LENGTH_SHORT).show();
                    final int position = view.getId();
                    final EditText answerBody = (EditText) view;
                    getItem(position).setBody(answerBody.getText().toString());
                    if (parentFragment != null && parentFragment instanceof onAnswerListItemFocusChange)
                         ((onAnswerListItemFocusChange) parentFragment).onListItemFocusChange(false,answer);
                    view.clearFocus();
                }
            }
        });
        holder.answer.setText(answer.getBody());
        holder.answer.setId(position);


        return convertView;
    }


    private static class ViewHolder{
        public EditText answer;
        public ImageView loadObjectImageView;

    }
}

此外我有android:descendantFocusability =&#34; afterDescendants&#34;在ListView中:

        <ListView
            android:id="@+id/bad_answer_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:focusable="true"
            android:descendantFocusability="afterDescendants">
        </ListView>

因此,当我在ListView中运行应用程序并将焦点EditText放在exmaple 2的位置时,我希望只看到&#34;单击2&#34;但结果我得到了#34;点击2&#34; &#34;取消点击2&#34; &#34;点击0&#34; &#34; unclick0&#34; &#34; CLICK2&#34; 因为我认为在我的ActionBar中我只看到一个动作 但通常在EditText中不在ListView中工作正常,我看到3.

那么请告诉我我做错了什么?

0 个答案:

没有答案