【问题标题】:Android LiistView item updateAndroid ListView 项目更新
【发布时间】:2015-04-09 01:09:52
【问题描述】:

我有一个包含 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;
    }

【问题讨论】:

  • 贴出你要更改图片的类的代码。
  • @KartikKaushik - 添加了我的代码

标签: android listview android-listview listviewitem onscrolllistener


【解决方案1】:

视图正在被回收。 This DevBytes video on ListView animation 准确解释了您的问题所在。它描述了如何通知框架您不希望回收特定视图。这是通过在有问题的View 上调用setHasTransientState() 来完成的。

如需更好的解决方案,请查看this DevBytes video on animating ListView deletion 中的StableArrayAdapter(尤其是前三分钟)。 StableArrayAdapter 覆盖hasStableIds() 返回true,效果与setHasTransientState() 相同。

/*
 * 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;
    }

}

【讨论】:

  • 感谢视频链接和代码,我正在修改代码以使用带有 hasStableIds() 的 ArrayAdapter,让您知道更新。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多