Fragment中RecyclerView的使用解析,以及監聽事件處理
阿新 • • 發佈:2019-01-03
RecyclerView是可以代替listview使用的新元件,個人感覺其主要特色:其介面卡adapter中,重寫的東西少了,頁面展示的效果跟加多了,比如可以在RecyclerView設定listview的顯示效果,也可以設定gridview的顯示效果,也可以設定瀑布流的顯示效果!下面程式碼主要是針對Fragment中RecyclerView元件中listview效果進行學習,在此做簡單總結記錄!
第一步:在fragment對應的佈局xml檔案中使用recyclerview
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" android:orientation="vertical"> <android.support.v7.widget.RecyclerView android:id="@+id/collect_recyclerView" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </LinearLayout>
簡單解釋:此處佈局根據具體業務進行屬性設定,我需要recyclerview充滿整個fragment
需要注意的是recyclerview是v7包中的元件,需要提前匯入v7包,有的可能在使用的時候不會出現提醒,但是我使用的匯入v7包的Android studio是有提示的!
第二步:在fragment中定義使用
public class CollectFragment extends Fragment { private View view;//定義view用來設定fragment的layout public RecyclerView mCollectRecyclerView;//定義RecyclerView //定義以goodsentity實體類為物件的資料集合 private ArrayList<GoodsEntity> goodsEntityList = new ArrayList<GoodsEntity>(); //自定義recyclerveiw的介面卡 private CollectRecycleAdapter mCollectRecyclerAdapter; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { //獲取fragment的layout view = inflater.inflate(R.layout.collect_page, container, false); //對recycleview進行配置 initRecyclerView(); //模擬資料 initData(); return view; } /** * TODO 模擬資料 */ private void initData() { for (int i=0;i<10;i++){ GoodsEntity goodsEntity=new GoodsEntity(); goodsEntity.setGoodsName("模擬資料"+i); goodsEntity.setGoodsPrice("100"+i); goodsEntityList.add(goodsEntity); } } /** * TODO 對recycleview進行配置 */ private void initRecyclerView() { //獲取RecyclerView mCollectRecyclerView=(RecyclerView)view.findViewById(R.id.collect_recyclerView); //建立adapter mCollectRecyclerAdapter = new CollectRecycleAdapter(getActivity(), goodsEntityList); //給RecyclerView設定adapter mCollectRecyclerView.setAdapter(mCollectRecyclerAdapter); //設定layoutManager,可以設定顯示效果,是線性佈局、grid佈局,還是瀑布流佈局 //引數是:上下文、列表方向(橫向還是縱向)、是否倒敘 mCollectRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false)); //設定item的分割線 mCollectRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL)); //RecyclerView中沒有item的監聽事件,需要自己在介面卡中寫一個監聽事件的介面。引數根據自定義 mCollectRecyclerAdapter.setOnItemClickListener(new CollectRecycleAdapter.OnItemClickListener() { @Override public void OnItemClick(View view, GoodsEntity data) { //此處進行監聽事件的業務處理 Toast.makeText(getActivity(),"我是item",Toast.LENGTH_SHORT).show(); } }); } }
第三步設定RecyclerView的介面卡
public class CollectRecycleAdapter extends RecyclerView.Adapter<CollectRecycleAdapter.myViewHodler> { private Context context; private ArrayList<GoodsEntity> goodsEntityList; //建立建構函式 public CollectRecycleAdapter(Context context, ArrayList<GoodsEntity> goodsEntityList) { //將傳遞過來的資料,賦值給本地變數 this.context = context;//上下文 this.goodsEntityList = goodsEntityList;//實體類資料ArrayList } /** * 建立viewhodler,相當於listview中getview中的建立view和viewhodler * * @param parent * @param viewType * @return */ @Override public myViewHodler onCreateViewHolder(ViewGroup parent, int viewType) { //建立自定義佈局 View itemView = View.inflate(context, R.layout.item_layout, null); return new myViewHodler(itemView); } /** * 繫結資料,資料與view繫結 * * @param holder * @param position */ @Override public void onBindViewHolder(myViewHodler holder, int position) { //根據點選位置繫結資料 GoodsEntity data = goodsEntityList.get(position); // holder.mItemGoodsImg; holder.mItemGoodsName.setText(data.goodsName);//獲取實體類中的name欄位並設定 holder.mItemGoodsPrice.setText(data.goodsPrice);//獲取實體類中的price欄位並設定 } /** * 得到總條數 * * @return */ @Override public int getItemCount() { return goodsEntityList.size(); } //自定義viewhodler class myViewHodler extends RecyclerView.ViewHolder { private ImageView mItemGoodsImg; private TextView mItemGoodsName; private TextView mItemGoodsPrice; public myViewHodler(View itemView) { super(itemView); mItemGoodsImg = (ImageView) itemView.findViewById(R.id.item_goods_img); mItemGoodsName = (TextView) itemView.findViewById(R.id.item_goods_name); mItemGoodsPrice = (TextView) itemView.findViewById(R.id.item_goods_price); //點選事件放在adapter中使用,也可以寫個介面在activity中呼叫 //方法一:在adapter中設定點選事件 itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //可以選擇直接在本位置直接寫業務處理 //Toast.makeText(context,"點選了xxx",Toast.LENGTH_SHORT).show(); //此處回傳點選監聽事件 if(onItemClickListener!=null){ onItemClickListener.OnItemClick(v, goodsEntityList.get(getLayoutPosition())); } } }); } } /** * 設定item的監聽事件的介面 */ public interface OnItemClickListener { /** * 介面中的點選每一項的實現方法,引數自己定義 * * @param view 點選的item的檢視 * @param data 點選的item的資料 */ public void OnItemClick(View view, GoodsEntity data); } //需要外部訪問,所以需要設定set方法,方便呼叫 private OnItemClickListener onItemClickListener; public void setOnItemClickListener(OnItemClickListener onItemClickListener) { this.onItemClickListener = onItemClickListener; } }
自定義實體類簡單展示:
public class GoodsEntity implements Serializable {
public String imgPath;//圖片地址
public String goodsName;//貨物名稱
public String goodsPrice;//貨物價格
public GoodsEntity() {
}
public GoodsEntity(String imgPath, String goodsName, String goodsPrice) {
this.imgPath = imgPath;
this.goodsName = goodsName;
this.goodsPrice = goodsPrice;
}
public String getImgPath() {
return imgPath;
}
public void setImgPath(String imgPath) {
this.imgPath = imgPath;
}
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
public String getGoodsPrice() {
return goodsPrice;
}
public void setGoodsPrice(String goodsPrice) {
this.goodsPrice = goodsPrice;
}
@Override
public String toString() {
return "GoodsEntity{" +
"imgPath='" + imgPath + '\'' +
", goodsName='" + goodsName + '\'' +
", goodsPrice='" + goodsPrice + '\'' +
'}';
}
}
顯示效果展示:
相關fragment xml adapter entity檔案下載