带字母的字母列表 - 可点击
Alphabetic list with letter - clickable
我正在创建一个按字母顺序排列的列表,其中还有一个字母显示在项目上方 - 参见图像:
这已经完成并且工作正常,但我需要让每个项目都可以点击,字母除外。
这是我的代码的一部分,来自 MainActivity.java(片段):
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_bohovia, container, false);
List<LetterSectionListItem> books = createBooks();
ListView bookListView = (ListView) rootView.findViewById(R.id.booklistview);
LetterSectionListAdapter bookListAdapter = new LetterSectionListAdapter(getContext(), R.layout.booklist_row, books) {
@NonNull
@Override
public View getView(int position, View v, ViewGroup parent) {
//Must call this before to wrap the header around the view
v = super.getView(position, v, parent);
TextView bookTitle = (TextView) v.findViewById(R.id.book_title);
TextView authorName = (TextView) v.findViewById(R.id.author_name);
Book book = (Book) this.getItem(position);
assert book != null;
bookTitle.setText(book.getBookName());
authorName.setText(book.getAuthorLastName() + ", " + book.getAuthorFirstName());
return v;
}
};
bookListView.setAdapter(bookListAdapter);
return rootView;
}
现在我尝试了什么:
我把这段代码放在那里 return rootView;
:
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(v.getContext(),SingleitemView.class);
intent.putExtra("place2", "test");
getContext().startActivity(intent);
}
});
但是,这工作正常,所以在单击该项目后打开另一个 activity,而且当我单击独立字母时也是如此,我不希望有可单击的字母 A、B、C。 .等,仅以下项目。
有人可以在这里提出建议吗,我做错了什么?
您需要使用 OnItemClick 并指定可点击的项目
这么简单的事情居然没人帮得上忙!我自己解决了,在我的适配器中我只是使用了这个:
TextView headerLetterTextView = (TextView) headerLayout.findViewById(R.id.item_row_letter);
headerLetterTextView.setOnClickListener(null);
很有魅力!
我正在创建一个按字母顺序排列的列表,其中还有一个字母显示在项目上方 - 参见图像:
这已经完成并且工作正常,但我需要让每个项目都可以点击,字母除外。
这是我的代码的一部分,来自 MainActivity.java(片段):
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_bohovia, container, false);
List<LetterSectionListItem> books = createBooks();
ListView bookListView = (ListView) rootView.findViewById(R.id.booklistview);
LetterSectionListAdapter bookListAdapter = new LetterSectionListAdapter(getContext(), R.layout.booklist_row, books) {
@NonNull
@Override
public View getView(int position, View v, ViewGroup parent) {
//Must call this before to wrap the header around the view
v = super.getView(position, v, parent);
TextView bookTitle = (TextView) v.findViewById(R.id.book_title);
TextView authorName = (TextView) v.findViewById(R.id.author_name);
Book book = (Book) this.getItem(position);
assert book != null;
bookTitle.setText(book.getBookName());
authorName.setText(book.getAuthorLastName() + ", " + book.getAuthorFirstName());
return v;
}
};
bookListView.setAdapter(bookListAdapter);
return rootView;
}
现在我尝试了什么:
我把这段代码放在那里 return rootView;
:
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(v.getContext(),SingleitemView.class);
intent.putExtra("place2", "test");
getContext().startActivity(intent);
}
});
但是,这工作正常,所以在单击该项目后打开另一个 activity,而且当我单击独立字母时也是如此,我不希望有可单击的字母 A、B、C。 .等,仅以下项目。
有人可以在这里提出建议吗,我做错了什么?
您需要使用 OnItemClick 并指定可点击的项目
这么简单的事情居然没人帮得上忙!我自己解决了,在我的适配器中我只是使用了这个:
TextView headerLetterTextView = (TextView) headerLayout.findViewById(R.id.item_row_letter);
headerLetterTextView.setOnClickListener(null);
很有魅力!