我有自定义ListView。
这是来源:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:focusable="false"
android:clickable="false"
android:id="@+id/image_view"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/list_item_string"
android:layout_width="wrap_content"
android:focusable="false"
android:clickable="false"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:paddingLeft="8dp"
android:textSize="25sp"
android:textStyle="bold"
android:layout_marginLeft="90dp" />
<ImageButton
android:id="@+id/edit_btn"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:focusable="false"
android:clickable="false"
android:layout_marginRight="5dp"
android:src="@android:drawable/ic_menu_edit"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp" />
<ImageButton
android:id="@+id/delete_btn"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_toLeftOf="@id/edit_btn"
android:layout_centerVertical="true"
android:focusable="false"
android:clickable="false"
android:layout_marginRight="10dp"
android:src="@android:drawable/ic_menu_delete"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp" />
</RelativeLayout>
这是我的活动的XML代码,其中定义了listView:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@color/AliceBlue"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".IntroductionActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:fastScrollAlwaysVisible="false" />
</RelativeLayout>
这是我的活动的源代码,它使用ListView:
package com.bundas.tobesmart;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class IntroductionActivity extends Activity {
private ListView list;
private Activity activity = this;
private Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_introduction);
ArrayList<String> arrayList = new ArrayList<String>();
for(int i = 0; i < 50; ++i){
arrayList.add("Number " + (i+1));
}
list = (ListView) findViewById(R.id.listView);
CustomAdapter customAdapter = new CustomAdapter(arrayList, this, this);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(context, "Oteviram Cheat Activity", Toast.LENGTH_SHORT).show();
Log.d("Inside onItem atd..", "Mel se provest Toast");
}
});
Log.d("tag1", "testiiiiiiiiiiiiiiiiiiiiiing");
list.setAdapter(customAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_introduction, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
为什么OnItemClickListener不起作用?
答案 0 :(得分:2)
试试这个..设置焦点false,列表项可以正常工作
list = (ListView) findViewById(R.id.listView);
list.setItemsCanFocus(false);