Fragment里面的BaseAdapter没有更新内容

时间:2016-02-07 13:54:21

标签: android listview android-fragments firebase baseadapter

我正在使用Firebase,我在Fragment中有一个ListView。在第一次加载和更改Firebase内容时,一切正常。但是,在替换Fragment(在其他Fragment上提供搜索功能)并恢复FragmentTransaction后,BaseAdapter无法正确更新(@android:显示id / empty)。

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ...

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.content, mCategoriesPopularFragment)
                .commit();
    }
}

@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
    /** Reverting FragmentTransaction */
    getSupportFragmentManager().popBackStackImmediate();
    return true;
}

@Override
public boolean onMenuItemActionExpand(MenuItem item) {
    /** Replacing by "search" Fragment */
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.content, mCategoriesFragment)
            .addToBackStack("CategoriesFragment")
            .commit();
    return true;
}

CategoriesPopularFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_categories_popular, container, false);

    ...

    return rootView;
}

@Override
public void onStart() {
    super.onStart();
    mLoadingLayout.setVisibility(View.VISIBLE);
    mFirebaseRef.orderByChild("name").addValueEventListener(this);
}

@Override
public void onStop() {
    super.onStop();
    mFirebaseRef.removeEventListener(this);
}

@Override
public void onDataChange(DataSnapshot dataSnapshot) {
    List<Category> allItems = new ArrayList<>();
    for (DataSnapshot d : dataSnapshot.getChildren()) {
        Category c = Category.populateFromFirebase(d);
        if (c.isPopular()) {
            allItems.add(c);
        }
    }

    if (mAdapter == null) {
        mAdapter = new CategoryPopularAdapter(getContext(), allItems);
        mListView.setAdapter(mAdapter);
    } else {
        mAdapter.updateItems(allItems);
    }

    mLoadingLayout.setVisibility(View.GONE);
}

CategoryPopularAdapter.java

public CategoryPopularAdapter(Context context, List<Category> items) {
    this.mInflater = LayoutInflater.from(context);
    this.mItems = new ArrayList<>(items);
}

public void updateItems(List<Category> items) {
    mItems.clear();
    mItems.addAll(items);
    notifyDataSetChanged();
}

fragment_categories_popular.xml

<LinearLayout 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:orientation="vertical"
    tools:context="br.com.jzone.comprebem.fragment.CategoriesPopularFragment">

    <RelativeLayout
        android:id="@+id/loading_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    </RelativeLayout>

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@null"
        tools:listitem="@layout/list_item_category" />

    <TextView
        android:id="@android:id/empty"
        style="@style/TextAppearance.AppCompat.Large"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="@dimen/activity_default_margin"
        android:gravity="center"
        android:text="@string/activity_main_no_category_found_message"
        android:textColor="@color/gray" />
</LinearLayout>

我做错了什么?这个问题与FragmentTransaction有关吗?我希望有人帮忙。

感谢。

0 个答案:

没有答案