【问题标题】:How to show an image button only on certain rows如何仅在某些行上显示图像按钮
【发布时间】:2014-04-19 04:07:54
【问题描述】:

我有一个带有自定义适配器的 ListView。我想根据列表中的条件设置每一行 ImageButton 的可见性。但是,这些行与我的设想不符。

在下面的示例中,我有一个名为ColorInfo 的类,其属性为count。每当计数大于 0 时,我都想显示图像。对于虚拟数据,我用 ColorInfo 的 20 个元素填充了数组,每个偶数项的计数都大于 0。但是,当我运行应用程序时,我没有在备用行上看到 ImageButton

下面是一个完整的例子:

DemoActivity

public class DemoActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ColorInfo[] clr= new ColorInfo[20];

        for(int i=0;i<20;i++){
            ColorInfo clrInfo = new ColorInfo();
            if (i%2 == 0) {
                clrInfo.count = 5;
            }
            clr[i] = clrInfo;
        }
        ((ListView)findViewById(R.id.list)).setAdapter(new MyAdapter(this, 0, clr));
    }

    private  class MyAdapter extends ArrayAdapter<ColorInfo>  {
        ViewHolder holder;
        LayoutInflater inflater;
        public MyAdapter(Context context, int textViewResourceId,ColorInfo[] objects) {
            super(context, textViewResourceId, objects);
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View itemView = convertView;

            final ColorInfo item = getItem(position);

            if(itemView == null){
                itemView = inflater.inflate(R.layout.row, null);
                holder = new ViewHolder();
                holder.editButton = (ImageButton) itemView.findViewById(R.id.some_button);
                itemView.setTag(holder);
            }
            else
                holder = (ViewHolder)itemView.getTag();

            if (item.count>0)
                holder.editButton.setVisibility(View.VISIBLE);

            return itemView;
        }
        private  class ViewHolder{
            ImageButton editButton;
        }
    }
    private static class ColorInfo{
        int count = 0;
    }
}

Main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="test"/>

    <ImageButton
        android:id="@+id/some_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#000000"
        android:visibility="gone"
        android:src="@drawable/ic_some_img"/>
</LinearLayout>

更新

我找到了答案here 显然,因为行被 ArrayAdapter 重用,所以有一个 else 条件也很好。

【问题讨论】:

    标签: android android-listview android-arrayadapter


    【解决方案1】:

    这是我如何在特定位置设置图像:

        @Override
                    public View getView(int position, View convertView, ViewGroup parent) {
    
                        View vi = convertView;
    
                        if (vi == null)
                            vi = inflater.inflate(R.layout.row, null);
    
    ImageView iv = (ImageView)vi.findViewById(R.id.someImageButton);
    
                        if (position == 0) {
    
    
    
                            iv.setVisibility(View.VISIBLE);
    
                        }
                        if (position == 1) {
    
                            //Something
    
                        }
                        if (position == 2) {
    
                            //Something
    
                        }
                        if (position == 3) {
    
    
    
                            iv.setVisibility(View.VISIBLE);
    
                        }
    
                        return vi;
                    }
                }
    

    也许这就是你要找的。​​p>

    【讨论】:

    • 所以我刚刚添加了一个else 条件,现在它正在工作。这个答案帮助了我:stackoverflow.com/a/4897545/3384340
    • 太完美了..我正要通过对可见性设置一些条件来编辑这个答案。感谢您的信息.. :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-01
    • 1970-01-01
    • 2021-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多