Android 适配器行隐藏

发布于 2024-11-27 08:02:55 字数 508 浏览 7 评论 0原文

在 arrayAdptor 中,我们使用以下代码:

 final LayoutInflater inflater = activity.getLayoutInflater();
    row = (LinearLayoutCustom) inflater.inflate(R.layout.row, null);
    final TextView label = (TextView) row.findViewById(R.id.title);
    label.setText(position + "" + items[position]); 
    return row;

现在假设某些值为 null (例如在位置 2 , items[2] = null ),所以我不想在行中显示它。我想隐藏它。如果我使用

               row.setVisibility(View.GONE) 

它,则会在这一行留下我不想要的空白。那我该怎么办呢?

In arrayAdptor we use following code:

 final LayoutInflater inflater = activity.getLayoutInflater();
    row = (LinearLayoutCustom) inflater.inflate(R.layout.row, null);
    final TextView label = (TextView) row.findViewById(R.id.title);
    label.setText(position + "" + items[position]); 
    return row;

Now suppose some value are null (for example at position 2 , items[2] = null ) so i dont want to show it in row. i want to hide it. if i use

               row.setVisibility(View.GONE) 

it leaves a blank space at this row which i dont want. so what should i do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

第几種人 2024-12-04 08:02:55

AFAIK,您不能从 getView 返回空视图,但您可以使视图不可见且高度为 1。尽管使用 getCount 进行操作可能是首选方式。

view.setVisibility(View.INVISIBLE);
view.getLayoutParams().height = 1;

AFAIK you can't return a null view from getView, but you could just make the view invisible and height 1. Although manipulating using the getCount is probably the preferred way.

view.setVisibility(View.INVISIBLE);
view.getLayoutParams().height = 1;
岁月静好 2024-12-04 08:02:55

您需要让适配器使用 getCount 返回非空项的总数,然后保留位置到内部数据结构的映射。

例如。
您有一个列表

1 - John
2 - null
3 - Bill
4 - Susan
5 - null

,当调用 getCount 时,它返回 3。

然后,当在位置 1 上调用 getView 时,您将返回 list[1] 处的项目。
位置 2 上的 getView 返回 list[3] (因为它是第二个非空),
等等。

这是我发现的唯一方法。

You'll need to have the adapter return the total number of non-null items with getCount and then keep a mapping of position to your internal data structure.

For example.
You have a list

1 - John
2 - null
3 - Bill
4 - Susan
5 - null

When getCount is called it returns 3.

Then when getView is called on position 1 you return the item at list[1].
getView on position 2 returns list[3] (as it's the 2nd non-null),
and so forth.

This is the only way I've found to do this.

花开柳相依 2024-12-04 08:02:55

您可以使用没有“隐藏”项目高度的视图,这样您就不必执行所有模型管理和映射。例如,假设您有一个“过滤器”EditText 字段,当输入数据时,它仅保留匹配的项目:

public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater   inflater = (LayoutInflater) MyActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    RelativeLayout view = (RelativeLayout) inflater.inflate(R.id.myListLayout, null, false);

    ...

    // if we didn't match filter be GONE and leave
    if (filterText.length() > 0 && myModelValueAtPosition.toLowerCase().indexOf(filterText) < 0){
        view = (RelativeLayout) inflater.inflate(R.layout.myListLayoutWithZeroHeight, null, false);
        view.setVisibility(View.GONE); // this doesn't really do anything useful; I'd hoped it would work by itself, but turns out the zero height layout is the key
        return view;
    }
    view.setVisibility(View.VISIBLE);

    ...
}

You can use a View that has no height for the "hidden" items so that you don't have to do all the model housekeeping and mapping. For example, suppose you had a "filter" EditText field that when data is entered it only keeps matching items:

public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater   inflater = (LayoutInflater) MyActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    RelativeLayout view = (RelativeLayout) inflater.inflate(R.id.myListLayout, null, false);

    ...

    // if we didn't match filter be GONE and leave
    if (filterText.length() > 0 && myModelValueAtPosition.toLowerCase().indexOf(filterText) < 0){
        view = (RelativeLayout) inflater.inflate(R.layout.myListLayoutWithZeroHeight, null, false);
        view.setVisibility(View.GONE); // this doesn't really do anything useful; I'd hoped it would work by itself, but turns out the zero height layout is the key
        return view;
    }
    view.setVisibility(View.VISIBLE);

    ...
}
欢你一世 2024-12-04 08:02:55

