listview中的onItemClickListener不起作用

时间:2013-02-27 18:44:01

标签: android listview android-arrayadapter onitemclicklistener

我正在尝试使用自定义的arrayAdapter实现listView。我可以看到我的列表但是,当我尝试选择列表的选项时,我实现的方法onItemClick不起作用。我在StackOverflow中看到过类似的问题,但是所提供的解决方案都不适用于我...我尝试使用android:descendantFocusability="blocksDescendants"android: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);

            }

        }
    });


}

}

任何人都可以帮助我吗?

提前谢谢!

1 个答案:

答案 0 :(得分:0)

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

这两行必须删除,以便为你的onItemClickekdListener调用:

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

您的item_elements.xml中的其他android:clickableandroid:focusable属性是不必要的,但它们不会导致您的问题。