为啥滚动时列表视图项目内的图像视图会消失?

Posted

技术标签:

【中文标题】为啥滚动时列表视图项目内的图像视图会消失?【英文标题】:Why the Image view inside the item of a list view disappears when i scroll?为什么滚动时列表视图项目内的图像视图会消失? 【发布时间】:2015-12-03 01:57:32 【问题描述】:

我在标签片段中有一个列表视图, 现在在列表视图中,我有两个视图,一个视图已经不可见。当我滚动列表视图时,两个视图都消失了。我正在处理持有者,但我仍然面临问题。

这是我的 xml:

<RelativeLayout
    android:id="@+id/rl_maino"
    android:layout_
    android:layout_ 
    android:background="@color/white"
    >

     <RelativeLayout
            android:id="@+id/rl_coloro"
            android:layout_
            android:layout_
            android:layout_alignParentLeft="true"
            android:layout_marginTop="13dp"
            android:background="@color/green"
             >
        </RelativeLayout>

    <LinearLayout
        android:id="@+id/ll_texto"
        android:layout_
        android:layout_
        android:orientation="vertical" 
           android:layout_marginTop="14dp"
        android:layout_marginLeft="16dp">

         <TextView
        android:id="@+id/tv_subtitleo"
        android:layout_
        android:layout_
        android:textColor="@color/black"
        android:textSize="14sp"
        android:text="Header" />

         <TextView
             android:id="@+id/tv_subo"
             android:layout_
             android:layout_
             android:textColor="@color/addr_col"
             android:textSize="12sp"
             android:layout_marginTop="3dp"
             android:text="subtext subtext subtext subtext subtext subtexts ubtexts subtexts" />

    </LinearLayout>


        <TextView
            android:id="@+id/tv_costo"
            android:layout_
            android:layout_
            android:layout_marginTop="14dp"
            android:layout_marginRight="16dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:textColor="@color/black"
            android:text="AED 12200" />

        <LinearLayout
            android:id="@+id/ll_addo"
            android:layout_
            android:layout_
            android:layout_below="@id/tv_costo"
            android:orientation="horizontal"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_marginTop="3dp"
            android:layout_marginBottom="14dp"
            android:layout_marginRight="16dp"

             >

            <ImageView
                android:id="@+id/iv_minuso"
                android:layout_
                android:layout_
                android:src="@drawable/minus_selector" 
                android:visibility="gone"/>

            <TextView
                android:id="@+id/tv_numbero"
                android:layout_
                android:layout_
                android:layout_gravity="center_vertical"
                android:textColor="@color/black"
                android:layout_margin="5dp"
                android:gravity="center"
                android:textSize="15sp"
                android:text="0" />

            <ImageView
                android:id="@+id/iv_pluso"
                android:layout_
                android:layout_
                android:src="@drawable/plus_selector" />

        </LinearLayout>

         <LinearLayout
            android:id="@+id/ll_unavailo"
            android:layout_
            android:layout_
            android:layout_below="@id/tv_costo"
            android:orientation="horizontal"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_marginTop="3dp"
            android:layout_marginBottom="10dp"
            android:layout_marginRight="16dp"
             android:visibility="gone"
             >


            <TextView
                android:id="@+id/tv_unavailo"
                android:layout_
                android:layout_
                android:layout_gravity="center_vertical"
                android:textColor="@color/addr_col"
                android:layout_margin="2dp"
                android:gravity="center_vertical|right"
                android:textSize="12sp"
                android:text="@string/c_unavail" />


        </LinearLayout>



</RelativeLayout>

