自定义listview的最后一行

时间:2012-06-25 08:53:26

标签: android listview customization

我有一个ListView,其中包含1个图像+ 2个文本+ 1个图像。这是我的listview的xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_height="20dp"
   android:gravity="left|center"
   android:layout_width="wrap_content"
   android:paddingLeft="5dp"
   android:paddingRight="5dp"
   android:background="@color/list_bg"
   >

    <ImageView
       android:id="@+id/avatar"
       android:layout_width="wrap_content"
       android:layout_height="48dp"
       android:layout_marginTop="10dp"
       android:layout_alignParentLeft="true"/>

    <LinearLayout
       android:orientation="vertical"
       android:layout_width="wrap_content"
       android:layout_height="48dp"
       android:layout_toRightOf="@+id/avatar"
       android:layout_marginLeft="5dp"
       android:layout_marginTop="5dp"
       >


    <TextView android:id="@+id/listitem1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginTop="2dp"
      android:textColor="@drawable/textblack_selected"
      android:textSize="18dp"
      android:textStyle="bold" 
      />
    <TextView android:id="@+id/listitem2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:textColor="@drawable/textorange_selected"
      />

    </LinearLayout>

    <ImageView
       android:id="@+id/arrowImage"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginTop="25dp"
       android:layout_alignParentRight="true"
       android:src="@drawable/arrow_selector"/>
</RelativeLayout>

我需要从最后一行删除最后一张图片(箭头)。在我的适配器中,我尝试使用它:

if (position == 4) lastimg.setImageResource(0);

but nothing changed.Here is my adapter:

public class UserItemAdapter extends ArrayAdapter < UserRecord > {
    private ArrayList < UserRecord > users;

    public UserItemAdapter(Context context, int textViewResourceId, ArrayList < UserRecord > users) {
        super(context, textViewResourceId, users);
        this.users = users;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.mainmenurowlist, null);
        }

        UserRecord user = users.get(position);
        if (user != null) {
            TextView listitem1 = (TextView) v.findViewById(R.id.listitem1);
            TextView listitem2 = (TextView) v.findViewById(R.id.listitem2);

            if (listitem1 != null) {
                listitem1.setText(user.listitem1);
            }

            if (listitem2 != null) {
                listitem2.setText(user.listitem2);
            }
        }

        firstimg = (ImageView) v.findViewById(R.id.avatar);
        lastimg = (ImageView) v.findViewById(R.id.arrowImage);

        firstimg.setImageResource(FirstImage[position]);
        if (position == 4) {
            lastimg.setImageResource(0);
            System.out.println("Exit!!");
        }
        return v;
    }
}

public class UserRecord {
    public String listitem1;
    public String listitem2;

    public UserRecord(String listitem1, String listitem2) {
        this.listitem1 = listitem1;
        this.listitem2 = listitem2;
    }
}
} 

有谁知道如何解决这个问题?提前谢谢。

2 个答案:

答案 0 :(得分:2)

试试这个

if(position == (data_id.size()-1))
{
  lastimg.setVisibility(View.INVISIBLE);
}

这里data_id只是一个数组列表

或尝试如下

if(position == 4)
{
  lastimg.setVisibility(View.INVISIBLE);
}

两者都是相同的

答案 1 :(得分:0)

在getView()方法中使用这种方式

if((users.size()-1)==position){
   // do here your stuff what u want on last item of listview
 }else{
  // do here your stuff what u want on each item of listview except last item
 }