分隔符在Android中的列表项之间丢失

时间:2011-02-24 12:31:11

标签: android android-layout

我使用自定义数组适配器来填充列表视图。我面临的问题是第四个和第五个列表项之间缺少分隔符。

以下是代码:

public class Clubs extends ListActivity{

    private static class EfficientAdapter extends BaseAdapter {
        private LayoutInflater mInflater;
        private Bitmap mIcon1;
        private Bitmap mIcon2;

        public EfficientAdapter(Context context) {
            // Cache the LayoutInflate to avoid asking for a new one each time.
            mInflater = LayoutInflater.from(context);

            // Icons bound to the rows.
            mIcon1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.yellow_offline);
            mIcon2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.green_online);
        }

        /**
         * The number of items in the list is determined by the number of speeches
         * in our array.
         *
         * @see android.widget.ListAdapter#getCount()
         */
        public int getCount() {
            return DATA.length;
        }

        /**
         * Since the data comes from an array, just returning the index is
         * sufficent to get at the data. If we were using a more complex data
         * structure, we would return whatever object represents one row in the
         * list.
         *
         * @see android.widget.ListAdapter#getItem(int)
         */
        public Object getItem(int position) {
            return position;
        }

        /**
         * Use the array index as a unique id.
         *
         * @see android.widget.ListAdapter#getItemId(int)
         */
        public long getItemId(int position) {
            return position;
        }

        /**
         * Make a view to hold each row.
         *
         * @see android.widget.ListAdapter#getView(int, android.view.View,
         *      android.view.ViewGroup)
         */
        public View getView(int position, View convertView, ViewGroup parent) {
            // A ViewHolder keeps references to children views to avoid unneccessary calls
            // to findViewById() on each row.
            ViewHolder holder;

            // When convertView is not null, we can reuse it directly, there is no need
            // to reinflate it. We only inflate a new View when the convertView supplied
            // by ListView is null.
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.people_list_item, null);

                // Creates a ViewHolder and store references to the two children views
                // we want to bind data to.
                holder = new ViewHolder();
                holder.text = (TextView) convertView.findViewById(R.id.textpeople);
                holder.icon = (ImageView) convertView.findViewById(R.id.image);

                convertView.setTag(holder);
            } else {
                // Get the ViewHolder back to get fast access to the TextView
                // and the ImageView.
                holder = (ViewHolder) convertView.getTag();
            }

            // Bind the data efficiently with the holder.
            holder.text.setText(DATA[position]);
            holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);

            return convertView;
        }

        static class ViewHolder {
            TextView text;
            ImageView icon;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new EfficientAdapter(this));
    }

    private static final String[] DATA = {
            "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam",
            "Abondance", "Ackawi", "Acorn", "Adelost", "Affidelice au Chablis",
            };
}

这是xml:

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

<LinearLayout
android:id="@+id/widget321"
android:layout_width="479dip"
android:layout_height="54dip"
android:layout_marginTop="16dip"
>

<ImageView
android:id="@+id/image"
android:layout_width="24dip"
android:layout_height="24dip"
android:layout_marginLeft="14dip"
android:layout_marginTop="5dip"
android:src="@drawable/green_online">
</ImageView>

<TextView
android:id="@+id/textpeople"
android:layout_marginLeft="10dip"
android:textColor="#FFF5EE" 
android:layout_height="29dip" 
android:textSize="16sp" 
android:layout_width="400dip" 
android:layout_marginTop="7dip"
>
</TextView>
</LinearLayout>
</LinearLayout>

以下是屏幕截图listseparator

有人可以告诉我哪里出错了吗? 感谢

修改
解决方案
这是为了让它正常工作而添加的代码

ListView lv=getListView();
 lv.setDividerHeight(2); 

1 个答案:

答案 0 :(得分:4)

简单的方法是在setDividerHeight(2)方法中调用onCreate。在更复杂的方式中,您需要确保您的资源正确放入layout-hdpi / layout中,因为正在发生的操作系统将近似1dpi到0dpi,因为应用程序在兼容模式下运行。另请查看清单中的supports-screens属性(尝试将所有内容设置为true)