这是我的适配器:

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

    if (convertView == null) 

        LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.only_pro_items, null);
        holder=new ViewHolder();

          holder.rl_main=(RelativeLayout)convertView.findViewById(R.id.rl_maino);
          holder.rl_color=(RelativeLayout)convertView.findViewById(R.id.rl_coloro);
          holder.ll_unavail=(LinearLayout)convertView.findViewById(R.id.ll_unavailo); 
          holder.ll_text=(LinearLayout)convertView.findViewById(R.id.ll_texto);
          holder.ll_add=(LinearLayout)convertView.findViewById(R.id.ll_addo);
          holder.tv_subtitle=(TextView)convertView.findViewById(R.id.tv_subtitleo);
          holder.tv_sub=(TextView)convertView.findViewById(R.id.tv_subo);
          holder.tv_cost=(TextView)convertView.findViewById(R.id.tv_costo);
          holder.tv_number=(TextView)convertView.findViewById(R.id.tv_numbero);
          holder.tv_unavail=(TextView)convertView.findViewById(R.id.tv_unavailo);
          holder.iv_minus=(ImageView)convertView.findViewById(R.id.iv_minuso);
          holder.iv_plus=(ImageView)convertView.findViewById(R.id.iv_pluso);

          convertView.setTag(holder);

       else 
          Log.d("ttt", "3");
          holder=(ViewHolder) convertView.getTag();

      

    holder.tv_subtitle.setTypeface(gotham_book);
    holder.tv_sub.setTypeface(gotham_light);
    holder.tv_cost.setTypeface(gotham_book);
    holder.tv_number.setTypeface(gotham_book);
    holder.tv_unavail.setTypeface(gotham_light);

    final Model_OnlyProduct model=arr_model.get(position);
    holder.tv_subtitle.setText(model.getTv_name());
    holder.tv_sub.setText(model.getTv_sub_name());

     final int price[]=0;

    if (!model.getTv_price().matches("")) 
        holder.tv_cost.setText(Constants.currency+model.getTv_price());
        price[0]=Integer.parseInt(model.getTv_price());
     else 
        holder.tv_cost.setText("From "+Constants.currency+model.getFrom_price());
        price[0]=Integer.parseInt(model.getFrom_price());
    




   if (model.getInStock().matches("In Stock")) 

      holder.ll_unavail.setVisibility(View.GONE);
    else 

        holder.ll_unavail.setVisibility(View.VISIBLE);
        holder.ll_add.setVisibility(View.GONE);
    


  String food_class=model.getRl_color();

   switch (food_class) 
    case "Veg":
        holder.rl_color.setBackgroundColor(activity.getResources().getColor(R.color.veg_green));
    break;
    case "Non Veg":
        holder.rl_color.setBackgroundColor(activity.getResources().getColor(R.color.non_red));
    break;

    case "Egg":
        holder.rl_color.setBackgroundColor(activity.getResources().getColor(R.color.egg_yellow));
    break;

    case "":
        holder.rl_color.setBackgroundColor(activity.getResources().getColor(R.color.white));
    break;    

    


   final int[] count=0;
   if (count[0]==0) 
    holder.tv_number.setVisibility(View.INVISIBLE);
       

   holder.iv_plus.setOnClickListener(new OnClickListener() 

        @Override
        public void onClick(View v) 

            if (count[0]>=0&&count[0]!=99) 
                Log.d("qqq", "ar1r="+model.getCustom_status());
                if (model.getCustom_status().matches("1")) 

                    if (model.getCustom_type().matches("Combo")) 
                        Intent intent=new Intent(activity, Combo.class);
                        intent.putExtra(Constants.ITEM_ID, String.valueOf(model.getId()));
                        intent.putExtra(Constants.ITEM_NAME,model.getTv_name());
                        if (!model.getTv_price().matches("")) 
                            intent.putExtra(Constants.ITEM_TYPE, "price");
                            intent.putExtra(Constants.ITEM_PRICE, model.getTv_price());
                         else 
                            intent.putExtra(Constants.ITEM_TYPE, "from");
                            intent.putExtra(Constants.ITEM_PRICE, model.getFrom_price());
                        

                        intent.putExtra(Constants.ITEM_COUNT,  String.valueOf(count[0]));
                        intent.putExtra(Constants.ITEM_SUBTEXT, model.getTv_sub_name());
                        intent.putExtra(Constants.ITEM_FROM, "menu");
                        intent.putExtra(Constants.VIEW_INDEX, ""+model.getView_index());
                        activity.startActivity(intent);
                        activity.overridePendingTransition(R.anim.move_up, R.anim.stay);
                     else 
                        Intent intent=new Intent(activity, Toppings.class);
                        intent.putExtra(Constants.ITEM_ID, String.valueOf(model.getId()));
                        intent.putExtra(Constants.ITEM_NAME,model.getTv_name());
                        if (!model.getTv_price().matches("")) 
                            intent.putExtra(Constants.ITEM_TYPE, "price");
                            intent.putExtra(Constants.ITEM_PRICE, model.getTv_price());
                         else 
                            intent.putExtra(Constants.ITEM_TYPE, "from");
                            intent.putExtra(Constants.ITEM_PRICE, model.getFrom_price());
                        

                        intent.putExtra(Constants.ITEM_COUNT,  String.valueOf(count[0]));
                        intent.putExtra(Constants.ITEM_SUBTEXT, model.getTv_sub_name());
                        intent.putExtra(Constants.ITEM_FROM, "menu");
                        activity.startActivity(intent);
                        activity.overridePendingTransition(R.anim.move_up, R.anim.stay);
                    
                else
                

                holder.tv_number.setVisibility(View.VISIBLE);
                count[0]++;
                Constants.cart_count++;

                activity.runOnUiThread(new Runnable() 

                    @Override
                    public void run() 
                        // TODO Auto-generated method stub
                        holder.tv_number.setText(String.valueOf(count[0]));
                        FragmentMenu.tv_count.setVisibility(View.VISIBLE);
                        FragmentMenu.tv_count.setText(String.valueOf(Constants.cart_count));

                        int total=price[0];
                        Constants.cart_amount+=total;
                        FragmentMenu.tv_cart_money.setText(Constants.currency+Constants.cart_amount);
                    
                );
                Constants.count++;
                String id=String.valueOf(Constants.count);
                HashMap<String, String> map=new HashMap<String, String>();
                map.put(Constants.ITEM_ID, String.valueOf(model.getId()));
                map.put(Constants.ITEM_NAME, model.getTv_name());


                    map.put(Constants.ITEM_PRICE, model.getTv_price());



                map.put(Constants.ITEM_PRICE, model.getTv_price());
                map.put(Constants.ITEM_COUNT, String.valueOf(count[0]));
                map.put(Constants.ID, id);


                if (Constants.arr_global.size()>0) 
                    int count=Constants.arr_global.size();
                    boolean yettop=false;
                    int qwer=0;
                    for (int i = 0; i < count; i++) 
                        HashMap<String, String> map2=Constants.arr_global.get(i);
                        String item_id=map2.get(Constants.ITEM_ID);
                        if (item_id.matches(String.valueOf(model.getId()))) 
                            yettop=true;
                            qwer=i;
                            break;
                        
                        else 
                            yettop=false;
                        
                    

                    if (yettop) 
                        Constants.arr_global.remove(qwer);
                        Constants.arr_global.add(map);
                     else 
                        Constants.arr_global.add(map);
                    
                else 
                    Constants.arr_global.add(map);
                

            

        
        
    );

    return convertView;


