在列表视图中显示或隐藏对象

Posted

技术标签:

【中文标题】在列表视图中显示或隐藏对象【英文标题】:Showing or hiding an object in a listview 【发布时间】:2018-09-22 16:05:36 【问题描述】:

imageview 中的图像从ListView 的滚动视图中消失。我该如何解决?

    public class IzahAdapter extends BaseAdapter 
    private ArrayList<IzahVeriModeli> list;
    LayoutInflater layoutInflater;
    Context context;
    holder Holder;
    int pos;
    public IzahAdapter(Context context, ArrayList<IzahVeriModeli> list) 
        this.context = context;
        // Layout Inflater tanımlanıyor...
        this.list = list;
    

    @Override
    public int getCount() 

        return list.size();
    

    @Override
    public Object getItem(int position) 
        return list.get(position);

    

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 

        Holder=null;
        View satirView = null;
        Button bIngg, bTrr;
        ImageView resimm = null;
//
    if (convertView == null) 
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.listview_izahat, parent, false);
        Holder = new holder();
        Holder.resim = (ImageView) convertView.findViewById(resim);
        Holder.bIng = (Button) convertView.findViewById(bIng);
        Holder.bTr = (Button) convertView.findViewById(bTr);

        Holder.bTr.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                pos = (Integer)v.getTag();
                list.get(pos).setSelected(false);
                System.out.println("pip2 = " + Integer.toString(pos));
                notifyDataSetChanged();
            
        );
        Holder.bIng.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                int pos = (Integer)v.getTag();
                list.get(pos).setSelected(true);
                System.out.println("pip3 = " + Integer.toString(pos));
                notifyDataSetChanged();

            
        );
        convertView.setTag(Holder);
    
    else 

        Holder = (holder) convertView.getTag();
    
        Holder.bTr.setTag(position);
        Holder.bIng.setTag(position);
        Holder.resim.setTag(position);
        System.out.println("pip1 = " + position);

        if (list.get(position).isSelected()) 
            Holder.bTr.setVisibility(View.VISIBLE);
            Holder.bIng.setVisibility(View.GONE);
            notifyDataSetChanged();
         else 
            Holder.bTr.setVisibility(View.GONE);
            Holder.bIng.setVisibility(View.VISIBLE);
            notifyDataSetChanged();
        
        if (this.list.get(position).getResim()!=null)
        
            Holder.resim.setImageBitmap(this.list.get(position).getResim2());
        
        else
        
            Holder.resim.getLayoutParams().width=0;
        

        return convertView;
    
    

Imageview 中的图像来自数据库。如果是null,ImageView 正在缩小,如果不是,它会添加图像,但是当我滚动列表视图时,事情变得一团糟。

我做了很多努力,但我没有解决它。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:background="@drawable/satir_arkaplan"
android:orientation="horizontal"

android:padding="0dp">

<LinearLayout
    android:layout_
    android:layout_
    android:layout_gravity="center_vertical|center_horizontal"
    android:layout_weight="1"
    android:orientation="horizontal"
    android:padding="5dp">

    <ImageView
        android:id="@+id/resim"
        android:layout_
        android:layout_
        android:layout_gravity="center_vertical" />

</LinearLayout>


<LinearLayout
    android:layout_
    android:layout_
    android:layout_weight="1"
    android:baselineAligned="false"
    android:gravity="center_horizontal|center"
    android:orientation="vertical"
    android:padding="5dp">

    <Button
        android:id="@+id/bIng"
        android:layout_
        android:layout_
        android:background="@drawable/ing"
        android:textSize="9dp"
        android:layout_weight="1"/>

</LinearLayout>

<LinearLayout
    android:layout_
    android:layout_
    android:layout_gravity="center_horizontal|center"
    android:layout_weight="1"
    android:gravity="center_horizontal"
    android:orientation="horizontal"
    android:padding="5dp">

    <Button
        android:id="@+id/bTr"
        android:layout_
        android:layout_
        android:layout_gravity="center_horizontal|center"
        android:background="@drawable/turk"
        android:textSize="10dp" />

</LinearLayout>

【问题讨论】:

Ali bey, bu siteyi ilk defa kullanıyorum, nasıl iletişime geçicez, yorumlardan yazsam olur mu tamam, attım,,, 【参考方案1】:

试试这样的:

  if (this.list.get(position).getResim()!=null) 

            Holder.resim.setVisibility(View.Visible);                
            Holder.resim.setImageBitmap(this.list.get(position).getResim2());
        
        else 
            Holder.resim.setVisibility(View.Gone);
            Holder.resim.setImageresource(0);
        

【讨论】:

以上是关于在列表视图中显示或隐藏对象的主要内容,如果未能解决你的问题,请参考以下文章

隐藏或显示表格视图

如何在列表视图项中显示和隐藏视图?

如何在滚动列表视图上显示/隐藏底部导航视图?

更新列表视图中的单行以显示隐藏的文本视图

单击按钮,在主视图中的 foreach 语句中一次显示/隐藏部分视图结果

列表视图来自另一个列表视图中选择的对象的项目