将列表视图中的最后一项放在底部

3

我正在开发一个侧边菜单列表视图,每个项目都是这个xml文件"rbm_item.xml":

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/rbm_item_height"
android:orientation="horizontal"
android:paddingTop="@dimen/rbm_item_padding_topbottom"
android:paddingBottom="@dimen/rbm_item_padding_topbottom"
android:paddingLeft="@dimen/rbm_item_padding_leftright"
android:paddingRight="@dimen/rbm_item_padding_leftright"    >

<ImageView
    android:id="@+id/rbm_item_icon"
    android:layout_height="@dimen/rbm_item_image_height"
    android:layout_width="@dimen/rbm_item_image_width"
    android:scaleType="fitCenter"
     />

<TextView         
    android:id="@+id/rbm_item_text"
    android:layout_width="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_height="wrap_content"
    android:textSize="@dimen/rbm_item_text_size"
    android:textColor="@color/rbm_item_text_color"
    android:paddingLeft="@dimen/rbm_item_text_padding_left"
    android:paddingTop="@dimen/rbm_item_text_padding_top"
    android:singleLine="true"
    android:ellipsize="end"/>

<TextView         
    android:id="@+id/rbm_item_text_sub"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/rbm_item_text"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:textSize="@dimen/rbm_item_text_sub_size"
    android:textColor="@color/rbm_item_text_sub_color"
    android:paddingLeft="@dimen/rbm_item_text_sub_padding_left"
    android:singleLine="true"
    android:ellipsize="end"/>

我为这个ListView的项目制作了一个适配器:
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(
                R.layout.rbm_item, null);
        }

        ImageView icon = (ImageView) convertView.findViewById(R.id.rbm_item_icon);
        icon.setImageResource(getItem(position).iconRes);

        TextView title = (TextView) convertView.findViewById(R.id.rbm_item_text);
        title.setText(getItem(position).tag);

        TextView Description = (TextView) convertView
            .findViewById(R.id.rbm_item_text_sub);


        return convertView;
    }

而这就是列表XML文件 "list.xml":

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@color/rbm_divider_color"
android:dividerHeight="@dimen/rbm_divider_ht"
android:paddingLeft="@dimen/list_padding"
android:paddingRight="@dimen/list_padding" />

而且onCreateView函数:

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    return inflater.inflate(R.layout.list, null);
    }

我通过这个函数动态地将项目添加到适配器adapter.add(...)中:

    public void parseXml(int menu) {
    try {
        adapter = new SampleAdapter(getActivity());
        XmlResourceParser xpp = getResources().getXml(menu);
        xpp.next();
        int eventType = xpp.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG) {
                String elemName = xpp.getName();
                if (elemName.equals("item")) {
                    String textId = xpp.getAttributeValue(
                        "http://schemas.android.com/apk/res/android", "title");
                    String iconId = xpp.getAttributeValue(
                        "http://schemas.android.com/apk/res/android", "icon");
                    String resId = xpp.getAttributeValue(
                        "http://schemas.android.com/apk/res/android", "id");
                    String Desc = MainActivity.getItemDescription(resId);

                    //Log.i("", "parsing :" + Desc);
                    this.adapter.add(new SampleItem(textId, Integer.valueOf(iconId
                        .replace("@", "")), Desc));

                }

            }

            eventType = xpp.next();

        }

    } catch (Exception e) {
        Log.e("log", "while parsing xml", e);
    }

    setListAdapter(adapter);
}

而SampleItem是一个简单的结构体,包含图标、标题等属性。Menu参数是包含在<menu>标签内的xml文件,我对其中的项目进行了处理,每个项目都类似于:

  <item android:id="@+id/ribbon_menu_profile" android:title="Profile" 
     android:icon="@drawable/profile"></item>
 .....

我希望的是将这些项目中的最后一个项目放在列表底部。
我已经搜索了很多,但如果我要通过getView函数中的convertView参数来设置它,我没有找到任何View类方法可以做到这一点。
那么有没有什么办法可以做到呢?
所以我想要像这样的东西:

enter image description here


你的ListView和Adapter实现在哪里? - Steve Benett
@SteveBenett编辑了!这样就够了吗? - walid
@walid 所以你想要将你的列表按照相反的顺序排序?这正确吗? - malimo
不,我只想把这些项目中的最后一个放在列表视图布局的底部,并将其余部分保留在顶部。 - walid
它现在与其他项目一起在顶部,我希望它在底部。 - walid
显示剩余2条评论
2个回答

0
你可以这样做:将你的列表放在一个RelativeLayout中,并在下面添加一个额外的项目。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lastStickyItem"    
android:layout_width="match_parent"
android:layout_height="@dimen/rbm_item_height"
android:orientation="horizontal"
android:paddingTop="@dimen/rbm_item_padding_topbottom"
android:paddingBottom="@dimen/rbm_item_padding_topbottom"
android:paddingLeft="@dimen/rbm_item_padding_leftright"
android:paddingRight="@dimen/rbm_item_padding_leftright"    >

<ImageView
android:id="@+id/rbm_item_icon"
android:layout_height="@dimen/rbm_item_image_height"
android:layout_width="@dimen/rbm_item_image_width"
android:scaleType="fitCenter"
 />

<TextView         
android:id="@+id/rbm_item_text"
android:layout_width="wrap_content"
android:layout_alignParentTop="true"
android:layout_height="wrap_content"
android:textSize="@dimen/rbm_item_text_size"
android:textColor="@color/rbm_item_text_color"
android:paddingLeft="@dimen/rbm_item_text_padding_left"
android:paddingTop="@dimen/rbm_item_text_padding_top"
android:singleLine="true"
android:ellipsize="end"/>

<TextView         
android:id="@+id/rbm_item_text_sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/rbm_item_text"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:textSize="@dimen/rbm_item_text_sub_size"
android:textColor="@color/rbm_item_text_sub_color"
android:paddingLeft="@dimen/rbm_item_text_sub_padding_left"
android:singleLine="true"
android:ellipsize="end"/>
</RelativeLayout>

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/lastStickyItem"
android:divider="@color/rbm_divider_color"
android:dividerHeight="@dimen/rbm_divider_ht"
android:paddingLeft="@dimen/list_padding"
android:paddingRight="@dimen/list_padding" />

</RelativeLayout>

在您的活动中填充额外(最后)的项目,而不是适配器...


0

尝试使用listview.addFooterView(view);


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接