Android LiistView项目更新

时间:2015-04-09 01:09:53

标签: android listview android-listview listviewitem onscrolllistener

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

1 个答案:

答案 0 :(得分: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;
    }

}