使用形状背景时,Android ListView会删除最后一项

时间:2015-09-23 19:47:52

标签: android listview

我有一个显示ListView的Android应用,其中每个元素都有一个形状背景。问题是ListView似乎砍掉了最后一个元素,没有为形状背景留出空间......我已经在网上找到了这里(例如other thread)但是找不到答案。

这是一个包含三个项目的例子,这些项目可以阅读“年龄”,“态度”和“#39;和'主题'。正如您所看到的,似乎(大部分)都显示了文本但是listview大小调整不允许形状背景。

ListView chopped at the bottom

列表视图定义为:

<ListView
        android:id="@+id/section3_topic_list"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:choiceMode="singleChoice"
        android:dividerHeight="7sp"
        android:divider="@null"
        android:fastScrollEnabled="true"
        android:layout_weight="0.8"
        android:layout_marginRight="10dp"
        />

项目布局简单:

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tv_item"
    android:layout_width="fill_parent"
    android:layout_height="44dp"
    android:gravity="center"
    android:textColor="@color/font_dark"
    android:layout_marginRight="10dp"
    android:layout_marginLeft="10dp"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:textSize="22dp"
/>

在上面,我也尝试过layout_hetent for layout_height(没有区别),但是这里我显示了一个固定的大小,希望这可以解决问题。没有按&#39;吨。 ListView高度被设置为wrap_content(但问题是它太小了,可悲的是)。

形状背景定义为

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<gradient
    android:startColor="#ffe4e8ff"
    android:endColor="#80f4fff0"
    android:angle="0"/>

<corners android:radius="8dp" />

</shape>

在List适配器中,我基本上看到了形状背景:

view.setBackgroundResource(R.drawable.listview_even);

实际上,奇数/偶数有不同的形状,但这不是问题。列表视图已快速滚动,如果滚动列表,最后一项显示为完整。然后顶部的项目被切断,所以问题似乎是ListView hieght没有正确计算。当然,如果列表比屏幕长,则没有问题,因此您必须滚动才能到达终点。但是,如果内容少于全屏,则会出现我正在显示的问题。

我已经尝试了其他几种高度设置的变化,但没有用,也没有想法;我认为这总结了问题 - 任何人都有建议吗?形状背景真的是问题吗?还是不考虑分隔线?

更新:布局代码:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/section3_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/cork2"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity$Section3Fragment"
>

<ScrollView ...
</ScrollView>

<RelativeLayout ...
</RelativeLayout>

<LinearLayout android:id="@+id/section3_l_s1_ll"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_below="@+id/section3_l_list"
    >

    <ListView
        android:id="@+id/section3_topic_list"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:choiceMode="singleChoice"
        android:dividerHeight="7sp"
        android:divider="@null"
        android:fastScrollEnabled="true"
        android:layout_weight="0.8"
        android:layout_marginRight="10dp"
        />

    <ListView
        android:id="@+id/section3_topic_list_alpha"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:dividerHeight="8sp"
        android:divider="@null"
        android:layout_weight="0.2"
        android:layout_marginLeft="34dp"
        android:layout_gravity="center_horizontal"
        />
</LinearLayout>

<LinearLayout  android:id="@+id/section3_l_s2_ll"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_below="@+id/section3_l_list"
    android:visibility="gone"
    >

    <ListView
        android:id="@+id/section3_author_list"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:choiceMode="singleChoice"
        android:dividerHeight="7sp"
        android:divider="@null"
        android:fastScrollEnabled="true"
        android:layout_weight="0.8"
        android:layout_marginRight="10dp"
        />

    <ListView
        android:id="@+id/section3_author_list_alpha"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:dividerHeight="8sp"
        android:divider="@null"
        android:layout_weight="0.2"
        android:layout_marginLeft="34dp"
        android:layout_gravity="center_horizontal"
        />
</LinearLayout>

由于我们正在深入研究这个问题,因此这里有一个更新的图片,其中显示了section3_author_list和section3_author_list_alpha。 alpha列表用于在此字母大小时跳转到另一个字母,但这并不重要。请注意,两者都在底部被切断,这让我觉得问题不是列表视图所在的容器的整体大小(这里是一个水平的LinearLayout),而是列表视图本身的东西。

enter image description here

啊,布局代码是(这是section3_author_list的部分 - 类似于section3_author_list_alpha - 未显示):

    final ListView lv_main = (ListView) rootView.findViewById(resource_main);
    final ListView lv_alpha = (ListView) rootView.findViewById(resource_alpha);

    // list handling for the main
    final Section3StableArrayAdapter topicAdapter = new Section3StableArrayAdapter(context, R.layout.listview_custom, list_main);
    lv_main.setAdapter(topicAdapter);
 ...

  public static class Section3StableArrayAdapter extends ArrayAdapter<String> {

    private int mAuthorCurrentIndex = -1;
    private int mLastGetViewPosition = -1;

    HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

    public Section3StableArrayAdapter(Context context, int textViewResourceId, List<String> objects) {
        super(context, textViewResourceId, objects);
        for (int i = 0; i < objects.size(); ++i) {
            mIdMap.put(objects.get(i), i);
        }
    }

    @Override
    public long getItemId(int position) {
        String item = getItem(position);
        return mIdMap.get(item);
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);

        mLastGetViewPosition = position;

        //  Drawable shape = mContext.getResources().getDrawable(R.drawable.listview_even);
        if(position == mAuthorCurrentIndex)
        {
            view.setBackgroundResource(R.drawable.listview_selected);
        }
        else {
            if (position % 2 == 0)
                view.setBackgroundResource(R.drawable.listview_even);
            else
                view.setBackgroundResource(R.drawable.listview_odd);
        }

        return view;
    }


    public void setSelectedIndex(int k)
    {
        mAuthorCurrentIndex = k;
    }

    /*
    public int lastViewPosition()
    {
        return mLastGetViewPosition;
    }
    */
}

@cybersam =谢谢!我确实看到了那个,但我已经尝试过layout_content for layout_height - 没有工作

UPDATE2:它似乎是不考虑ListView中的dividerHeight的功能(分别设置为section3_author_list和section3_author_list_alpha中的7sp和8sp)。对于一个项目没有问题,因为我增加的项目数量越来越多被切断。如果我将dividerHeight设置为零,那么没有任何东西被切断。

1 个答案:

答案 0 :(得分:0)

在列表项上放置一个下边距不起作用(我记得读过这个)。但好的,问题是ListView没有考虑dividerHeight。即使显示器确实显示了分隔器间隙,但它没有被考虑 - 我猜是因为当我像这样更改ListViews时,我将分隔符设置为 //MARK: Realm Migrations Realm.Configuration.defaultConfiguration = Realm.Configuration( // bump the schema version to 2 schemaVersion: 2, migrationBlock: { migration, oldSchemaVersion in migration.enumerate(Organization.className()) { oldObject, newObject in // make sure to check the version accordingly if (oldSchemaVersion < 2) { // the magic happens here: `id` is the property you specified // as your primary key on your Model newObject!["primaryKeyProperty"] = "id" } } } ) ,它可以工作!

android:divider="@null"

请注意,分隔符现在设置为透明而不是null。我希望这一切能帮助那些可能遇到同样事情的人。

我将此提交为错误:Android Bug Submit。在我看来,如果ListView将尊重dividerHeight间距,无论分隔符设置如何,那么总高度应该是正确的。