因此,我为带有三个视图的TextView,ImageView和基本View创建了用于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"
android:id="@+id/AllNotesFragment">
<TextView
android:id="@+id/addNoteTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="30dp"
android:layout_marginTop="25dp"
android:textSize="18sp"
android:gravity="center"
android:fontFamily="@font/ukij_qolyazma"
android:text="+new"
/>
<ImageButton
android:id="@+id/deleteNoteImageButton"
android:layout_width="50dp"
android:layout_height="70dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="0dp"
android:layout_marginEnd="30dp"
android:textSize="24sp"
android:gravity="center"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:fontFamily="@font/ukij_qolyazma"
/>
<View
android:id="@+id/underlineView"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginHorizontal="30dp"
android:layout_marginTop="70dp"
android:background="@color/colorMainDark" />
<ListView
android:id="@+id/notesListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginTop="75dp"
android:layout_marginBottom="0dp">
</ListView>
我已经创建并实例化了ListView
notesListView = view.findViewById(R.id.notesListView);
并在其中塞满一堆我的笔记
notes = (ArrayList<Note>) db.getAllNotesForDay(NotesForDayActivity.getRememberDay(),
NotesForDayActivity.getRememberMonth(),
NotesForDayActivity.getRememberYear());
for (Note note : notes) {
noteTitles.add(note.getTitle());
}
NotesListAdapter adapter = new NotesListAdapter(((NotesForDayActivity) getActivity()).getContext(), notes);
notesListView.setAdapter(adapter);
使用我的自定义适配器:
public class NotesListAdapter extends BaseAdapter {
private static final String TAG = "NotesListAdapter";
public static Context context;
private RelativeLayout notesListRelativeLayout;
private TextView noteTitleTextView;
private ImageView tickImage;
private View underlineView;
private List<Note> notes;
// !
private View listItemsView;
public NotesListAdapter(Context context, List<Note> notes) {
this.notes = notes;
this.context = context;
}
@Override
public int getCount() {
return NotesForDayActivity.getCountNotes();
}
@Override
public Object getItem(int position) {
return notes.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
listItemsView = convertView;
if (listItemsView == null) {
listItemsView = LayoutInflater.from(NotesForDayActivity.context).inflate(R.layout.notes_list_layout, null);
}
underlineView = listItemsView.findViewById(R.id.underlineView);
notesListRelativeLayout = (RelativeLayout) listItemsView.findViewById(R.id.notesListRelativeLayout);
noteTitleTextView = (TextView) listItemsView.findViewById(R.id.noteTitleTextView);
tickImage = (ImageView) listItemsView.findViewById(R.id.tickImageView);
noteTitleTextView.setText(notes.get(position).getTitle());
return listItemsView;
}
我可以使用OnItemClickListener访问ListView内部的项目,但我不知道如何访问ListView特定项目内的视图。
所以,如果我这样设置OnClick:
notesListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
我如何引用该项目中的三个视图之一。例如,我需要设置imageview的可见性:
感谢您的任何帮助。
尝试一下,OnItemClickListener将返回该视图,以便您可以通过该视图进行访问
notesListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ImageView tickImage = view.findViewById(R.id. tickImageView);
if(tickImage!=null){
tickImage.setVisibility(View.GONE);
}
}
});