0

の最初の行をlistviewasに設定するには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;
    }

最初の行のすべての内容を非表示にしますが、空白は残します。助言がありますか?

4

2 に答える 2

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;
}
于 2013-11-14T13:14:28.840 に答える
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;
}
}

行.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>

スナップショット

ここに画像の説明を入力

于 2013-11-14T13:11:32.643 に答える