尚硅谷android企業(yè)級技術(shù)之-recyclerview_第1頁
尚硅谷android企業(yè)級技術(shù)之-recyclerview_第2頁
尚硅谷android企業(yè)級技術(shù)之-recyclerview_第3頁
尚硅谷android企業(yè)級技術(shù)之-recyclerview_第4頁
尚硅谷android企業(yè)級技術(shù)之-recyclerview_第5頁
已閱讀5頁,還剩11頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、第 14 講_RecyclerView1_創(chuàng)建 RecyclerViewDemo在 build.gradle 配置 RecyclerView 的庫2_在布局文件寫定義 RecyclerView3_在代碼中實(shí)例化 RecyclerView 并且初始化數(shù)據(jù)1/* 數(shù)據(jù)集合compile com.android.support:recyclerview-v7:23.3.04_設(shè)置適配器(難點(diǎn)重點(diǎn))1.定義適配器2public class MyAdapter extendsRecyclerView.Adapter private final Context context;private final

2、ArrayList datas;public MyAdapter(Context context,ArrayList datas)this.context = context;this.datas = datas;/*Overrideprotected void onCreate(Bundle savedInstane) super.onCreate(savedInstane); setContentView(R.layout.activity_main);recyclerview = (RecyclerView) findViewById(R.id.recyclerview); initDa

3、ta();/* 初始化數(shù)據(jù)*/private void initData() datas = new ArrayList();for (i = 0; i 100; i+) datas.add(Content + i);*/private ArrayList datas;3相當(dāng)于 ListView 適配器中的 getView 的創(chuàng)建 holder 布局*pararentparam viewTypereturn*/Overridepublic MyViewHolder onCreateViewHolder(ViewGroup parent,viewType)View view = View.inf

4、late(context, R.layout.item_o, null); return new MyViewHolder(view);Overridepublic void onBindViewHolder(MyViewHolder holder,ition) holder.tv_text.setText(datas.get(ition); holder.iv_icon.setBackgroundResource(R.mipmap.ic_launcher);OverridepublicgetItemCount() return datas.size();class MyViewHolder

5、extends RecyclerView.ViewHolder private TextView tv_text; private ImageView iv_icon;public MyViewHolder(View itemView) super(itemView);tv_text = (TextView) itemView.findViewById(R.id.tv_text);iv_icon = (ImageView) itemView.findViewById(R.id.iv_icon);2.設(shè)置布局文件45_設(shè)置適配器-水平方向-豎直方向-網(wǎng)格-瀑布流6_設(shè)置分割線參照:5/設(shè)置分割線

6、-分割線需要自定義&還可以自定義分割線的樣式/沒有提供默認(rèn)的分割線recyclerview.addItemDecoration(new DividerListItemDecoration(this, DividerListItemDecoration.VERTICAL_LIST);public class DividerListItemDecoration extends RecyclerView.ItemDecorationmyAdapter = new MyAdapter(this,datas);recyclerview.setAdapter(myAdapter);/*設(shè)置布局:第一個參數(shù)

7、:上下文第二參數(shù):方向第三個參數(shù):排序低到高還是高到低顯示,false 是低到高顯示*/LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3,GridLayoutManager.VERTICAL, false);StaggeredGridLayoutManager staggeredGridLayo

8、utManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.HORIZONTAL); recyclerview.setLayoutManager(linearLayoutManager);6private sic final ATTRS = new android.R.attr.listDivider;public sic finalHORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;public sic finalVERTICAL_LIST = LinearLay

