4 changed files with 143 additions and 4 deletions
@ -1,6 +1,126 @@ |
|||||||
package cc.niushuai.didicheck.ui.main.records; |
package cc.niushuai.didicheck.ui.main.records; |
||||||
|
|
||||||
|
import android.content.Context; |
||||||
|
import android.os.Bundle; |
||||||
|
import android.util.Log; |
||||||
|
import android.view.LayoutInflater; |
||||||
|
import android.view.View; |
||||||
|
import android.view.ViewGroup; |
||||||
|
|
||||||
|
import androidx.annotation.NonNull; |
||||||
|
import androidx.annotation.Nullable; |
||||||
|
import androidx.constraintlayout.widget.ConstraintLayout; |
||||||
import androidx.fragment.app.Fragment; |
import androidx.fragment.app.Fragment; |
||||||
|
import androidx.recyclerview.widget.DefaultItemAnimator; |
||||||
|
import androidx.recyclerview.widget.DividerItemDecoration; |
||||||
|
import androidx.recyclerview.widget.LinearLayoutManager; |
||||||
|
import androidx.recyclerview.widget.RecyclerView; |
||||||
|
|
||||||
|
import org.reactivestreams.Subscription; |
||||||
|
|
||||||
|
import java.util.Collections; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
import cc.niushuai.didicheck.biz.entity.CheckRecord; |
||||||
|
import cc.niushuai.didicheck.biz.room.DBManager; |
||||||
|
import cc.niushuai.didicheck.databinding.FragmentDataListBinding; |
||||||
|
import cc.niushuai.didicheck.ui.main.home.HomeRecycleViewAdapter; |
||||||
|
import cc.niushuai.didicheck.util.Toasts; |
||||||
|
import io.reactivex.Flowable; |
||||||
|
import io.reactivex.FlowableSubscriber; |
||||||
|
import io.reactivex.android.schedulers.AndroidSchedulers; |
||||||
|
import io.reactivex.schedulers.Schedulers; |
||||||
|
|
||||||
public class DataListFragment extends Fragment { |
public class DataListFragment extends Fragment { |
||||||
|
|
||||||
|
|
||||||
|
private FragmentDataListBinding fragmentDataListBinding; |
||||||
|
|
||||||
|
private HomeRecycleViewAdapter dataListRecycleViewAdapter; |
||||||
|
|
||||||
|
@Override |
||||||
|
public void onCreate(@Nullable Bundle savedInstanceState) { |
||||||
|
super.onCreate(savedInstanceState); |
||||||
|
} |
||||||
|
|
||||||
|
@Nullable |
||||||
|
@Override |
||||||
|
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { |
||||||
|
|
||||||
|
Log.d("DataList", "onCreateView"); |
||||||
|
fragmentDataListBinding = FragmentDataListBinding.inflate(inflater, container, false); |
||||||
|
ConstraintLayout rootLayout = fragmentDataListBinding.getRoot(); |
||||||
|
|
||||||
|
initRecycleView(); |
||||||
|
|
||||||
|
// 默认查一次今天的数据
|
||||||
|
refreshAllDate(); |
||||||
|
|
||||||
|
return rootLayout; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void onAttach(@NonNull Context context) { |
||||||
|
super.onAttach(context); |
||||||
|
Log.d("onAttach", "xxx"); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void onStart() { |
||||||
|
super.onStart(); |
||||||
|
Log.d("onStart", "xxx"); |
||||||
|
refreshAllDate(); |
||||||
|
} |
||||||
|
|
||||||
|
private void refreshAllDate() { |
||||||
|
Flowable<List<CheckRecord>> flowable = DBManager.INSTANCE.checkRecordDao().listAll(); |
||||||
|
flowable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()) |
||||||
|
.subscribe(new FlowableSubscriber<List<CheckRecord>>() { |
||||||
|
@Override |
||||||
|
public void onSubscribe(Subscription s) { |
||||||
|
// 3s超时
|
||||||
|
s.request(1); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void onNext(List<CheckRecord> checkRecords) { |
||||||
|
Log.d("DataListOnNext", "查询到数据 " + checkRecords.size()); |
||||||
|
RecyclerView recyclerView = fragmentDataListBinding.dataListRecyclerView; |
||||||
|
int childCount = recyclerView.getChildCount(); |
||||||
|
if (childCount > 0) { |
||||||
|
recyclerView.removeAllViews(); |
||||||
|
} |
||||||
|
dataListRecycleViewAdapter.resetRecordList(checkRecords); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void onError(Throwable t) { |
||||||
|
Toasts.shortShow(getActivity(), "DataList根据时间拉取历史数据失败: ", t.getLocalizedMessage()); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void onComplete() { |
||||||
|
} |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
private void initRecycleView() { |
||||||
|
RecyclerView homeDataZoneRecyclerView = fragmentDataListBinding.dataListRecyclerView; |
||||||
|
homeDataZoneRecyclerView.addItemDecoration(new DividerItemDecoration(this.getActivity(), DividerItemDecoration.VERTICAL)); |
||||||
|
DefaultItemAnimator itemAnimator = new DefaultItemAnimator(); |
||||||
|
itemAnimator.setAddDuration(1000); |
||||||
|
itemAnimator.setRemoveDuration(1000); |
||||||
|
homeDataZoneRecyclerView.setItemAnimator(itemAnimator); |
||||||
|
|
||||||
|
dataListRecycleViewAdapter = new HomeRecycleViewAdapter(getActivity(), Collections.emptyList()); |
||||||
|
fragmentDataListBinding.dataListRecyclerView.setAdapter(dataListRecycleViewAdapter); |
||||||
|
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()); |
||||||
|
fragmentDataListBinding.dataListRecyclerView.setLayoutManager(layoutManager); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void onDestroy() { |
||||||
|
super.onDestroy(); |
||||||
|
fragmentDataListBinding = null; |
||||||
|
} |
||||||
} |
} |
||||||
|
@ -0,0 +1,19 @@ |
|||||||
|
<?xml version="1.0" encoding="utf-8"?> |
||||||
|
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto" |
||||||
|
xmlns:tools="http://schemas.android.com/tools" |
||||||
|
android:id="@+id/HomeFragmentRootLayout" |
||||||
|
android:layout_width="match_parent" |
||||||
|
android:layout_height="match_parent" |
||||||
|
tools:context=".ui.main.records.DataListFragment"> |
||||||
|
|
||||||
|
<!--数据展示view--> |
||||||
|
<androidx.recyclerview.widget.RecyclerView |
||||||
|
android:id="@+id/data_list_recyclerView" |
||||||
|
android:layout_width="match_parent" |
||||||
|
android:layout_height="0dp" |
||||||
|
app:layout_constraintHeight_percent="1" |
||||||
|
app:layout_constraintLeft_toLeftOf="parent" |
||||||
|
app:layout_constraintTop_toTopOf="parent" /> |
||||||
|
|
||||||
|
</androidx.constraintlayout.widget.ConstraintLayout> |
Loading…
Reference in new issue