使listview项目可单击并可选择

时间:2013-10-30 02:07:57

标签: android listview android-listview

是否可以使listview项目既可点击又可选择?

如果我设定 android:textIsSelectable =“true”

我的onitemclicklistener不起作用。或者是否可以在不使用textIsSelectable的情况下突出显示listview中的textview?因为我使用下面的xml代码在我的textview

中创建突出显示
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Selected --> 
  <item 
    android:state_focused="true" 
    android:state_selected="false" 
    android:drawable="@drawable/focused"/> 
    <item android:drawable="@color/black" /> <!-- default -->
</selector> 

2 个答案:

答案 0 :(得分:0)

我认为您需要自定义列表视图。

<ScollView><Linearlayout android:id="@+id/listview">  the as a listview of rootView</LinearLayout></ScollView>

LinearLayout listView =(LinearLayout)findViewById(R.id.listview); 将子项视图添加到listView中。 所以现在你可以在子项视图上添加选择器和clicklistener。

答案 1 :(得分:0)

这是我的wertherApp代码:

public class MainListAdapter extends BaseAdapter :

   public void change(ArrayList<MainListInfo> mainWether) {

    if (mainWether == null)

        this.mainWether = new ArrayList<MainListInfo>();

    else

        this.mainWether = mainWether;

    this.notifyDataSetChanged();

}

    public void setcurtentitem(int curentitem) {

    this.curentitem = curentitem;

    this.notifyDataSetChanged();

}

 public class MainListAdapter extends BaseAdapter {

  ……
  if (position == curentitem) {

        convertView.setBackgroundResource(R.drawable.bg_01_down);

    } else {

        convertView.setBackgroundResource(R.drawable.bg_01_up);

    }

 ……


 }

然后在活动中:

listview.setOnItemClickListener(new OnItemClickListener() {

        @Override

        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {

            adapter.setcurtentitem(arg2);

        }

    });