1

我正在尝试使用自定义的 arrayAdapter 来实现 listView。我可以看到我的列表,但是当我尝试选择列表中的一个选项时,我实现的方法 onItemClick 不起作用。我在 StackOverflow 中看到过类似的问题,但是提供的解决方案都不适合我……我已经尝试过使用android:descendantFocusability="blocksDescendants", withandroid:clickable="false"android:focusable="false"用于列表中每一行的元素,并且我也尝试过制作 MainActivity。 java实现onItemClick,但它不起作用。我知道我的代码一定有错误,但我找不到它......

这是我的代码:

guia_general_list.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E6CEC1"
android:orientation="horizontal" >

<ListView
    android:id="@+id/lstOpciones"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="top|center"
    android:background="#E6CEC1" />

</LinearLayout>

item_elements.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layoutGeneral"
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="@drawable/menu_selector_bar"
android:clickable="true"
android:focusable="true"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/ivQueVerElement"
    android:layout_width="65dp"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical"
    android:paddingBottom="1dp"
    android:paddingLeft="1dp"
    android:paddingTop="1dp"
    android:scaleType="fitXY" />

<TextView
    android:id="@+id/txtOptionName"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_gravity="center_vertical"
    android:layout_weight="1"
    android:gravity="center_vertical"
    android:paddingLeft="5dp"
    android:textColor="#FFFFFF"
    android:textStyle="bold" />

<ImageView
    android:id="@+id/ivVerOpcion"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_gravity="right|center_vertical"
    android:src="@drawable/navigation_next_item" />

</LinearLayout>

ElementsAdapter.java

public class ElementsAdapter extends ArrayAdapter<OptionList>{

int layoutResourceId;
Context context;
OptionList data[] = null;

public ElementsAdapter(Context context, int layoutResourceId, OptionList[] data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

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

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

        holder = new OptionListHolder();
        holder.imgIcon = (ImageView)row.findViewById(R.id.ivQueVerElement);
        holder.txtTitle = (TextView)row.findViewById(R.id.txtOptionName);

        row.setTag(holder);
    }else{
        holder = (OptionListHolder)row.getTag();
    }

    OptionList option = data[position];
    holder.txtTitle.setText(option.getOptionName());
    holder.imgIcon.setImageResource(option.getImageName());

    LinearLayout layoutItem = (LinearLayout) row.findViewById(R.id.layoutGeneral);


    return row;
}

static class OptionListHolder{
    ImageView imgIcon;
    TextView txtTitle;
}

public int getCount() {
    return data.length;
}


public OptionList getItem(int position) {
    return ((OptionList)data[position]);
}

public long getItemId(int position) {
    return position;
}


}

MainActivity.java:

public class MainActivity extends Activity{

ArrayAdapter<String> adaptador;

public MainActivity() {
    // TODO Auto-generated constructor stub
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.guia_general_list);

}

@Override
protected void onResume() {
        super.onResume();

    OptionList optionListData[] = new OptionList[]{
            new OptionList("Option1",  R.drawable.image1),
            new OptionList("Option2", R.drawable.image2),
            new OptionList("Option3", R.drawable.image3)
    };

    ElementsAdapter adapter = new ElementsAdapter(this, R.layout.item_elements, optionListData);        

    ListView lstOptions = (ListView)findViewById(R.id.lstOpciones);

    lstOptions.setAdapter(adapter);

lstOptions.setOnItemClickListener(new OnItemClickListener(){
        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id){

            Toast.makeText(getApplicationContext(),   "ENTRA!", Toast.LENGTH_LONG).show();
            Log.d("xxx","AAA: Position: "+position);

            }

        }
    });


}

}

谁能帮我?

先感谢您!

4

1 回答 1

0

您的代码看起来不错,除了 item_elements.xml。那里的视图是可点击的和/或可聚焦的。

这两行,必须删除部分 to 才能让您的 onItemClickekdListener 被调用:

android:clickable="true"
android:focusable="true"
android:descendantFocusability="blocksDescendants"

整个 item_elements.xml 中的 otherandroid:clickableandroid:focusable属性是不必要的,但它们不应导致您的问题。

于 2013-02-27T19:05:18.990 回答