繁体   English   中英

使用形状背景时,Android ListView截取了最后一项

[英]Android ListView chops off last item when using shape backgrounds

我有一个Android应用程序,它显示一个ListView,其中每个元素都有一个形状背景。 问题是ListView似乎切断了最后一个元素,不允许为形状背景留出空间...我在网上和这里(例如, 其他线程 )看了一下,但找不到答案。

这是一个示例,其中包含三个项目,分别显示“年龄”,“态度”和“主题”。 如您所见,似乎(大部分)文本已全部显示,但是listview的大小不允许形状背景。

底部切碎的ListView

listview将定义为:

<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"
/>

在上面,我也尝试过为wrap_content设置layout_height(没有区别),但是在这里我显示了一个固定的大小,希望这可以解决问题。 不。 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>

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

view.setBackgroundResource(R.drawable.listview_even);

实际上,奇数/偶数有不同的形状,但这并不成问题。 列表视图快速滚动, 如果滚动列表,则最后一项确实显示为完整 然后将最上面的项目切掉,因此问题似乎在于ListView的层次不正确。 当然,如果列表长于屏幕,则没有问题,因为无论如何您都必须滚动到末尾。 但是,如果内容占用的时间少于全屏显示的时间,则会出现我所显示的问题。

我尝试了其他几种高度设置,但无济于事,没有主意。 我认为这就是问题的总括---有任何建议吗? 形状背景真的是问题吗? 还是不考虑分频器?

更新:布局代码:

<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),而是列表视图本身的问题。

在此处输入图片说明

嗯,布局代码是(这是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 =谢谢! 我确实看到了那个,但是我已经尝试将wrap_content用于layout_height-没用

UPDATE2:似乎不考虑ListView中的DividerHeight的功能(分别在section3_author_list和section3_author_list_alpha中设置为7sp和8sp)。 对于一件商品没有问题,因为我增加了商品的数量,并且越来越多地被切碎。 如果我将DividerHeight设置为零,则不会被斩断。

在列表项上放置一个底边距是行不通的(我记得读过)。 但是好吧,问题在于ListView无法解决dividerHeight。 即使显示器确实显示了分隔线间隙,也没有解决-我猜是因为当我像这样更改ListViews时,将分隔线设置为android:divider="@null" ,它起作用了!

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

注意,分隔线现在设置为透明且不为null。 我希望所有这些对其他可能遇到同样事情的人有所帮助。

我将此提交为Bug: Android Bug Submit 在我看来,如果ListView要遵循dividerHeight间距而不考虑分隔符设置,那么总高度应该是正确的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM