带有androidx的OnRefreshListener的SwipeRefreshLayout数据绑定

时间:2019-07-02 03:17:55

标签: android androidx swiperefreshlayout

如何为layout.xml中的OnRefreshListener小部件分配SwipeRefreshLayout

对于Button,我可以这样做(使用OnClickListener):

<layout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable
            name="addAccount_onClickListener"
            type="android.view.View.OnClickListener"/>
    </data>

    <RelativeLayout>

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/button_home_add_account"
            android:layout_width="56dp"
            android:layout_height="56dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:onClick="@{addAccount_onClickListener::onClick}"/>

    </RelativeLayout>
</layout>

但这根本没有给我这种选择:

        <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
            android:id="@+id/swiperefreshlayout_home_pulldown"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="-10dp">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerview_home_listaccounts"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                tools:listitem="@layout/fragment_account_home"
            />

        </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以这样实现布局-

 <layout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      xmlns:app="http://schemas.android.com/apk/res-auto">

   <data>
       <import type="android.view.View"/>
       <variable name="loading" type="boolean"/>
   </data>

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

          <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
            android:id="@+id/swiperefreshlayout_home_pulldown"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="-10dp">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerview_home_listaccounts"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                tools:listitem="@layout/fragment_account_home"/>

        </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

    </RelativeLayout>
 </layout>

然后在您的活动中这样做-

 public abstract class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<ResourceLoaderResult<T>>, SwipeRefreshLayout.OnRefreshListener {
    protected LayoutInfoBinding binding;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.layout_info, container, false);

        binding.setTitleText(getTitleResourceId());
        setMainText(getActivity().getString(R.string.no_data));
        binding.swipeRefreshLayout.setOnRefreshListener(this);
        binding.setLoading(true);

        return binding.getRoot();
    }

    @Override
    public void onResume() {
        super.onResume();
        getLoaderManager().initLoader(getLoaderId(), null, this).forceLoad();
    }

    @Override
    public void onLoadFinished(Loader<ResourceLoaderResult<T>> loader, ResourceLoaderResult<T> data) {
        binding.swipeRefreshLayout.setRefreshing(false);
        binding.setLoading(false);
    }

    @Override
   public void onRefresh() {
       getLoaderManager().getLoader(getLoaderId()).forceLoad();
   }