3

我试图隐藏自定义列表适配器中的项目。我可以隐藏文本的可见性,但我无法隐藏整个列表项。它仍然显示分隔线等。我尝试过:

tv.setVisibility(View.INVISIBLE);
tv.setVisibility(View.GONE);
convertView.setVisibility(View.INVISIBLE);
convertView.setVisibility(View.GONE);

当我使用 convertView 时,我得到一个空指针异常。

4

7 回答 7

11
  1. 您可以使用无元素设置 ContentView。

    在您的自定义适配器的 getView() 中。

    if(condition)
    {
      convertView=layoutInflater.inflate(R.layout.row_null,null);
      return convertView;
    }
    else
    {
       convertView=layoutInflater.inflate(R.layout.row_content,null);
       return convertView;
    }
    
  2. 你的 XML row_null.xml

    <?xml version="1.0" encoding="utf-8"?>
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="match_parent"
         android:layout_height="wrap_content">
    </LinearLayout>
    
于 2013-12-16T12:33:59.780 回答
1

你有 3 种方法可以做到这一点:

  1. 从适配器内部或外部的列表中删除项目。在适配器内部,您可以在构造函数中执行此操作。

    private List<String> list; public MyAdapter(Context context, List<String> list) { list.remove(0); list.remove(1); list.remove(<whateverPositionYouLike>); this.list = list; }

  2. 您需要弄清楚要隐藏多少项并需要构建类似的逻辑。

@Override public int getCount() { // In this adapter, we are supposed to hide the first item of the list view // Returning 0 if no of items are already 0 if(list.size() <=1) return 0; // if number of items are more than one, returning one item less return list.size() - 1; }

@Override public Object getItem(int position) { // skipping the position return list.get(position + 1); }

`

 @Override
    public View getView(final int position, View v, ViewGroup arg2) {

    // this is important, as we are supposed to skip the first item of listview
    final int localPosition = position +1;

    ViewHolderItem holder;
    if (v == null) {
        holder = new ViewHolderItem();
        v = inflater.inflate(R.layout.list_row, null);
        v.setTag(holder);
    } else {
        holder = (ViewHolderItem) v.getTag();
    }
    return v;
    }

`

  1. @hims_3009 提供的答案
于 2014-12-19T10:33:13.133 回答
0

我认为我有一个更简单/更安全的解决方案:您只需将您的项目“嵌入”到布局中,并更改此父布局的可见性。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <!-- Embed ListView Item into a "parent" Layout -->
    <LinearLayout
        android:id="@+id/parentLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <!-- This is the normal content of your ListView Item -->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="World" />

    </LinearLayout>
</LinearLayout>

然后在您的代码中执行以下操作:

public View getView(int position, View view, ViewGroup parent) {
    if (view == null) {
        LayoutInflater li = mActivity.getLayoutInflater();
        view = li.inflate(R.layout.my_listview_item, null);
    }
    LinearLayout parentLayout = (LinearLayout) view.findViewById(R.id.parentLayout);
    if (shouldDisplayItem(position)) {
        parentLayout.setVisibility(View.VISIBLE);
    } else {
        parentLayout.setVisibility(View.GONE);
    }
    return view;
}

这样你总是使用/重用同一个项目,只是隐藏/显示它。

于 2014-05-13T14:03:11.627 回答
0

您不能以您尝试的方式,您需要使用自定义适配器并在其中实现显示/不显示线条的逻辑。

于 2013-03-10T15:47:03.190 回答
0

如果要在列表视图中隐藏一行,则需要删除该位置的数据。例如,如果您使用数组适配器并希望隐藏 5. 位置上的行。您必须从数组中删除行并调用 notifyDatasetChanged() 方法。

(您可以使用 tempArray 从数组中删除数据)

或使用这种方式

private List<String> items = new ArrayList<String>();    
// make list a new array, clearing out all old values in it.
for(i=0; i<10; i++)
{
  if((i != 5)  && (i != 6)){        
   // if k = 5 or 6, dont add those items to the list
     items.add(something[i]);   /// whatever your list items are.
   }
}

ArrayAdapter<String> itemList = new
ArrayAdapter<String>(this,R.layout.itemlistlayout);
setListAdapter(itemlist); 
于 2013-03-10T15:54:25.957 回答
0

如果要隐藏整个项目,则需要在 getView() 方法中构建一些逻辑,以使其跳过数据的各个部分。

假设您有一个 ArrayAdapter,我想隐藏索引 2 处的数据。您的 getView() 方法可能看起来像这样。

@Override
public View getView(int pos, View convertView, ViewGroup, parent){
    View mView = convertView;
    //TODO: Check if convertView was null, and inflate
    //      or instantiate if needed.

    //Now we are going to set the data
    mTxt = mView.findViewById(R.id.mTxt);
    if(pos >= 2){
        //If position is 2 or above, we ignore it and take the next item.
        mTxt.setText(this.getItem(pos + 1).toString()); 
    }else{
        //If position is below 2 then we take the current item.
        mTxt.setText(this.getItem(pos).toString());
    }
    return mView;
}

请注意,此示例是通用的,并不意味着可以直接放入您的项目中。我不得不对一些我不知道真相的事情做出假设。您可以使用与我在这里展示的相同的概念,并根据您的情况对其进行修改,以便能够“隐藏”ListView 中的行。

于 2013-03-10T15:55:12.140 回答
0

如果您已经有一个自定义列表适配器,您只需在适配器上调用 notifyDataSetChanged() ,适配器本身当然可以实现逻辑来过滤掉您想要隐藏的行的视图。这些是我想到的需要反映该逻辑的方法:

    @Override
    public Object getItem(int position) {
        return mItems.get(position);
    }

    @Override
    public int getCount() {
        return mItems.size();
    }

    @Override
    public View getView(final int position, final View convertView, final ViewGroup parent) {
        // here goes your logic to hide a row

此外,您可能还必须更改 getItemId() 。

于 2013-03-10T15:57:01.957 回答