android - 在什么情况下适配器不会更新附加 View

标签 android listview android-listview android-adapter android-adapterview

我正在使用这个库来创建可刷卡:https://github.com/Diolor/Swipecards

控制可滑动卡片的 View ,连接到适配器并从中获取数据。

在我的实现中,每张卡片都有一个按钮,单击该按钮时,源数组中的某些内容会发生变化,为此我想刷新整个卡片列表。我在关联的适配器上调用 notifyDataSetChanged(),但适配器中永远不会调用 getView() 来查看任何更新。

奇怪的是,同一个适配器与 ListView 完美配合

适配器端或 View 本身是否有任何特定要求是notifyDataSetChanged正常运行所必需的?

我的代码:
(请忽略 ViewHolder 模式的缺失和适配器内点击接收器的存在。当关键功能无法正常工作时,代码质量是我现在最不需要担心的事情)

适配器(使用阵列适配器)

public class TourCardAdapter extends ArrayAdapter<TourCardBean> implements View.OnClickListener {

    Context context;
    ToursFragment.ToursControlsClickListener clickListener;

    public TourCardAdapter(Context context, ArrayList<TourCardBean> tourCardsArr, ToursFragment.ToursControlsClickListener clickListener) {
        super(context, 0, tourCardsArr);
        this.context = context;
        this.clickListener = clickListener;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View rowView = convertView;
        ViewHolder viewHolder;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        TourCardBean tourCard = getItem(position);

        rowView = inflater.inflate(R.layout.card_tour, parent, false);


        viewHolder = new ViewHolder();

        viewHolder.title = (TextView) rowView
                .findViewById(R.id.title);
        viewHolder.detail = (TextView) rowView
                .findViewById(R.id.detail);
        viewHolder.likeCount = (TextView) rowView
                .findViewById(R.id.likeCount);
        viewHolder.image = (ImageView) rowView
                .findViewById(R.id.cardLocationImage);
        viewHolder.likeButton = (ImageView) rowView
                .findViewById(R.id.cardLikeImage);
        viewHolder.shareButton = (ImageView) rowView
                .findViewById(R.id.cardShareImage);

        viewHolder.likeButton.setOnClickListener(this);
        viewHolder.shareButton.setOnClickListener(this);

        viewHolder.title.setText(tourCard.getTitle());
        viewHolder.detail.setText(tourCard.getDetails());
        viewHolder.likeCount.setText("" + tourCard.getLikeCount());
        viewHolder.likeButton.setTag(tourCard.getId());
        viewHolder.shareButton.setTag(tourCard.getId());

        return rowView;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cardLikeImage:
                clickListener.onLikeClick((int) v.getTag());
                break;
            case R.id.cardShareImage:
                clickListener.onShareClick((int) v.getTag());
                break;
        }
    }


    /**
     * View Holder for ListView
     *
     * @author Aman Alam
     */
    class ViewHolder {
        public ImageView image;
        public ImageView likeButton;
        public ImageView shareButton;
        public TextView title;
        public TextView detail;
        public TextView likeCount;
    }
}

最佳答案

如果它适用于 ListView,那么您的适配器应该不是问题。

SwipeFlingAdapterView (或多或少)直接基于 AdapterView ,它根本不调用 getView() 。因此,当数据集发生更改时,它有责任调用getView()。代码的相关部分似乎是,可能会被您的点击事件阻止:

if (this.flingCardListener.isTouching()) {
    PointF lastPoint = this.flingCardListener.getLastPoint();
    if (this.mLastTouchPoint == null || !this.mLastTouchPoint.equals(lastPoint)) {
        this.mLastTouchPoint = lastPoint;
        removeViewsInLayout(0, LAST_OBJECT_IN_STACK);
        layoutChildren(1, adapterCount);
    }
}

总的来说,SwipeFlingAdapterView 似乎根本没有为数据集更改事件做好准备。

关于android - 在什么情况下适配器不会更新附加 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32339755/

相关文章:

android - 将搜索功能添加到我的应用程序的最佳解决方案

Android - 为什么这告诉我 "Content view not yet created"?

Android HTTP 请求异步任务

android - 如何在 Android ListActivity 中调用 onContentChanged 时关闭列表滚动?

.net - ListView 的 DragEnter、DragOver、DragDrop 事件未引发 (AllowDrop=True)

Android 从代码更改 tableLayout 不起作用

android - ListView 项目不会膨胀以按预期包装内容,高度 == 0 但不应该

android - SQLiteOpenHelper onCreate()/onUpgrade() 何时运行?

java - FileNotFoundException 打开失败 : ENOENT (No such file or directory)

android - 无法仅使用 xml 样式隐藏导航栏