ListView上的setAdapter后片段消失

时间:2014-09-30 08:20:09

标签: android listview android-fragments android-listview baseadapter

我在片段中有一个ListView,还包含一些其他UI元素。一切正常,直到我在ListView上调用setAdapter。在那一刻,我可以调试ListView填充元素,但在整个片段消失后立即(包括其他UI元素)。如果我在1秒后设置适配器,一切正常。

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mView = inflater.inflate(R.layout.fragment_listview, null);
    mListView = (ListView)mView.findViewById(R.id.list_items);

    TextView emptyView = (TextView)mView.findViewById(R.id.text_empty);
    mListView.setEmptyView(emptyView);

    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            // this is the only work around to not make the list view disappear
            loadContacts();
        }
    }, 100);
    // if I call loadContacts() here, the Fragment disappears

    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Person p = mContacts.get(position);
            if (pickerInterface != null) {
                pickerInterface.pickedContact(p);
            }
        }
    });
    return mView;
}


public void loadContacts() {
    mContacts = ContactsDataSource.getAllPhoneContacts(getActivity(), true);
    mAdapter = new ContactsAdapter(getActivity());
    mAdapter.mContacts = mContacts;
    mAdapter.isPlainList = true;
    // after the following line the Fragment is filled and then immediately disappears
    mListView.setAdapter(mAdapter);
}

ContactsAdapter非常简单:

public class ContactsAdapter extends BaseAdapter {
    public List<Person> mContacts = new ArrayList<Person>();
    public Activity context;

    // Constructor
    public ContactsAdapter(Activity c){
        context = c;
    }

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

    public Person getItem(int position){
        return mContacts.get(position);
    }

    @Override
    public boolean isEmpty()
    {
        return mContacts.size() == 0;
    }

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

    @SuppressLint("InflateParams")
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_item_contact, null);
        }
        Person person = mContacts.get(position);

        TextView tvName = (TextView) convertView.findViewById(R.id.text_name);
        tvName.setText(person.getDisplayName());
        return convertView;
    }
}

我花了几个小时调试和搜索原因并且没有任何线索。谢谢你的任何想法。

编辑: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"
    android:background="@color/white" >

    <ListView
        android:id="@+id/list_items"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/line_horizontal"
        android:layout_alignParentTop="true" />

    <TextView
        android:id="@+id/text_empty"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="15dp"
        android:layout_alignParentTop="true"
        android:text="@string/label_no_entries"
        android:textColor="@color/grey"
        android:textSize="21sp" />

    <View
        android:id="@+id/line_horizontal"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_above="@+id/layout_navigation"
        android:background="@color/black" />

    <RelativeLayout
        android:id="@+id/layout_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

        <TextView
            android:id="@+id/text_scrollback"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:drawablePadding="10dp"
            android:drawableRight="@drawable/backward"
            android:gravity="center_vertical"
            android:paddingBottom="15dp"
            android:paddingLeft="10dp"
            android:paddingTop="15dp"
            android:text="@string/activity_contacts_backward_label"
            android:textColor="@color/mediumgrey" />

        <View
            android:id="@+id/view_coverback"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/text_scrollback"
            android:layout_alignParentLeft="true"
            android:layout_alignRight="@+id/text_scrollback"
            android:layout_alignTop="@+id/text_scrollback"
            android:background="#c0ffffff" />

        <TextView
            android:id="@+id/text_scrollforward"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:drawableLeft="@drawable/forward"
            android:drawablePadding="10dp"
            android:gravity="center_vertical"
            android:paddingBottom="15dp"
            android:paddingRight="10dp"
            android:paddingTop="15dp"
            android:text="@string/activity_contacts_forward_label"
            android:textColor="@color/mediumgrey" />

        <View
            android:id="@+id/view_coverforward"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/text_scrollforward"
            android:layout_alignParentRight="true"
            android:layout_alignLeft="@+id/text_scrollforward"
            android:layout_alignTop="@+id/text_scrollforward"
            android:background="#c0ffffff" />

        <TextView
            android:id="@+id/text_pages"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/text_scrollforward"
            android:layout_toLeftOf="@+id/text_scrollforward"
            android:layout_toRightOf="@+id/text_scrollback"
            android:gravity="center"
            android:textColor="@color/mediumgrey"
            android:textSize="17sp" />
    </RelativeLayout>

</RelativeLayout>

4 个答案:

答案 0 :(得分:1)

您在哪里初始化“mListView”? 片段膨胀的时间是“onCreateView()”,并且在此方法上完成从布局获取视图实例的一般方法。如下所示

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  View v = inflater.inflate(R.layout.fragment_one, null);
  mListView = (ListView)v.findViewById(R.id.list_items);
  ...
  return v;
}

如何尝试在“onCreateView”中初始化您的视图(ListViews和其他人)? 我认为问题是由于片段膨胀之前获取视图实例引起的。

答案 1 :(得分:1)

不确定您的问题是什么。可能是自定义超类。否则,在这里疯狂猜测,但尝试像这样膨胀你的观点。

mView = inflater.inflate(R.layout.fragment_listview, container, false);

答案 2 :(得分:0)

首先,存储上下文对象可能很危险,因此应该尽可能避免......您可以通过以下步骤轻松地在适配器中删除它(希望也可以解决您的问题):

- &GT;尝试使用适配器的构造函数:

public ContactsAdapter(Context c){
    super(c, R.layout.yourlayout_listitem_name;
}

- &GT;并调整getView方法以使用类似:

final View rowView = View.inflate(viewGroup.getContext(), R.layout.yourlayout_listitem_name, null);

而不是inflater-if-block。然后总是返回rowView

并且确定我会删除overriden-method:

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

因为它无论如何都没有做任何有用的事情; - )

答案 3 :(得分:0)

感谢您的支持。通过删除所有代码并逐步重新组装,我找到了原因。以防有人有一天遇到同样的问题。

我在超类中声明的OnScrollListener上有一个ListView。在OnScrollListener中,我使用Visibility.GONE按需隐藏了导航箭头。即使NavigationArrows不影响ListView的布局,这也是问题所在。通过设置Visibility.INVISIBLE一切正常。我想这与一些不起作用的整个布局的重新绘制问题有关。