我无法弄清楚为什么setOnItemClickListener不起作用。我有自定义ListItem布局的ListView, - 我想在用户点击行时启动其他活动。
我看到很多关于这个问题的话题,但没有任何帮助。
屏幕:
activity_main.xml中:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/notes_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/addNote">
</ListView>
<TextView
android:id="@+id/empty_list_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/empty_notes"
android:visibility="gone"
android:gravity="center"
android:layout_above="@+id/addNote"
/>
<Button
android:id="@+id/addNote"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/add_note"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
list_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >
<!-- Note text-->
<TextView
android:id="@+id/noteText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#040404"
android:typeface="sans"
android:textSize="15dip"
android:textStyle="bold" />
<!-- Note createdAt -->
<TextView
android:id="@+id/noteCreatedAt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/noteText"
android:textColor="#343434"
android:textSize="10dip"
android:layout_marginTop="1dip" />
<!-- Note complete -->
<Button
android:id="@+id/btnCompleteNote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="Complete" />
</RelativeLayout>
RNoteAdapter.java:
public class RNoteAdapter extends ArrayAdapter<RNote> {
private Context context;
private int resource;
RNote[] data;
private RNoteRepository noteRepository;
public RNoteAdapter(Context context, int resource, RNote[] data) {
super(context, resource, data);
this.context = context;
this.resource = resource;
this.data = data;
this.noteRepository = new RNoteRepository(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
RNoteHolder holder = null;
if(row == null) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(resource, parent, false);
holder = new RNoteHolder();
holder.noteText = (TextView)row.findViewById(R.id.noteText);
holder.noteCreatedAt = (TextView)row.findViewById(R.id.noteCreatedAt);
holder.btnCompleteNote = (Button)row.findViewById(R.id.btnCompleteNote);
row.setTag(holder);
} else {
holder = (RNoteHolder)row.getTag();
}
final RNote note = data[position];
if(note.IsCompleted) {
holder.noteText.setPaintFlags(holder.noteText.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
}
holder.noteText.setText(note.Text);
DateFormat df = new SimpleDateFormat("dd.MM.yyyy в HH:mm");
holder.noteCreatedAt.setText(df.format(note.CreatedAtUtc));
holder.btnCompleteNote.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
note.IsCompleted = true;
noteRepository.save(note);
RNoteAdapter.this.notifyDataSetChanged();
}
});
return row;
}
static class RNoteHolder {
TextView noteText;
TextView noteCreatedAt;
Button btnCompleteNote;
}
}
MainActivity.java
public class MainActivity extends ActionBarActivity {
private RNoteRepository _noteRepository = new RNoteRepository(this);
private Button addButton;
private ListView notesList;
RNoteAdapter adapter;
private void init() {
notesList = (ListView)findViewById(R.id.notes_list);
addButton = (Button)findViewById(R.id.addNote);
notesList.setEmptyView(findViewById(R.id.empty_list_item));
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showEditNoteActivity(0);
}
});
notesList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
showEditNoteActivity(id);
}
});
}
private void updateData() {
List<RNote> list = _noteRepository.getAll();
RNote[] array = list.toArray(new RNote[list.size()]);
adapter = new RNoteAdapter(this, R.layout.list_row, array);
notesList.setAdapter(adapter);
}
private void showEditNoteActivity(long noteId) {
Intent intent = new Intent(getApplicationContext(), EditNoteActivity.class);
intent.putExtra("noteId", noteId);
startActivityForResult(intent, 1);
}
private void showDialog(String text) {
Toast toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data == null) {
return;
}
String msg = data.getStringExtra("response");
showDialog(msg);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
@Override
protected void onResume() {
super.onResume();
updateData();
}
@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_main, 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);
}
}
答案 0 :(得分:1)
添加
android:descendantFocusability="blocksDescendants"
到ListView的行根。
getItemId
ArrayAdapter
的实现会返回位置,而不是RNote
的ID,因此您可能希望覆盖它以使其返回正确的信息
答案 1 :(得分:1)
将android:focusable="false"
android:focusableInTouchMode="false"
设置为listView项目中的所有可点击视图(如button或editText)