2016-11-25 51 views
0

在我的BaseAdpater中,我將一個視圖的可見性設置爲在某一操作後排成一行。所有操作都很好,但視圖在後臺保持可見,而較低的視圖向上移動直到我觸摸屏幕。這意味着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; 
    } 
} 
+0

發佈您的代碼。 –

+0

您能否提供您的代碼的相關片段? – 2016-11-25 09:51:59

+0

請立即檢查。 –

回答

0

,如果你的意思是設置可視性了一排後,該行仍然是空白,這一差距是明顯的,更好的辦法是使用用getFilter到適配器內容過濾器()顯示通過排除行內容和notifyadapter更改。

+0

我的意思是這個間隙被刪除,較低的項目也向上移動,但在這些視圖的後面,該項目仍然可見,除非或直到觸摸屏幕或移動listview –