[英]Keep Image of Image Button when scrolling in ListView
I'm having a problem while implementing a "mark as favorite" function in my listview. 在我的列表视图中实现“标记为收藏夹”功能时遇到问题。 I have an ImageButton placed on the right side of every listview item and "on click" I want to change the image of the button. 我在每个listview项的右侧放置了一个ImageButton,“单击时”我想更改按钮的图像。 It should look like this: 它看起来应该像这样:
That's pretty easy to implement and works perfectly fine. 这很容易实现,并且工作得很好。 The problem now is when I scroll the list, the selected image buttons (stars) won't stay the same. 现在的问题是,当我滚动列表时,选定的图像按钮(星号)将保持不变。
To keep the selection of a listview item isn't that hard, but to keep the changed image in the list is an issue for me. 保留对listview项的选择并不难,但是将更改后的图像保留在列表中对我来说是一个问题。 The former can be implemented in this way: https://stackoverflow.com/a/9281155/2054118 前者可以通过以下方式实现: https : //stackoverflow.com/a/9281155/2054118
So I tried something similar. 所以我尝试了类似的东西。 In the getView() method I init my imagebutton and its OnClickListener to save the actual position in a LinkedList. 在getView()方法中,我初始化了我的imagebutton及其OnClickListener,以将实际位置保存在LinkedList中。 All positions of the selected ImageButtons should be in the list: 所选ImageButton的所有位置都应在列表中:
MyAdapter.java: MyAdapter.java:
private LinkedList<Integer> selectedIndeces;
...
private static class ViewHolder {
...
public ImageButton favoriteButton;
}
public void changeSelectedPositions(int pos) {
int index = this.selectedIndeces.indexOf(pos);
if (index != -1) {
// image button in this row was selected
this.selectedIndeces.remove(index);
} else {
// mark position of the image button as selected
this.selectedIndeces.add(pos);
}
notifyDataSetChanged();
}
...
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View rowView = convertView;
// reuse views
if (rowView == null) {
rowView = this.mInflater.inflate(R.layout.talk_list_child, null);
// configure view holder
ViewHolder viewHolder = new ViewHolder();
...
ImageButton ib = (ImageButton) rowView.findViewById(R.id.ib_favorite);
ib.setFocusable(false);
ib.setFocusableInTouchMode(false);
ib.setOnClickListener(new OnClickListener() {
public void onClick(View button) {
// Set the button's appearance
button.setSelected(!button.isSelected());
changeSelectedPositions(position);
}
});
viewHolder.favoriteButton = ib;
}
// fill data
ViewHolder holder = (ViewHolder) rowView.getTag();
...
if (selectedIndeces.contains(position)) {
holder.favoriteButton.setImageResource(R.drawable.fav_selected);
} else {
holder.favoriteButton.setImageResource(R.drawable.fav_unselected);
}
}
But the problem is that the position I get in getView() isn't always the same and depends on where the list is scrolled to? 但是问题是我在getView()中获得的位置并不总是相同,并且取决于列表滚动到的位置?
So to shorten my question: How can I keep the selection/image of my ImageButtons in the ListView even when I scroll? 因此,为了简化我的问题:即使滚动,如何将ImageButton的选择/图像保留在ListView中?
You saved your position while preparing viewHolder
. 您在准备viewHolder
保存了职位。 Then after scrolling, view is recycled and the same viewHolder
with previously stored position is used. 然后,在滚动之后,将回收视图,并使用具有先前存储位置的相同viewHolder
。 You can just set listener every time you get getView()
call. 您每次调用getView()
都可以设置侦听器。
Move 移动
ib.setOnClickListener(new OnClickListener() {
public void onClick(View button) {
// Set the button's appearance
button.setSelected(!button.isSelected());
changeSelectedPositions(position);
}
});
Just before 就在之前
if (selectedIndeces.contains(position)) {
Keep an array list to save the position which is clicked in your adapter 保留一个数组列表以保存在适配器中单击的位置
iconClickedPostion = new ArrayList(); iconClickedPostion = new ArrayList();
Whenever you handle your click for the image button in the list, add the position of the listview in the arraylist. 只要您单击列表中的图像按钮,就将列表视图的位置添加到arraylist中。
iconClickedPostion.add(position); iconClickedPostion.add(position);
In getView(), check the condition if it is already clicked then set the desired image. 在getView()中,检查条件是否已单击,然后设置所需的图像。
if(iconClickedPostion.contains(position)){ completedIcon.setImageResource(R.drawable.ic_done); if(iconClickedPostion.contains(position)){completedIcon.setImageResource(R.drawable.ic_done); } else { completedIcon.setImageResource(iconId[position]); }其他{completedIcon.setImageResource(iconId [position]); } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.