在选择listitem时,如何避免listitem中的图像按钮聚焦?

时间:2012-07-18 09:15:48

标签: android

帮助我避免不必要的焦点。我禁用了焦点,但我不知道它是如何集中注意力的。我怀疑这是由于可扩展的listView或customadapter。提前谢谢。

    ListDoctorsActivity.java

    public class ListDoctorsActivity extends ListActivity implements
        OnItemClickListener, OnClickListener {
    private ImageButton addUpdateButton;

    private DoctorAdapter doctorAdapter;
    private SimpleSectionAdapter<Doctor> sectionAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.at_list_doctors);

        // UI References
        addUpdateButton = (ImageButton) findViewById(R.id.add_update);

        // Set listener
        addUpdateButton.setOnClickListener(this);
        getListView().setOnItemClickListener(this);
    }

    private class DoctorAdapter extends ArrayAdapter<Doctor> {
        // Attributes
        private List<Doctor> doctors;
        private int expandedViewPosition = -1;

        public DoctorAdapter(Context context, int textViewResourceId,
                List<Doctor> doctors) {
            super(context, textViewResourceId, doctors);
            this.doctors = doctors;

        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = convertView;
            Holder holder = null;

            if (view == null) {
                view = View.inflate(ListDoctorsActivity.this,
                        R.layout.at_list_item_doctor, null);

        holder = new Holder();
        holder.doctorNameTextView = (TextView) view
                        .findViewById(R.id.doctor_name);
        holder.specializationTextView = (TextView) view
                        .findViewById(R.id.specialization);
        holder.editImageButton = (ImageButton) view
                        .findViewById(R.id.edit);
        holder.expandableView =       view.findViewById(R.id.expandable_view);
        holder.callButton = (Button) view.findViewById(R.id.call);
        holder.emailButton = (Button) view.findViewById(R.id.email);
        holder.appointmentsButton = (Button) view
                        .findViewById(R.id.appointments);

                // Set properties
                holder.doctorNameTextView.setTypeface(headerTitleFont);

                view.setTag(holder);
            } else {
                holder = (Holder) view.getTag();
            }

            // Set properties
            final Doctor doctor = doctors.get(position);
            holder.doctorNameTextView.setText(doctor.getName());
            holder.specializationTextView.setText(doctor.getSpecialization());

            // Show / Hide the expandable view
            int visibility = position == expandedViewPosition ? View.VISIBLE
                    : View.GONE;
            holder.expandableView.setVisibility(visibility);
            // Event listeners
            OnClickListener clickListener = new OnClickListener() {

                @Override
                public void onClick(View v) {
                    switch (v.getId()) {
                    case R.id.edit:
                        edit(doctor);
                        break;
                    case R.id.call:
                        call(doctor);
                        break;
                    case R.id.email:
                        email(doctor);
                        break;
                    case R.id.appointments:
                        showAppointments(doctor);
                        break;
                    }
                }
            };

            holder.editImageButton.setOnClickListener(clickListener);
            holder.callButton.setOnClickListener(clickListener);
            holder.emailButton.setOnClickListener(clickListener);
            holder.appointmentsButton.setOnClickListener(clickListener);

            // Disable focus
            disableFocus(holder);

            return view;
        }

        private void disableFocus(Holder holder) {
            holder.editImageButton.setFocusable(false);
            holder.callButton.setFocusable(false);
            holder.emailButton.setFocusable(false);
            holder.appointmentsButton.setFocusable(false);
        }

        public void toggleExpandedView(int position) {
            expandedViewPosition = expandedViewPosition == position ? -1
                    : position;
            notifyDataSetChanged();
        }
    }

    static class Holder {
        public TextView doctorNameTextView;
        public TextView specializationTextView;
        public ImageButton editImageButton;
        public View expandableView;
        public Button callButton;
        public Button emailButton;
        public Button appointmentsButton;
    }

    class DoctorSectionizer implements Sectionizer<Doctor> {

        @Override
        public String getSectionTitleForItem(Doctor doctor) {
            return doctor.getName().toUpperCase().substring(0, 1);
        }
    }



    @Override
    protected void onStart() {
        super.onStart();
        // Model
        List<Doctor> doctors = Sorting(OrmanHelper.getDoctors());

        // Adapter
        doctorAdapter = new DoctorAdapter(this, R.layout.at_list_item_doctor,
                doctors);
        sectionAdapter = new SimpleSectionAdapter<Doctor>(this, doctorAdapter,
        R.layout.cmn_list_section, R.id.title, new   DoctorSectionizer());
        getListView().setAdapter(sectionAdapter);

    }

    @Override
    public void onItemClick(AdapterView<?> parentView, View view, int position,
            long id) {
        // Toggle list items
        int index = sectionAdapter.getIndexForPosition(position);
        doctorAdapter.toggleExpandedView(index);
        sectionAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.add_update:
            Intent intent = new Intent(this, AddEditDoctorActivity.class);
            startActivity(intent);
            break;
        }
    }
    }

at_list_item_doctor.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:focusable="false" >

    <TextView
        android:id="@+id/doctor_name"
        style="@style/summary_text"
        android:layout_marginLeft="14dp"
        android:paddingTop="10dp"
        android:text="@string/name" />

    <TextView
        android:id="@+id/specialization"
        style="@style/medium_text"
        android:layout_below="@+id/doctor_name"a
        android:layout_alignLeft="@id/doctor_name"
        android:paddingBottom="10dp"
        android:textColor="@color/gray_darkest"
        android:text="@string/specialization" />

    <ImageButton
        android:id="@+id/edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@id/doctor_name"
        android:layout_alignBottom="@id/specialization"
        android:focusable="false"
        android:background="@android:color/transparent"
        android:src="@drawable/edit" />

    <include
        android:id="@+id/expandable_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/specialization"
        android:visibility="gone"
        layout="@layout/at_list_item_expandable_doctor" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

最佳创意只是向列表项行中的任何一个Textview请求焦点,

类似的东西,

<TextView
        android:id="@+id/doctor_name"
        style="@style/summary_text"
        android:layout_marginLeft="14dp"
        android:paddingTop="10dp"
        android:text="@string/name" 

<requestFocus />
/>

OR以编程方式在Adapter类的getView()

holder.doctorNameTextView.requestFocus(); // Code for understanding

试试这个,让我知道它是否有效。