我想用SwipeToRefresh小部件刷新我的片段。
这是我的XML代码
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:background="@drawable/gradient_drawable"
android:padding="10dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Fragment_1">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/progress"
android:layout_width="76dp"
android:layout_height="64dp"
android:elevation="10dp"
android:foregroundGravity="center"
android:indeterminateTint="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.84" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:padding="5dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</FrameLayout>
这是我的JAVA代码
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_1, container, false);
newconfig= getResources().getConfiguration();
db=new DBHandler(getActivity());
swipeRefreshLayout=view.findViewById(R.id.swipe);
recyclerView=view.findViewById(R.id.listview);
progressBar=view.findViewById(R.id.progress);
if(book.size()==0) {
book = db.readCourses();
}
progressBar.setVisibility(View.GONE);
BookFragAdapter adapter = new BookFragAdapter(Fragment_land1.this,book);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(),RecyclerView.VERTICAL,false));
recyclerView.setAdapter(adapter);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
this.onRefresh();
getFragmentManager().beginTransaction().detach(Fragment_land1.this).attach(Fragment_land1.this).commit();
swipeRefreshLayout.setRefreshing(false);
}
});
return view;
}
当我尝试刷新时,它没有刷新,并且小工具在250ms后没有停止。我假设onRefreshListener内的代码实际上没有执行,因此下一行被忽略。我不知道如何更改它。我在Stackoverflow和Developer.android中冲浪,没有一个答案解决了我的问题。有人能帮助我吗?
1条答案
按热度按时间emeijp431#
首先检查您是否已在build.gradle文件中添加依赖项,即实现“androidx. swiperefresh布局:swiperefresh布局:1.1.0”
第二次滑动以刷新布局标签应从开始而不是片段开始,例如:
然后在java文件中写入swipeRefreshLayout.setOnRefreshListener代码