如何将listview的第一行设置为不可见?

时间:2013-11-14 13:10:52

标签: android

如何将listview的第一行设为invisible

适配器' getView

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        if(position == 0)
            convertView.setVisibility(View.INVISIBLE);
        else
            convertView.setVisibility(View.VISIBLE);
         return convertView;
    }

隐藏第一行的所有内容但留下空格。 有什么建议吗?

2 个答案:

答案 0 :(得分:1)

您可以过滤项目并将其显示在列表视图中,而不是使其invisiblegone

实施自定义过滤器。

编辑:

使用以下链接中的代码。它的工作原理

http://www.sherif.mobi/2012/01/listview-with-ability-to-hide-rows.html

示例:隐藏第3行

MainActivity.java

public class MainActivity extends Activity {

    ListView lv;
    String items[]={"A","B","C","D","E"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView) findViewById(R.id.listView1);
        CustomAdapter cus = new CustomAdapter(this,items);
        cus.hide(2);// hide letter c
        lv.setAdapter(cus);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    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" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

    </ListView>

</RelativeLayout>

CustomAdapter.java

class CustomAdapter extends BaseAdapter
{
    LayoutInflater inflater;
    String items[];
    boolean[] hidden = null;
    public CustomAdapter(MainActivity mainActivity, String[] items) {
        // TODO Auto-generated constructor stub
        inflater = LayoutInflater.from(mainActivity);
        this.items=items;
        hidden = new boolean[items.length]; 
        for (int i = 0; i < items.length; i++)
               hidden[i] = false;
    }
    private int getRealPosition(int position) {
         int hElements = getHiddenCountUpTo(position);
         int diff = 0;
         for(int i=0;i<hElements;i++) {
          diff++;
          if(hidden[position+diff])
           i--;
         }
         return (position + diff);
        }
        private int getHiddenCountUpTo(int location) {
         int count = 0;
         for(int i=0;i<=location;i++) {
          if(hidden[i])
           count++;
         }
         return count;
        }

    public void hide(int position) {
         hidden[getRealPosition(position)] = true;
         notifyDataSetChanged();
         notifyDataSetInvalidated();
        }
        public void unHide(int position) {
         hidden[getRealPosition(position)] = false;
         notifyDataSetChanged();
         notifyDataSetInvalidated();
        }
    @Override
    public int getCount() {
     return (items.length - getHiddenCount());
    }

    private int getHiddenCount() {
     int count = 0;
     for(int i=0;i<items.length;i++)
      if(hidden[i])
       count++;
     return count;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder; 

        int pos = getRealPosition(position);
        if (convertView == null) { 
            convertView = inflater.inflate(R.layout.row, parent, false);
            holder = new ViewHolder(); 
            holder.tv = (TextView) convertView.findViewById(R.id.textView1); 
            holder.tv2 = (TextView) convertView.findViewById(R.id.textView2); 
           convertView.setTag(holder); 
       } else { 
           holder = (ViewHolder) convertView.getTag(); 
       } 
        holder.tv.setText("Postion at"+pos); 
        holder.tv2.setText("Item is "+items[pos]);
        return convertView;
    }
static class ViewHolder
{
    TextView tv,tv2;
}
}

row.xml

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="18dp"
        android:layout_marginTop="24dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView1"
        android:layout_alignBottom="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:text="TextView" />

</RelativeLayout>

快照

enter image description here

答案 1 :(得分:1)

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    if(position == 0)
        convertView.setVisibility(View.GONE);
    else
        convertView.setVisibility(View.VISIBLE);
     return convertView;
}