在这里,您需要在 getCount()< 中编写逻辑/a>,getItemId() 和 <一个href="http://developer.android.com/reference/android/widget/Adapter.html#getItem%28int%29" rel="nofollow">getItem(),
它将创建 getCount 返回的行数

//How many items are in the data set represented by this Adapter
    public int getCount() {
            return //Should return the count of rows you need to display (here the count excluding null values)
        }

//This need to return data item associated with the specified position in the data set.
public Object getItem(int position) {
        return //Return the object need to display at position, need the logic to skip null value  
    }

编辑:所以在您的 getview 中

 public View getView(int position, View convertView, ViewGroup parent) { 
         ---- 
        getItem(position);//Object corresponding to position ,In your case it will not be null since you need to write the logic to skip null object at getItem
        ----
}

Here you need to write the logic in your getCount(),getItemId() and getItem(),
It will create the no of rows what the getCount return

//How many items are in the data set represented by this Adapter
    public int getCount() {
            return //Should return the count of rows you need to display (here the count excluding null values)
        }

And

//This need to return data item associated with the specified position in the data set.
public Object getItem(int position) {
        return //Return the object need to display at position, need the logic to skip null value  
    }

Edit:So in your getview

 public View getView(int position, View convertView, ViewGroup parent) { 
         ---- 
        getItem(position);//Object corresponding to position ,In your case it will not be null since you need to write the logic to skip null object at getItem
        ----
}
笙痞 2024-12-04 08:02:55

这是我实现的解决方案,这里有一个代码示例供每个正在寻找它的人使用:

class Shipment_Adapter extends ArrayAdapter<Shipment>{
     ....
     ArrayList<Integer> emptyPositions = new ArrayList<>();

     public Shipment_Adapter(Context context, int shipment_row, Shipment[] myShipments){
        super(context, R.layout.shipment_row,myShipments);

        //populate emptyPositions list
        for(int i = 0; i< myShipments.length; i++){
            if(myShipments[i]==null){
                emptyPositions.add(i);
            }
        }

        this.mShipment = myShipments;
        this.mContext = context;
    }

    //corrects size of List
    @Override
    public int getCount() {
        return (mShipment.length - emptyPositions.size());
    }

    //recursive function that checks if position is not empty until it isn't
    public int isEmpty(int positiontocheck){
         int newposition;
         if(emptyPositions.contains(positiontocheck)){
             //true? check that next one is free
            return isEmpty(positiontocheck+1);
         }else{
            newposition = positiontocheck;
         }

         return newposition;
     }
}

public View getView(int position, View convertView, ViewGroup parent) {

    //now just need to use isEmpty to get the next not empty position in 
    //case our real position is empty

    position= isEmpty(position);

    Shipment shipment = mShipment[position];

    ...//and so on

 }

希望这会有所帮助!

This is the solution I implemented, here is a code example for everyone that is looking it:

class Shipment_Adapter extends ArrayAdapter<Shipment>{
     ....
     ArrayList<Integer> emptyPositions = new ArrayList<>();

     public Shipment_Adapter(Context context, int shipment_row, Shipment[] myShipments){
        super(context, R.layout.shipment_row,myShipments);

        //populate emptyPositions list
        for(int i = 0; i< myShipments.length; i++){
            if(myShipments[i]==null){
                emptyPositions.add(i);
            }
        }

        this.mShipment = myShipments;
        this.mContext = context;
    }

    //corrects size of List
    @Override
    public int getCount() {
        return (mShipment.length - emptyPositions.size());
    }

    //recursive function that checks if position is not empty until it isn't
    public int isEmpty(int positiontocheck){
         int newposition;
         if(emptyPositions.contains(positiontocheck)){
             //true? check that next one is free
            return isEmpty(positiontocheck+1);
         }else{
            newposition = positiontocheck;
         }

         return newposition;
     }
}

public View getView(int position, View convertView, ViewGroup parent) {

    //now just need to use isEmpty to get the next not empty position in 
    //case our real position is empty

    position= isEmpty(position);

    Shipment shipment = mShipment[position];

    ...//and so on

 }

hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文