Answers:
有了您的信息,米歇尔,我找到了答案。您确实可以使用来获得正确的视图View#getChildAt(int index)
。问题是它从第一个可见项目开始计数。实际上,您只能获得可见的项目。您可以使用解决ListView#getFirstVisiblePosition()
。
例:
private void updateView(int index){
View v = yourListView.getChildAt(index -
yourListView.getFirstVisiblePosition());
if(v == null)
return;
TextView someText = (TextView) v.findViewById(R.id.sometextview);
someText.setText("Hi! I updated you manually!");
}
这个问题已在Google I / O 2010上提出,您可以在这里观看:
基本上什么罗曼盖伊解释是调用getChildAt(int)
上ListView
得到的看法和(我认为)调用getFirstVisiblePosition()
,找出位置和指数之间的相关性。
Romain还以名为Shelves的项目为例,我认为他可能是这个方法ShelvesActivity.updateBookCovers()
,但我找不到的调用getFirstVisiblePosition()
。
令人敬畏的更新即将到来:
RecyclerView将在不久的将来解决此问题。如http://www.grokkingandroid.com/first-glance-androids-recyclerview/所指出的那样,您将能够调用方法来精确指定更改,例如:
void notifyItemInserted(int position)
void notifyItemRemoved(int position)
void notifyItemChanged(int position)
此外,每个人都希望使用基于RecyclerView的新视图,因为它们将获得精美外观的动画!未来看起来棒极了!:-)
这是我的方法:
您的商品(行)必须具有唯一的ID,以便以后进行更新。当列表从适配器获取视图时,设置每个视图的标签。(如果在其他地方使用了默认标签,也可以使用键标签)
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = super.getView(position, convertView, parent);
view.setTag(getItemId(position));
return view;
}
对于更新,请检查列表的每个元素,如果存在具有给定id的视图,则该视图可见,因此我们执行更新。
private void update(long id)
{
int c = list.getChildCount();
for (int i = 0; i < c; i++)
{
View view = list.getChildAt(i);
if ((Long)view.getTag() == id)
{
// update view
}
}
}
实际上,它比其他方法更容易,并且在处理id而不是头寸时会更好!您还必须为可见的项目调用更新。
首先像这个模型类对象一样获得全局的模型类
SampleModel golbalmodel=new SchedulerModel();
并初始化为全局
通过将模型初始化为全局模型来获得视图的当前行
SampleModel data = (SchedulerModel) sampleList.get(position);
golbalmodel=data;
将更改的值设置为要设置的全局模型对象方法,并为我添加notifyDataSetChanged
golbalmodel.setStartandenddate(changedate);
notifyDataSetChanged();
答案是明确正确的,在此我将为CursorAdapter
案例添加一个思路。
如果您是子类化CursorAdapter
(或ResourceCursorAdapter
或SimpleCursorAdapter
),则可以实现ViewBinder
或重写bindView()
和newView()
方法,这些方法不会在参数中接收当前列表项的索引。因此,当一些数据到达并且您想要更新相关的可见列表项时,您如何知道它们的索引?
我的解决方法是:
newView()
notifyDatasetChanged()
并刷新所有数据更好由于视图回收,我需要存储和迭代的视图引用数将大致等于屏幕上可见的列表项数。
int wantedPosition = 25; // Whatever position you're looking for
int firstPosition = linearLayoutManager.findFirstVisibleItemPosition(); // This is the same as child #0
int wantedChild = wantedPosition - firstPosition;
if (wantedChild < 0 || wantedChild >= linearLayoutManager.getChildCount()) {
Log.w(TAG, "Unable to get view for desired position, because it's not being displayed on screen.");
return;
}
View wantedView = linearLayoutManager.getChildAt(wantedChild);
mlayoutOver =(LinearLayout)wantedView.findViewById(R.id.layout_over);
mlayoutPopup = (LinearLayout)wantedView.findViewById(R.id.layout_popup);
mlayoutOver.setVisibility(View.INVISIBLE);
mlayoutPopup.setVisibility(View.VISIBLE);
对于RecycleView,请使用此代码
我使用了提供Erik的代码,效果很好,但是我为列表视图使用了一个复杂的自定义适配器,并且我遇到了两次更新UI的代码实现。我试图从我的适配器的getView方法中获取新视图(保存listview数据的arraylist已全部更新/更改):
View cell = lvOptim.getChildAt(index - lvOptim.getFirstVisiblePosition());
if(cell!=null){
cell = adapter.getView(index, cell, lvOptim); //public View getView(final int position, View convertView, ViewGroup parent)
cell.startAnimation(animationLeftIn());
}
它运行良好,但是我不知道这是否是一个好习惯。因此,我不需要实现两次更新列表项的代码。
正是我用这个
private void updateSetTopState(int index) {
View v = listview.getChildAt(index -
listview.getFirstVisiblePosition()+listview.getHeaderViewsCount());
if(v == null)
return;
TextView aa = (TextView) v.findViewById(R.id.aa);
aa.setVisibility(View.VISIBLE);
}
我提出了另一种解决方案,像RecyclyerView方法无效notifyItemChanged(int position)
,创建CustomBaseAdapter类就像这样:
public abstract class CustomBaseAdapter implements ListAdapter, SpinnerAdapter {
private final CustomDataSetObservable mDataSetObservable = new CustomDataSetObservable();
public boolean hasStableIds() {
return false;
}
public void registerDataSetObserver(DataSetObserver observer) {
mDataSetObservable.registerObserver(observer);
}
public void unregisterDataSetObserver(DataSetObserver observer) {
mDataSetObservable.unregisterObserver(observer);
}
public void notifyDataSetChanged() {
mDataSetObservable.notifyChanged();
}
public void notifyItemChanged(int position) {
mDataSetObservable.notifyItemChanged(position);
}
public void notifyDataSetInvalidated() {
mDataSetObservable.notifyInvalidated();
}
public boolean areAllItemsEnabled() {
return true;
}
public boolean isEnabled(int position) {
return true;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getView(position, convertView, parent);
}
public int getItemViewType(int position) {
return 0;
}
public int getViewTypeCount() {
return 1;
}
public boolean isEmpty() {
return getCount() == 0;
} {
}
}
不要忘了创建CustomDataSetObservable类也为mDataSetObservable
变量CustomAdapterClass,就像这样:
public class CustomDataSetObservable extends Observable<DataSetObserver> {
public void notifyChanged() {
synchronized(mObservers) {
// since onChanged() is implemented by the app, it could do anything, including
// removing itself from {@link mObservers} - and that could cause problems if
// an iterator is used on the ArrayList {@link mObservers}.
// to avoid such problems, just march thru the list in the reverse order.
for (int i = mObservers.size() - 1; i >= 0; i--) {
mObservers.get(i).onChanged();
}
}
}
public void notifyInvalidated() {
synchronized (mObservers) {
for (int i = mObservers.size() - 1; i >= 0; i--) {
mObservers.get(i).onInvalidated();
}
}
}
public void notifyItemChanged(int position) {
synchronized(mObservers) {
// since onChanged() is implemented by the app, it could do anything, including
// removing itself from {@link mObservers} - and that could cause problems if
// an iterator is used on the ArrayList {@link mObservers}.
// to avoid such problems, just march thru the list in the reverse order.
mObservers.get(position).onChanged();
}
}
}
在CustomBaseAdapter类上有一个方法notifyItemChanged(int position)
,并且可以在想要更新行的任何位置(从按钮单击或任何要调用该方法的地方)调用该方法。瞧!,您的单行将立即更新。
我的解决方案:如果正确*,请更新数据和可见项,而无需重新绘制整个列表。否则notifyDataSetChanged。
正确-oldData size ==新数据大小,旧数据ID及其顺序==新数据ID和顺序
怎么样:
/**
* A View can only be used (visible) once. This class creates a map from int (position) to view, where the mapping
* is one-to-one and on.
*
*/
private static class UniqueValueSparseArray extends SparseArray<View> {
private final HashMap<View,Integer> m_valueToKey = new HashMap<View,Integer>();
@Override
public void put(int key, View value) {
final Integer previousKey = m_valueToKey.put(value,key);
if(null != previousKey) {
remove(previousKey);//re-mapping
}
super.put(key, value);
}
}
@Override
public void setData(final List<? extends DBObject> data) {
// TODO Implement 'smarter' logic, for replacing just part of the data?
if (data == m_data) return;
List<? extends DBObject> oldData = m_data;
m_data = null == data ? Collections.EMPTY_LIST : data;
if (!updateExistingViews(oldData, data)) notifyDataSetChanged();
else if (DEBUG) Log.d(TAG, "Updated without notifyDataSetChanged");
}
/**
* See if we can update the data within existing layout, without re-drawing the list.
* @param oldData
* @param newData
* @return
*/
private boolean updateExistingViews(List<? extends DBObject> oldData, List<? extends DBObject> newData) {
/**
* Iterate over new data, compare to old. If IDs out of sync, stop and return false. Else - update visible
* items.
*/
final int oldDataSize = oldData.size();
if (oldDataSize != newData.size()) return false;
DBObject newObj;
int nVisibleViews = m_visibleViews.size();
if(nVisibleViews == 0) return false;
for (int position = 0; nVisibleViews > 0 && position < oldDataSize; position++) {
newObj = newData.get(position);
if (oldData.get(position).getId() != newObj.getId()) return false;
// iterate over visible objects and see if this ID is there.
final View view = m_visibleViews.get(position);
if (null != view) {
// this position has a visible view, let's update it!
bindView(position, view, false);
nVisibleViews--;
}
}
return true;
}
而且当然:
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
final View result = createViewFromResource(position, convertView, parent);
m_visibleViews.put(position, result);
return result;
}
忽略bindView的最后一个参数(我用它来确定是否需要回收ImageDrawable的位图)。
如上所述,“可见”视图的总数大约是适合屏幕显示的数量(忽略方向更改等),因此从内存角度来讲并不是很大。
除了此解决方案(https://stackoverflow.com/a/3727813/5218712)之外,仅想补充一点,即listView.getChildCount() == yourDataList.size();
ListView内可能有其他视图时,它才有效。