9、outManager.VERTICAL;private Drawable mDivider; privatemOrienion;public DividerListItemDecoration(Context context,orienion) final TypedArray a = context.obtainStyledAttributes(ATTRS); mDivider = a.getDrawable(0);a.recycle();setOrienion(orienion);public void setOrienion(orienion) if (orienion != HORIZ

10、ONTAL_LIST & orienion != VERTICAL_LIST)throw new IllegalArgumentException(invalid orienion);mOrienion = orienion;Overridepublic void onDraw(Canvas c, RecyclerVierent) /Log.e(recyclerview - itemdecoration, onDraw();if (mOrienion = VERTICAL_LIST) drawVertical(c, parent); else drawHorizontal(c, parent)

11、;public void drawVertical(Canvas c, RecyclerVierent) 7finalleft = parent.getPaddingLeft();finalright = parent.getWidth() - parent.getPaddingRight();finalchildCount = parent.getChildCount();for (i = 0; i childCount; i+) final View child = parent.getChildAt(i); android.support.v7.widget.RecyclerView v

12、 = newandroid.support.v7.widget.RecyclerVierent.getContext();final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();finaltop = child.getBottom() + params.bottomMargin; finalbottom = top + mDivider.getrinsicHeight(); mDivider.setBounds(left, top, right, bottom);

13、mDivider.draw(c);public void drawHorizontal(Canvas c, RecyclerVierent) finaltop = parent.getPaddingTop();finalbottom = parent.getHeight() - parent.getPaddingBottom();finalchildCount = parent.getChildCount();for (i = 0; i childCount; i+) final View child = parent.getChildAt(i); final RecyclerView.Lay

14、outParams params =(RecyclerView.LayoutParams) child.getLayoutParams();finalleft = child.getRight() + params.rightMargin; finalright = left + mDivider.getrinsicHeight(); mDivider.setBounds(left, top, right, bottom); mDivider.draw(c);Overridepublic void getItemOffsets(Rect outRect,itemition, RecyclerV

15、iew parent) if (mOrienion = VERTICAL_LIST) outRect.set(0, 0, 0, mDivider.getrinsicHeight(); else outRect.set(0, 0, mDivider.getrinsicWidth(), 0);設(shè)置分割線樣式在 styles.xml 樣式文件中8color/colorPrimarycolor/colorPrimaryDarkcolor/colorAccentdrawable/divider_bgdrawable/divider_bg在 drawable 目錄下的 divider_bg.xml7_自定

16、義設(shè)置item 的點(diǎn)擊事件或者item 中某個控件的點(diǎn)擊事件RecyclerView 默認(rèn)是沒有點(diǎn)擊事件的,需要自定義點(diǎn)擊事件用到知識點(diǎn):接口,getLayoutition()9public class MyAdapter extends RecyclerView.Adapterprivate final Context context;private final ArrayList datas;/設(shè)置點(diǎn)擊某個 item 的publicerface OnItemClickListenervoid onItemClick(View view,ition,String content);priva

17、te OnItemClickListener onItemClickListener;/*設(shè)置某條的param onItemClickListener*/public void setOnItemClickListener(OnItemClickListener onItemClickListener) this.onItemClickListener = onItemClickListener;/設(shè)置點(diǎn)擊/設(shè)置點(diǎn)擊某個 item 的publicerface OnImageViewClickListenervoid onImageViewClick(View view,ition);10pri

18、vate OnImageViewClickListener onImageViewClickListener;/*設(shè)置param onImageViewClickListener*/public void setOnImageViewClickListener(OnImageViewClickListener onImageViewClickListener) this.onImageViewClickListener = onImageViewClickListener;.class MyViewHolder extends RecyclerView.ViewHolder private T

19、extView tv_text; private ImageView iv_icon;public MyViewHolder(View itemView) super(itemView);tv_text = (TextView) itemView.findViewById(R.id.tv_text);iv_icon = (ImageView) itemView.findViewById(R.id.iv_icon);/設(shè)置點(diǎn)擊事件itemView.setOnClickListener(new View.OnClickListener() Overridepublic void onClick(V

20、iew v) if(onItemClickListener != null)onItemClickListener.onItemClick(v,getLayoutition(),datas.get(getLayo utition(););/設(shè)置iv_icon.setOnClickListener(new View.OnClickListener() Overridepublic void onClick(View v) if(onImageViewClickListener != null)11onImageViewClickListener.onImageViewClick(v,getLay

21、outition(););在 Activity 中使用自定義的點(diǎn)擊事件/設(shè)置點(diǎn)擊 item 的點(diǎn)擊事件myAdapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() Overridepublic void onItemClick(View view,ition, String content) Toast.makeText(RecyclerViewActivity.this,content=+content+,-ition=+ition, Toast.LENGTH_SHORT).show(););/設(shè)置點(diǎn)擊某張的點(diǎn)擊事件

22、myAdapter.setOnImageViewClickListener(newMyAdapter.OnImageViewClickListener() Overridepublic void onImageViewClick(View view,ition) Toast.makeText(RecyclerViewActivity.this,ition=+ition+,view=+view.toString(), Toast.LENGTH_SHORT).show(););8_刪除和增加數(shù)據(jù)1_在適配器中新增加添加和刪除兩個方法2_在布局文件中添加點(diǎn)擊按鈕如圖:12public class M

23、yAdapter extends RecyclerView.Adapter./*添加數(shù)據(jù)parositionparam content*/public void addData(ition,String content) datas.add(ition,content); notifyItemInserted(ition);/*移除數(shù)據(jù)parosition*/public void removeData(ition) datas.remove(ition); notifyItemRemoved(ition);代碼如下:133_點(diǎn)擊事件14private void setLitener() btn_add.setOnClickListener(new View.OnClickListener() Overridepublic void onClick(View v) 9_設(shè)置刪除某條和增加某條的動畫沒有設(shè)置的情況是默認(rèn)有動畫的,也可以自己設(shè)置15myAdapter.addData(

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論