是否有可能检查在ListView
项中单击的视图是什么?
换句话说,当您在ListView
项目应用内单击不同的视图时,应用应执行不同的操作。
详细地说,我有一个简单的Book.java
类,其中包含一些书籍描述。
然后,我使用ListView<Book>
创建BooksAdapter.class
:
public class BooksAdapter extends ArrayAdapter<Book> {
public BooksAdapter (Context context, List<Book> books) {
super(context,0,books);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
// ConstraintLayout constraintLayout = new ConstraintLayout();
// constraintLayout.setVisibility();
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.books_list_item, parent, false);
}
Book currentBook = getItem(position);
ImageView coverView = (ImageView) listItemView.findViewById(R.id.preview_image_view);
if (currentBook.getImage() == "No cover") {
coverView.setImageResource(R.drawable.no_book_cover);
} else {
Picasso.get().load(currentBook.getImage()).into(coverView);
}
TextView authorTextView = (TextView)listItemView.findViewById(R.id.autor_text);
authorTextView.setText(formatAuthor(currentBook.getAuthor(),currentBook.getDate()));
TextView titleTextView = (TextView)listItemView.findViewById(R.id.title_text);
titleTextView.setText(currentBook.getTitle());
//TextView descrTextView = (TextView)listItemView.findViewById(R.id.description_text);
//descrTextView.setText(currentBook.getDescription());
return listItemView;
}
private String formatAuthor (String name,String date ) {
name = name.substring(2,name.length()-2);
date = date.substring(0,4);
String fullString = name + ", " + date;
return(fullString);
}
}
books_list_item.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_item"
android:layout_width="match_parent"
android:layout_height="120dp"
android:orientation="horizontal"
android:paddingEnd="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingStart="16dp"
android:paddingBottom="16dp"
android:paddingTop="8dp">
<ImageView
android:id="@+id/preview_image_view"
android:layout_width="80dp"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/autor_text"
android:layout_width="match_parent"
android:layout_height="40dp"
android:maxLines="1"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="8dp"
android:textColor="@color/textColorPrimary"
android:textSize="16sp" />
<TextView
android:id="@+id/title_text"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_gravity="center_vertical"
android:maxLines="2"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="0dp"
android:textColor="@color/textColorLight"
android:textSize="16sp" />
</LinearLayout>
<ImageView
android:layout_width="40dp"
android:layout_height="match_parent"
android:src="@drawable/ic_info_black_24dp"
android:scaleType="center"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@android:color/darker_gray"/>
</LinearLayout>
在MainActivity.java
中创建mAdapter并覆盖setOnItemClickListener
:
ListView booksListView = (ListView) findViewById(R.id.list);
mAdapter = new BooksAdapter(this, new ArrayList<Book>());
booksListView.setAdapter(mAdapter);
booksListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Book book = mAdapter.getItem(position);
ad = new AlertDialog.Builder(BooksListActivity.this);
ad.setTitle("Book description");
ad.setMessage(book.getDescription());
AlertDialog alert = ad.create();
alert.show();
//ad.create();
}
});
这是一些常见的问题,我们可以在项目内部的不同onClickListeners
(例如Views
和@+id/autor_text
)上设置@+id/title_text
并在这些情况下执行不同的操作吗? br />
我正在阅读关于这几个地方的信息,但找不到任何有用的东西。谢谢您的帮助。
答案 0 :(得分:3)
在您的getView()
方法内部,将onClickListener
设置为所有不同的Views
并执行各自的操作。
通过实现以下代码:-
authorTextView.setOnClickListener(this);
titleTextView.setOnClickListener(this);
public void onClick(View v) {
switch(v.getId()){
case R.id.authorTextView:
//code to be written to handle the click event
break;
case R.id.titleTextView:
//code to be written to handle the click event
break;
}
}
};
答案 1 :(得分:0)
在您的getView
authorTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Do your action
}
});
titleTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Do your action
}
});