ActionBarPullToRefresh не работает, когда список пуст

Я использую Криса Бейнса ActionBar-PullToRefresh в моем ListFragment.

Все работает нормально, за исключением случаев, когда ListView пуст. Когда ListView пуст (и виден пустой текст), функция извлечения для обновления не работает, хотя я установил getListView().getEmptyView() в theseChildrenArePullable().

Я также пробовал allChildrenArePullable(), но это не сработало.

Код из моего ListFragment:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    listAdapter = new ListsAdapter(getActivity(), items);
    setListAdapter(listAdapter);

    Cursor cursor = ListProvider.getInstance().selectAll();
    ArrayList<BaseModel> cursorItems = ListProvider.getInstance().rowsToArrayList(cursor);
    for(BaseModel cursorItem : cursorItems) {
        items.add((ListModel) cursorItem);
    }
}

public void onActivityCreated(Bundle savedState) {
    super.onActivityCreated(savedState);
    registerForContextMenu(getListView());
}

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

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    getListView().setOnItemLongClickListener(this);

    mPullToRefreshLayout = new PullToRefreshLayout(getActivity());

    ActionBarPullToRefresh.from(getActivity())
            .insertLayoutInto((ViewGroup) view)
            .theseChildrenArePullable(getListView(), getListView().getEmptyView())
            .listener(this)
            .setup(mPullToRefreshLayout);
}

Код из моего файла adapter_lists.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:drawSelectorOnTop="false"/>

    <TextView android:id="@id/android:empty"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:text="@string/message_my_lists_empty"/>

</LinearLayout>

person Tijme    schedule 14.05.2015    source источник
comment
Попробуйте установить высоту вашего ListView и вашего LinearLayout на fill_parent.   -  person JonasCz    schedule 14.05.2015
comment
fill_parent — это старая версия match_parent. Так что это не поможет.   -  person Ayzen    schedule 14.05.2015
comment
@JonasCz @Ayzen прав. К сожалению, установка высоты ListView и LinearLayout на fill_parent не сработала.   -  person Tijme    schedule 14.05.2015


Ответы (1)


Поэтому я исправил это с помощью SwipeRefreshLayout, который является родным .

Чтобы это работало, вам нужно ваше представление:

<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:text="@string/hello_world"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:gravity="center"/>

    </ScrollView>

</android.support.v4.widget.SwipeRefreshLayout>

И некоторый код в вашей функции onViewCreated:

swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
swipeLayout.setOnRefreshListener(this);
swipeLayout.setColorScheme(
    android.R.color.holo_blue_bright, 
    android.R.color.holo_green_light, 
    android.R.color.holo_orange_light, 
    android.R.color.holo_red_light);
person Tijme    schedule 16.05.2015