Android列表视图项目没有获得焦点

时间:2014-10-30 22:47:55

标签: android listview

我的listview中的项目没有得到焦点,我的onFocusChanged没有工作,所以我创建了一个日志语句来记录焦点,我总是为v.isFocused()的结果返回“false”。

onClick方法运行正常。

有什么想法吗?

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View row = convertView;
    final ExerciseListHolder holder;

    if (row == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new ExerciseListHolder();
        holder.textView = (TextView) row.findViewById(R.id.exercise_list_text_view);
        holder.imageView = (ImageView) row.findViewById(R.id.trash_button);
        holder.editText = (EditText) row.findViewById(R.id.exercise_list_edit_text_view);
        holder.deleteButton = (Button) row.findViewById(R.id.delete_button);

        row.setTag(holder);

    } else {
        holder = (ExerciseListHolder) row.getTag();
    }

    final Exercise exercise = data.get(position);
    holder.textView.setText(exercise.getName());
    holder.imageView.setVisibility(View.GONE);
    holder.editText.setVisibility(View.GONE);
    holder.deleteButton.setVisibility(View.GONE);

    holder.imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            holder.imageView.setVisibility(View.GONE);
            holder.deleteButton.setVisibility(View.VISIBLE);
            holder.deleteButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Database db = new Database(context);
                            db.deleteExercise(exercise);
                            notifyDataSetChanged();

                }
            });

        }
    });
    row.setFocusable(true);

    row.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            v.requestFocus();

            Log.d("focused?", Boolean.toString(v.isFocusable()));
            Log.d("focused?", Boolean.toString(v.isFocused()));

            ExerciseListHolder holder = (ExerciseListHolder) v.getTag();

            holder.editText.setText(holder.textView.getText());
            holder.editText.setSelection(holder.editText.getText().length());

            holder.imageView.setVisibility(View.VISIBLE);
            holder.textView.setVisibility(View.GONE);
            holder.editText.setVisibility(View.VISIBLE);

        }
    });


    return row;
}

1 个答案:

答案 0 :(得分:1)

也许添加到listview android:descendantFocusability =" afterDescendants",可以帮到你。更多文档:here