隐藏列表视图行,但没有间距

时间:2015-05-28 06:30:54

标签: android listview row listviewitem

如果删除一个空格,即使我成功隐藏了一行,但它在那里显示了一个空格。当状态等于“被授予”时,它将隐藏一行'如下所示。提前谢谢你。

import android.app.Activity;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ListViewAdapter extends BaseAdapter {

    Activity context;
    String ref[];
    String policy[];
    String natureofloss[];
    String make[];
    String registration[];
    String modelname[];
    String year[];
    String amount[];
    String status[];
    String quot_id[];

    public ListViewAdapter(Activity context, String[] ref, String[] policy,
            String[] natureofloss, String[] make, String[] registration,
            String[] modelname, String[] year, String[] amount,
            String[] status, String[] qout_id) {
        super();
        this.context = context;
        this.ref = ref;
        this.policy = policy;
        this.natureofloss = natureofloss;
        this.make = make;
        this.registration = registration;
        this.modelname = modelname;
        this.year = year;
        this.amount = amount;
        this.status = status;
        this.quot_id = qout_id;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return ref.length;
    }

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

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

    private class ViewHolder {
        TextView ref;
        TextView policy;
        TextView natureofloss;
        TextView make;
        TextView registration;
        TextView modelname;
        TextView year;
        TextView amount;
        TextView status;
        TextView quot_id;

    }

    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder;
        LayoutInflater inflater = context.getLayoutInflater();

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.rowitem, null);
            holder = new ViewHolder();
            holder.ref = (TextView) convertView
                    .findViewById(R.id.tv_refpolicyno);
            holder.policy = (TextView) convertView
                    .findViewById(R.id.tv_policyno);
            holder.natureofloss = (TextView) convertView
                    .findViewById(R.id.tv_natureofloss);
            holder.make = (TextView) convertView.findViewById(R.id.tv_make);
            holder.registration = (TextView) convertView
                    .findViewById(R.id.tv_registration);
            holder.modelname = (TextView) convertView
                    .findViewById(R.id.tv_modelname);
            holder.year = (TextView) convertView.findViewById(R.id.tv_year);
            holder.amount = (TextView) convertView.findViewById(R.id.tv_amount);
            holder.status = (TextView) convertView.findViewById(R.id.tv_status);
            holder.quot_id = (TextView) convertView
                    .findViewById(R.id.tv_quotid_clientqoutlist);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (!status[position].equals("Awarded")) {
            holder.ref.setText(ref[position]);
            holder.policy.setText("/" + policy[position]);
            holder.natureofloss.setText(natureofloss[position]);
            holder.make.setText(make[position]);
            holder.registration.setText(registration[position]);
            holder.modelname.setText(modelname[position]);
            holder.year.setText(year[position]);
            holder.amount.setText(amount[position]);
            holder.status.setText(status[position]);
            holder.quot_id.setText(quot_id[position]);
        } else {
            convertView.setVisibility(View.GONE);

        }

        return convertView;
    }

}

行的屏幕截图

enter image description here

1 个答案:

答案 0 :(得分:1)

您需要创建适配器,该适配器将使用getCount()返回非空项目的总数,然后保持获取数据的位置。

例如你有一个列表作为位置1 - A,2 - B,3 - null,4 - D,5 - null

调用getCount时,返回3.

当时在位置1调用getView,返回列表[1]中的项目。位置3上的getView返回列表[4](因为第四个位置是非空的),依此类推。