在BaseAdapter中setVisibility(View.GONE);没有隐藏视图但是空间被释放

时间:2016-11-25 09:50:38

标签: android android-layout listview listviewitem

在我的BaseAdpater中,我在一些操作后将视图的可见性设置为连续的GONE。所有操作都很好,但视图在背景中仍然可见,而较低的视图向上移动,直到我触摸屏幕。这意味着listview正在更新视图并释放空间,将可见性设置为GONE,但视图在后台仍然可见。可能是什么问题?

代码:

import android.content.Context;
import android.content.DialogInterface;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.iu.foodbucket.BaseActivity;
import com.iu.foodbucket.R;
import com.iu.foodbucket.database.MyDataSource;
import com.iu.foodbucket.fragments.CartFragment;
import com.iu.foodbucket.models.Cart;
import com.iu.foodbucket.utils.AppGlobal;

import java.util.List;

public class CartAdapter extends BaseAdapter {
    Fragment context;
    List<Cart> data;
    MyDataSource myDataSource;
    boolean isOldOrder;

    public CartAdapter(Fragment ctx, List<Cart> data, boolean orderStatus) {
        this.context = ctx;
        this.data = data;
        this.isOldOrder = orderStatus;
        myDataSource = new MyDataSource(ctx.getActivity());
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

        LayoutInflater inflater = (LayoutInflater) context.getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.row_cart, parent, false);
            holder.title_tv = (TextView) convertView.findViewById(R.id.title_tv);
            holder.quantity_tv = (TextView) convertView.findViewById(R.id.quantity_tv);
            holder.price_tv = (TextView) convertView.findViewById(R.id.price_tv);
            holder.edit_iv = (ImageView) convertView.findViewById(R.id.edit_iv);
            holder.thumbnail_iv = (ImageView) convertView.findViewById(R.id.thumbnail_iv);
            holder.editPanel = (LinearLayout) convertView.findViewById(R.id.editPanel);
            convertView.setTag(holder);
        } else {

            holder = (ViewHolder) convertView.getTag();
        }
        holder.title_tv.setText(data.get(position).getMenu().getTitle());
        holder.quantity_tv.setText(context.getString(R.string.quantity_abbr) + " " + data.get(position).getQuantity());


        if (((BaseActivity) context.getActivity()).dataPreference.getIsImageEnabled())
            Glide.with(context.getActivity()).load(data.get(position).getMenu().getThumbnail()).placeholder(R.drawable.ic_tag_face).centerCrop().into(holder.thumbnail_iv);

        float price = Float.parseFloat(data.get(position).getMenu().getPrice());
        price = (float) AppGlobal.round(price, 2);

        holder.price_tv.setText(data.get(position).getMenu().getPriceUnit() + " " + price);
        final View finalConvertView = convertView;
        holder.edit_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (holder.editPanel.getVisibility() != View.VISIBLE) {
                    holder.editPanel.setVisibility(View.VISIBLE);
                    showEditablePanel(finalConvertView, position, holder.editPanel);
                } else {
                    holder.editPanel.setVisibility(View.GONE);
                }
            }
        });
        return convertView;
    }

    private void showEditablePanel(View v, final int position, final LinearLayout editPanel) {
        final TextView quantity_tv = (TextView) v.findViewById(R.id.quantityUpdated_tv);
        final ImageButton minus_btn = (ImageButton) v.findViewById(R.id.minus_btn);
        final ImageButton plus_btn = (ImageButton) v.findViewById(R.id.plus_btn);
        ImageButton update_btn = (ImageButton) v.findViewById(R.id.update_btn);
        ImageButton remove_btn = (ImageButton) v.findViewById(R.id.remove_btn);
        final int[] quantity = {Integer.parseInt(data.get(position).getQuantity())};
        quantity_tv.setText(quantity[0] + "");
        minus_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                quantity[0]--;
                updateViews(quantity[0], minus_btn, quantity_tv);
            }
        });
        plus_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                quantity[0]++;
                updateViews(quantity[0], minus_btn, quantity_tv);
            }
        });
        update_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!isOldOrder)   // update cart item in DB
                    myDataSource.updateCartItem(data.get(position).getMenu().getId(), quantity[0] + "");
                data.get(position).setQuantity(quantity[0] + "");
                editPanel.setVisibility(View.GONE);
                notifyDataSetChanged();
                ((CartFragment) context).updateTotalValues();
            }
        });
        remove_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                deletionConfirmationDialog(position, editPanel);
            }
        });
    }

    public void deletionConfirmationDialog(final int position, final LinearLayout editPanel) {
        final android.app.AlertDialog.Builder alertDialogBuilder = new android.app.AlertDialog.Builder(context.getActivity());
        alertDialogBuilder.setCancelable(false);
        alertDialogBuilder.setTitle(context.getString(R.string.title_delete));
        alertDialogBuilder.setMessage(context.getString(R.string.message_confirm_deletion));
        alertDialogBuilder.setPositiveButton(context.getString(R.string.title_yes), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                if (!isOldOrder)     // delete cart item from DB
                    myDataSource.deleteCartItem(data.get(position).getMenu().getId());
                editPanel.setVisibility(View.GONE);
                data.remove(position);
                notifyDataSetChanged();
                ((CartFragment) context).updateTotalValues();
            }
        });
        alertDialogBuilder.setNegativeButton(context.getString(R.string.title_no), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // if this button is clicked, just close
            }
        });
        android.app.AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }


    private void updateViews(int quantity, ImageButton minus_btn, TextView quantity_tv) {
        if (quantity > 1) {
            minus_btn.setEnabled(true);
            minus_btn.setAlpha((float) 1.0);
        } else {
            minus_btn.setEnabled(false);
            minus_btn.setAlpha((float) 0.4);
        }
        quantity_tv.setText(quantity + "");
    }

    private class ViewHolder {
        TextView title_tv, quantity_tv, price_tv;
        ImageView thumbnail_iv, edit_iv;
        LinearLayout editPanel;
    }
}

1 个答案:

答案 0 :(得分:0)

如果你的意思是在连续设置visiblity后,该行保持空白并且该间隙是可见的,更好的方法是通过使用getFilter()显示来排除适配器内容,方法是排除该行内容并更改notifyadapter。 / p>