Android LiistView 项目更新
Android LiistView item update
我有一个包含 30 到 40 个项目的列表视图。每个项目都有一个图像视图和许多其他视图。当用户点击图像视图(日出图像)时,它应该与另一个图像视图(日落图像)切换。
当我点击一个项目中的日出图像时,许多其他项目也会切换。 我想知道其他项目的图像视图是如何更新的,以及我如何处理以仅在用户点击的确切位置切换项目?
我的适配器 - MyAdapter extends BaseAdapter implements AbsListView.OnScrollListener
.
如果我需要任何其他信息,请告诉我。
编辑:添加我的 getView() 方法。
public View getView(int position, View view, ViewGroup parent) {
if (position < this.data.size()) {
if (view == null) {
view = createView(position, parent);
}
//some other data fill
view.findViewById(R.id.sunrise).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
((ImageView)view.findViewById(R.id.sunrise)).setImageResource(R.drawable.sunset);
}
});
} else {
if (view == null) {
LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.waiting, null);
}
}
return view;
}
正在回收视图。 This DevBytes video on ListView
animation explains exactly what your problem is. It describes how to notify the framework that you don't want a particular view recycled. This is accomplished by calling setHasTransientState()
在有问题的 View
上。
要获得更好的解决方案,请查看 this DevBytes video on animating ListView
deletion (particularly the first three minutes). The StableArrayAdapter
overrides hasStableIds()
to return true, which has the same effect as setHasTransientState()
中的 StableArrayAdapter
。
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.listviewremovalanimation;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
public class StableArrayAdapter extends ArrayAdapter<String> {
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
View.OnTouchListener mTouchListener;
public StableArrayAdapter(Context context, int textViewResourceId,
List<String> objects, View.OnTouchListener listener) {
super(context, textViewResourceId, objects);
mTouchListener = listener;
for (int i = 0; i < objects.size(); ++i) {
mIdMap.put(objects.get(i), i);
}
}
@Override
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item);
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if (view != convertView) {
// Add touch listener to every new view to track swipe motion
view.setOnTouchListener(mTouchListener);
}
return view;
}
}
我有一个包含 30 到 40 个项目的列表视图。每个项目都有一个图像视图和许多其他视图。当用户点击图像视图(日出图像)时,它应该与另一个图像视图(日落图像)切换。
当我点击一个项目中的日出图像时,许多其他项目也会切换。 我想知道其他项目的图像视图是如何更新的,以及我如何处理以仅在用户点击的确切位置切换项目?
我的适配器 - MyAdapter extends BaseAdapter implements AbsListView.OnScrollListener
.
如果我需要任何其他信息,请告诉我。
编辑:添加我的 getView() 方法。
public View getView(int position, View view, ViewGroup parent) {
if (position < this.data.size()) {
if (view == null) {
view = createView(position, parent);
}
//some other data fill
view.findViewById(R.id.sunrise).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
((ImageView)view.findViewById(R.id.sunrise)).setImageResource(R.drawable.sunset);
}
});
} else {
if (view == null) {
LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.waiting, null);
}
}
return view;
}
正在回收视图。 This DevBytes video on ListView
animation explains exactly what your problem is. It describes how to notify the framework that you don't want a particular view recycled. This is accomplished by calling setHasTransientState()
在有问题的 View
上。
要获得更好的解决方案,请查看 this DevBytes video on animating ListView
deletion (particularly the first three minutes). The StableArrayAdapter
overrides hasStableIds()
to return true, which has the same effect as setHasTransientState()
中的 StableArrayAdapter
。
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.listviewremovalanimation;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
public class StableArrayAdapter extends ArrayAdapter<String> {
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
View.OnTouchListener mTouchListener;
public StableArrayAdapter(Context context, int textViewResourceId,
List<String> objects, View.OnTouchListener listener) {
super(context, textViewResourceId, objects);
mTouchListener = listener;
for (int i = 0; i < objects.size(); ++i) {
mIdMap.put(objects.get(i), i);
}
}
@Override
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item);
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if (view != convertView) {
// Add touch listener to every new view to track swipe motion
view.setOnTouchListener(mTouchListener);
}
return view;
}
}