欧美成人午夜免费全部完,亚洲午夜福利精品久久,а√最新版在线天堂,另类亚洲综合区图片小说区,亚洲欧美日韩精品色xxx

扣丁學堂Android培訓之實現(xiàn)RecyclerView可點擊頂部懸浮條效果

2018-03-28 10:46:37 2070瀏覽

今天給大家介紹項目之前需要開發(fā)一個懸浮置頂?shù)男Ч旅嫖覀円黄饋砜匆幌戮唧w的扣丁學堂Android培訓之實現(xiàn)RecyclerView可點擊頂部懸浮條效果吧。



下面附上關(guān)鍵代碼
  //隨意添加的一些數(shù)據(jù),其中用top字段判斷是否存在懸浮條,用日期分類,每個日期的第一條top為true
  datas=newArrayList<>();
  datas.add(newWalletBean("20180101",100,false));
  datas.add(newWalletBean("20180101",100,false));
  datas.add(newWalletBean("20180101",100,false));
  datas.add(newWalletBean("20180102",100,false));
  datas.add(newWalletBean("20180102",100,false));
  datas.add(newWalletBean("20180102",100,false));
  datas.add(newWalletBean("20180102",100,false));
  datas.add(newWalletBean("20180103",100,false));
  datas.add(newWalletBean("20180103",100,false));
  datas.add(newWalletBean("20180103",100,false));
  datas.add(newWalletBean("20180103",100,false));
  datas.add(newWalletBean("20180104",100,false));
  datas.add(newWalletBean("20180105",100,false));
  linearLayoutManager=newLinearLayoutManager(getActivity());
  rcList.setLayoutManager(linearLayoutManager);
  //BaseQuickAdapter是我使用的一個快速適配器框架,把代碼放入Recycler的adapter中都可以使用
  mAdapter=newBaseQuickAdapter<WalletBean,BaseViewHolder>(R.layout.item_wallet,datas){
  @Override
  protectedvoidconvert(BaseViewHolderhelper,WalletBeanitem){
  intpos=helper.getLayoutPosition();
  if(pos>=1){
  if(item.getTime().equals(datas.get(pos-1).getTime())){
  helper.setVisible(R.id.ll_top,false);
  item.setTop(false);
  }else{
  helper.setVisible(R.id.ll_top,true);
  item.setTop(true);
  }
  }else{
  helper.setVisible(R.id.ll_top,true);
  item.setTop(true);
  }
  }
  };
  //重點是這里,給Recycler添加一個crollListener其中l(wèi)lTop是寫在布局文件里的
  rcList.addOnScrollListener(newRecyclerView.OnScrollListener(){
  @Override
  publicvoidonScrollStateChanged(RecyclerViewrecyclerView,intnewState){
  super.onScrollStateChanged(recyclerView,newState);
  mSuspensionHeight=llTop.getHeight();//懸浮條最底部的y
  }
  @Override
  publicvoidonScrolled(RecyclerViewrecyclerView,intdx,intdy){
  super.onScrolled(recyclerView,dx,dy);
  //獲取最頂部的item
  Viewview=linearLayoutManager.findViewByPosition(mCurrentPosition+1);
  //獲取到的view頂部坐標小于懸浮條最底部的y軸坐標就是要重疊了
  //datas.get(mCurrentPosition+1).isTo表示下個item存在懸浮條
  if(view.getTop()<=mSuspensionHeight&&datas.get(mCurrentPosition+1).isTop()){
  llTop.setY(-(mSuspensionHeight-view.getTop()));
  }else{
  llTop.setY(0);
  }
  //mCurrentPosition不是當前顯示最頂部的position
  if(mCurrentPosition!=linearLayoutManager.findFirstVisibleItemPosition()){
  mCurrentPosition=linearLayoutManager.findFirstVisibleItemPosition();
  llTop.setY(0);
  }
  }
  });
  llTop.setOnClickListener(newView.OnClickListener(){
  @Override
  publicvoidonClick(Viewv){
  //TODO:2018/3/27點擊事件處理
  ToastUtils.show("SHOW");
  }
  });