问题是:

if (model.getInStock().matches("In Stock")) 

      holder.ll_unavail.setVisibility(View.GONE);
    else 

        holder.ll_unavail.setVisibility(View.VISIBLE);
        holder.ll_add.setVisibility(View.GONE);
    

在这个循环中,下一个项目的可见性得到改变..

【问题讨论】:

您的布局不包含任何按钮... 我的错误图片视图 【参考方案1】:

使用这个条件并检查

 if (model.getInStock().matches("In Stock")) 
holder.ll_add.setVisibility(View.VISIBLE);
      holder.ll_unavail.setVisibility(View.GONE);
    else 

        holder.ll_unavail.setVisibility(View.VISIBLE);
        holder.ll_add.setVisibility(View.GONE);
    

【讨论】:

欢迎您也必须对 holder.tv_number 做同样的事情,我认为

以上是关于为啥滚动时列表视图项目内的图像视图会消失?的主要内容,如果未能解决你的问题,请参考以下文章

在我的 android 应用程序中滚动列表视图时,数据正在消失。我在片段中使用列表视图

滚动登录时间后,表格视图中的图像会消失

如何使用滚动视图单击列表部分内的项目以导航到详细信息页面 SwiftUI

android在单击按钮时更改列表视图内的图像视图

Flutter:从网格视图横向滚动的列表视图过渡

如何使用滚动视图使隐藏的按钮菜单出现/消失?