0

单击 ListView 中的 ListItem 没有响应,但是 longClickListener 正在响应。当用户单击列表视图项时,我想开始一个新活动。

我尝试了什么:

android:clickable="false" // and true - not working
android:descendantFocusability="blocksDescendants" // no change with or without it

为什么 onItemClickListener 没有响应?

MainActivity.xml

public class MainActivity extends Activity implements OnClickListener
{
    private ListView remindersList;
    private ImageView createEvent; 
    private ImageView deleteEvent;
    private TextView noRemindersSet;
    private String[] values = {"One", "Two", "Three"};  
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    createEvent = (ImageView) findViewById(R.id.imageView2);
    deleteEvent = (ImageView) findViewById(R.id.imageView1);
    createEvent.setOnClickListener(this);
    deleteEvent.setOnClickListener(this);
    remindersList = (ListView) findViewById(android.R.id.list);

    ListViewArrayAdapter adapter = new ListViewArrayAdapter(MainActivity.this, 
             android.R.id.list, values);
    //setting the adapter
    remindersList.setAdapter(adapter);
    remindersList.setTextFilterEnabled(true);
    remindersList.setOnItemClickListener(new OnItemClickListener() {

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

            Log.d("s click","id: ");
        }});

    remindersList.setLongClickable(true);
    remindersList.setOnItemLongClickListener(new OnItemLongClickListener() {
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int pos, long id)
        {
            Log.d("long clicked","id: " + id);
            return true;
        }
    });
 }
}

阵列适配器

public class ListViewArrayAdapter extends ArrayAdapter<String>
{

private final Context context;
String[] values;
private TextView textView;

public ListViewArrayAdapter(Context context, int resource, String[] values) {
    super(context, resource, values);
    // TODO Auto-generated constructor stub
    this.context = context;
    this.values = values;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
    LayoutInflater inflater = (LayoutInflater) context
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.list_item, parent, false);
    textView = (TextView) rowView.findViewById(R.id.textView1);
    textView.setText(values[position]);
  return rowView;
    }

}

List_View.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#01B0F1"
    android:longClickable="true"
    android:paddingBottom="2dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="2dp"
    android:text="@string/textview"
    android:textColor="#FFFFFF"
    android:textSize="25sp" />

</LinearLayout>
4

4 回答 4

2

在您的适配器上更改此方法:

@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.list_item, parent, false);
    rowView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Log.d("s click","id: ");
                Intent i = new Intent(context, yourActivity.class);  //change your activity to the one you want to launch
                context.startActivity(i); //you already had your context defined above so just use it to start the activity

            }
        });

    textView = (TextView) rowView.findViewById(R.id.textView1);
    textView.setText(values[position]);

    return rowView;
    }

}

并删除你的remindersList.setOnItemClickListener. 这将工作;)

于 2013-10-25T10:22:29.010 回答
2

设置文本视图

android:focusableInTouchMode="false"
android:focusable="false"

这将使您的列表具有焦点,否则焦点移动到 TextView 并且您的列表视图没有被点击。

- 带有 TextView 的 ListView 示例

于 2013-10-25T09:40:06.217 回答
1

尝试删除 MainActivtiy 中的 implements 语句。

于 2013-10-25T09:46:29.910 回答
0

尝试这个..

remindersList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View v, int pos,
                long id) {

            Log.d("s click","pos: "+ pos);
             Log.d("s click","id: "+ id);
        }});
于 2013-10-25T09:34:17.783 回答