下面是xml布局文件
  <RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@color/gray_bg">
  <android.support.v4.widget.SwipeRefreshLayout
  android:id="@+id/swipeLayout"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  >
  <android.support.v7.widget.RecyclerView
  android:background="@color/color_white"
  android:id="@+id/rc_list"
  android:scrollbars="none"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  </android.support.v7.widget.RecyclerView>
  </android.support.v4.widget.SwipeRefreshLayout>
  <LinearLayout
  android:id="@+id/ll_top"
  android:background="@color/red"
  android:paddingRight="20dp"
  android:paddingLeft="20dp"
  android:paddingBottom="9dp"
  android:paddingTop="9dp"
  android:orientation="horizontal"
  android:layout_width="match_parent"
  android:layout_height="wrap_content">
  <TextView
  android:id="@+id/tv_time_day"
  android:layout_weight="1"
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:text="2018年2月20日"
  android:textColor="@color/color_rb_checked_false"
  android:textSize="13sp"/>
  <TextView
  android:id="@+id/tv_sum_money"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="合計-1075.51元"
  android:textColor="@color/color_rb_checked_false"
  android:textSize="13sp"/>
  </LinearLayout>
  </RelativeLayout>
  <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical"
  android:background="@drawable/list_touch_bg">
  <LinearLayout
  android:id="@+id/ll_top"
  android:background="@color/red"
  android:paddingRight="20dp"
  android:paddingLeft="20dp"
  android:paddingBottom="9dp"
  android:paddingTop="9dp"
  android:orientation="horizontal"
  android:layout_width="match_parent"
  android:layout_height="wrap_content">
  <TextView
  android:id="@+id/tv_time_day"
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:layout_weight="1"
  android:text="2018年2月20日"
  android:textColor="@color/color_rb_checked_false"
  android:textSize="13sp"/>
  <TextView
  android:id="@+id/tv_sum_money"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="合計-1075.51元"
  android:textColor="@color/color_rb_checked_false"
  android:textSize="13sp"/>
  </LinearLayout>
  <LinearLayout
  android:paddingBottom="12dp"
  android:paddingLeft="20dp"
  android:paddingRight="20dp"
  android:paddingTop="12dp"
  android:background="@color/color_white"
  android:orientation="horizontal"
  android:layout_width="match_parent"
  android:layout_height="wrap_content">
  <TextView
  android:id="@+id/tv_time"
  android:layout_weight="1"
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:text="21:44結(jié)算"
  android:textColor="@color/color_rb_checked_true"
  android:textSize="17sp"/>
  <TextView
  android:id="@+id/tv_money"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="-1075.51元"
  android:textColor="@color/color_rb_checked_true"
  android:textSize="17sp"
  />
  </LinearLayout>
  <View
  android:background="@color/gray_bg"
  android:layout_width="match_parent"
  android:layout_height="1dp"/>
  </LinearLayout>

可以看到我在SwipeRefreshLayout中放了一個lltop作為懸浮條,每個item中都給了同樣布局的懸浮條,但默認隱藏,所以我們的點擊事件實際操作的是lltop,至于更新lltop中的數(shù)據(jù)就不用說了,滑動到下個懸浮條的時候更新就可以了。

RecyclerView頂部懸浮條的功能也就這樣實現(xiàn)了,當然考慮到代碼實際性能可能并不如recyclerView.addItemDecoration,但思路是一樣的,并且支持了懸浮條的點擊事件。

最后想要了解更多請關(guān)注扣丁學堂Android培訓官網(wǎng)、微信公眾號平臺,扣丁學堂Android視頻教程從零基礎(chǔ)到精通免費試聽試學,就業(yè)終端服務系統(tǒng)隨時監(jiān)控學員的學習情況和效果,順利畢業(yè)后根據(jù)學員自身風格和學習效果推薦就業(yè)。如果你想要學習Android開發(fā)工程師技術(shù)就不要再猶豫了,喜歡就付諸行動吧。


扣丁學堂微信公眾號



【關(guān)注微信公眾號獲取更多學習資料】



查看更多關(guān)于“Android開發(fā)技術(shù)的相關(guān)資訊>>

標簽: Android開發(fā)從入門到精通 Android學習路線圖 Android培訓 Android開發(fā)工程師 Android視頻教程

熱門專區(qū)

暫無熱門資訊

課程推薦

微信
微博
15311698296

全國免費咨詢熱線

郵箱:codingke@1000phone.com

官方群:148715490

北京千鋒互聯(lián)科技有限公司版權(quán)所有   北京市海淀區(qū)寶盛北里西區(qū)28號中關(guān)村智誠科創(chuàng)大廈4層
京ICP備2021002079號-2   Copyright ? 2017 - 2022
返回頂部 返回頂部