方法getView不会在Android上的ListView中调用

时间:2015-10-15 19:33:42

标签: android listview android-fragments listadapter

为什么getView不会被大小列表调用?我有2个片段。我用ViewPager将它放在标签中。在第一个片段中,getView被调用了4次,对应于列表大小(list有4个子)。但是在第二个片段中,getView被调用了2次,应该被调用4次(list有4个孩子)。

片段1

 @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootview = inflater.inflate(R.layout.fragment_sev, container, false);
    list = (ListView) rootview.findViewById(R.id.list);
    list.setVisibility(View.INVISIBLE);

    if (!MainActivity.activOutlet.equals("kosong")) {
        list.setVisibility(View.VISIBLE);
        mAdapter = new OrderSEVAdapter(getActivity(), outlets.get(idOutlet).getOrderSEVList(), idOutlet, getContext());
        list.setAdapter(mAdapter);
    }
    return rootview;
}

public void refresh(int idOutlet, List<outlet> outlets) {
    if (MainActivity.activOutlet.equals("kosong")) {
    MainActivity.activOutlet = idOutlet + "";
    this.outlets = outlets;
    this.idOutlet = idOutlet;
    mAdapter = new OrderSEVAdapter(getActivity(), outlets.get(idOutlet).getOrderSEVList(), idOutlet, getContext());
    mAdapter.notifyDataSetChanged();
    list.setAdapter(mAdapter);
}

适配器列表1:

 public OrderSEVAdapter(Activity activity, List<orderSEV> items, int id_outlet, Context context) {
    this.activity = activity;
    this.itemss = items;
    this.id_outlet = id_outlet;
    this.context = context;
}

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

@Override
public Object getItem(int location) {
    return itemss.get(location);
}

@Override
public long getItemId(int position) {
    return position;
}

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

    if (inflater == null) {
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.item_ordersev, null);
    }
    TextView title = (TextView) convertView.findViewById(R.id.title);
    TextView total = (TextView) convertView.findViewById(R.id.total);
    EditText unit = (EditText) convertView.findViewById(R.id.edunit);

    return convertView;
}

片段2:

 @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootview = inflater.inflate(R.layout.fragment_cekoutlet, container, false);
    list = (ListView) rootview.findViewById(R.id.list);
    list.setVisibility(View.INVISIBLE);

    if (!MainActivity.activOutlet.equals("kosong")) {
        list.setVisibility(View.VISIBLE);
        mAdapter = new CekOutletAdapter(getActivity(), outlets.get(idOutlet).getCekOutletList(), idOutlet, getContext());
        list.setAdapter(mAdapter);
    }
    return rootview;
}

public void refresh(int idOutlet, List<outlet> outlets) {
    if (MainActivity.activOutlet.equals("kosong")) {
    MainActivity.activOutlet = idOutlet + "";
    this.outlets = outlets;
    this.idOutlet = idOutlet;
    mAdapter = new CekOutletAdapter(getActivity(), outlets.get(idOutlet).getCekOutletList(), idOutlet, getContext());
    mAdapter.notifyDataSetChanged();
    list.setAdapter(mAdapter);
}

适配器列表2:

public CekOutletAdapter(Activity activity, List<cekOutlet> items, int id_outlet, Context context) {
    this.activity = activity;
    this.itemss = items;
    this.id_outlet = id_outlet;
    this.context = context;
}

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

@Override
public Object getItem(int location) {
    return itemss.get(location);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    Log.d(TAG+"count", parent.getChildCount() + "");
    if (inflater == null) {
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.item_cekoutlet, null);
    }
    TextView title = (TextView) convertView.findViewById(R.id.title);

    return convertView;
}

2个片段和2个适配器的相似性几乎达到95%。但为什么第二个适配器只调用getView 2次?它应该调用getView 4次(list中有4个实际的孩子)。

0 个答案:

没有答案