4040import com .gcssloop .diycode .base .app .ViewHolder ;
4141import com .gcssloop .diycode .base .recyclerview .GcsAdapter ;
4242import com .gcssloop .diycode .base .recyclerview .GcsViewHolder ;
43+ import com .gcssloop .diycode .utils .Config ;
4344import com .gcssloop .diycode .utils .DataCache ;
4445import com .gcssloop .diycode .utils .RecyclerViewUtil ;
4546import com .gcssloop .diycode .utils .TimeUtil ;
4849import com .gcssloop .diycode_sdk .api .news .bean .New ;
4950import com .gcssloop .diycode_sdk .api .news .event .GetNewsListEvent ;
5051import com .gcssloop .diycode_sdk .api .user .bean .User ;
52+ import com .gcssloop .diycode_sdk .log .Logger ;
5153
5254import org .greenrobot .eventbus .EventBus ;
5355import org .greenrobot .eventbus .Subscribe ;
@@ -83,13 +85,16 @@ public class NewsListFragment extends BaseFragment {
8385
8486 // 数据
8587 private Diycode mDiycode ; // 在线(服务器)
88+ private Config mConfig ;
8689 private DataCache mDataCache ; // 缓存(本地)
8790
8891 // View
8992 private GcsAdapter <New > mAdapter ;
9093 private SwipeRefreshLayout mRefreshLayout ;
9194
95+ private NestedScrollView mScrollView ;
9296 private boolean isFirstLunch = true ;
97+ boolean isFirstLaunch = true ;
9398
9499 public static NewsListFragment newInstance () {
95100 Bundle args = new Bundle ();
@@ -102,6 +107,7 @@ public static NewsListFragment newInstance() {
102107 public void onCreate (@ Nullable Bundle savedInstanceState ) {
103108 super .onCreate (savedInstanceState );
104109 mDiycode = Diycode .getSingleInstance ();
110+ mConfig = Config .getSingleInstance ();
105111 mDataCache = new DataCache (getContext ());
106112 }
107113
@@ -110,13 +116,24 @@ protected int getLayoutId() {
110116 return R .layout .fragment_recycler_refresh ;
111117 }
112118
113- private void loadData () {
119+ private void initData () {
114120 mRefreshLayout .setEnabled (true );
115121 // 第一次加载,默认从缓存加载
116122 List <New > news = mDataCache .getNewsList ();
117123 if (null != news && news .size () > 0 ) {
124+ pageIndex = mConfig .getNewsListPageIndex ();
118125 mAdapter .addDatas (news );
119126 mFooter .setText (FOOTER_NORMAL );
127+ if (isFirstLaunch ) {
128+ final int lastScroll = mConfig .getNewsLastScroll ();
129+ mScrollView .post (new Runnable () {
130+ @ Override
131+ public void run () {
132+ mScrollView .scrollTo (0 , lastScroll );
133+ }
134+ });
135+ isFirstLaunch = false ;
136+ }
120137 } else {
121138 loadMore ();
122139 mFooter .setText (FOOTER_LOADING );
@@ -126,10 +143,11 @@ private void loadData() {
126143 @ Override
127144 protected void initViews (ViewHolder holder , View root ) {
128145 mFooter = holder .get (R .id .footer );
146+ mScrollView = holder .get (R .id .scroll_view );
129147 initRefreshLayout (holder );
130148 initRecyclerView (getContext (), holder );
131149 initListener (holder );
132- loadData ();
150+ initData ();
133151 }
134152
135153 private void initRefreshLayout (ViewHolder holder ) {
@@ -276,4 +294,20 @@ public void onStop() {
276294 super .onStop ();
277295 EventBus .getDefault ().unregister (this );
278296 }
297+
298+ @ Override
299+ public void onDestroyView () {
300+ // 保存
301+ int lastScrollY = mScrollView .getScrollY ();
302+ mConfig .saveNewsListScroll (lastScrollY );
303+ mConfig .saveNewsListPageIndex (pageIndex );
304+ super .onDestroyView ();
305+ }
306+
307+ public void quickToTop () {
308+ Logger .e ("快速返回" );
309+ if (mScrollView != null ) {
310+ mScrollView .smoothScrollTo (0 , 0 );
311+ }
312+ }
279313}
0 commit comments