Android列表RecyclerView排列布局
本文實(shí)例為大家分享了Android列表RecyclerView排列布局的具體代碼,供大家參考,具體內(nèi)容如下
效果圖:
1.要添加相關(guān)的依賴
implementation 'androidx.recyclerview:recyclerview:1.1.0'
2.然后布局文件中準(zhǔn)備容器
這個(gè)標(biāo)簽是顯示目標(biāo)容器對象的,其他需求可自定義
<androidx.recyclerview.widget.RecyclerView android:id="@+id/rv_list" android:layout_width="match_parent" android:layout_height="wrap_content"> </androidx.recyclerview.widget.RecyclerView>
3.制作模板顯示
這里的模板是自定義的,需要什么樣的就怎么布局,這里只是模板,后面做好就會根據(jù)模板生成樣式的
4.寫一個(gè)適配器,用于加載數(shù)據(jù)
public class ShiftInForMationAdapter extends RecyclerView.Adapter<ShiftInForMationAdapter.ShiftInForMation>{ private List<ShiftInFormationBean> mListData;//需要加載的數(shù)據(jù) private Activity mActivityContext; //上下文 private MyApplication myApplication;//全局Application對象 private OnItemClickListener onItemClickListener;//存放點(diǎn)擊事件的實(shí)現(xiàn)類 //存放點(diǎn)擊事件的實(shí)現(xiàn)類 public interface OnItemClickListener{ //整條數(shù)據(jù)的點(diǎn)擊事件 void onItemClick(View view,ShiftInFormationBean shift,int position); //點(diǎn)擊》事件 void onImageClick(View view,ShiftInFormationBean shift,int position); } /** * 構(gòu)造方法 獲取需要的參數(shù) * @param mListData 需要的數(shù)據(jù) * @param mActivityContext 上下文 * @param myApplication app全局變量 */ public ShiftInForMationAdapter(List<ShiftInFormationBean> mListData ,Activity mActivityContext,MyApplication myApplication){ this.mActivityContext = mActivityContext; this.mListData = mListData; this.myApplication = myApplication; } //設(shè)置點(diǎn)擊事件方法 public void setItemClickListener(OnItemClickListener onItemClickListener){ this.onItemClickListener = onItemClickListener; } /** * 生命周期方法 創(chuàng)建 * 創(chuàng)建ViewHolder 設(shè)置RecyclerViewItem布局 * @param parent * @param viewType * @return */ @NonNull @Override public ShiftInForMation onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_search_result,parent,false); return new ShiftInForMation(view); } /** * 綁定數(shù)據(jù) * @param holder 顯示的列表具體信息的布局 * @param position */ @Override public void onBindViewHolder(@NonNull ShiftInForMation holder, int position) { ShiftInFormationBean shift = mListData.get(position); if(shift != null){ //余座剩余小于1 灰色 if(Integer.valueOf(shift.getSearchSupus()) < 1){ //沒有余座,設(shè)置為灰色 holder.tvShiftName.setTextColor(mActivityContext.getResources().getColor(R.color.colorGray)); } holder.tvShiftName.setText("班次名稱:" +shift.getShiftName()); //==設(shè)置班次名稱 BigDecimal discount = BigDecimal.valueOf(0.98);//默認(rèn) if(myApplication.isLogin()){ discount = new BigDecimal(myApplication.getLoginUser().getMemberDiscount()); } //設(shè)置事件 if(onItemClickListener != null){ //設(shè)置整條數(shù)據(jù)的點(diǎn)擊事件 holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onItemClickListener.onItemClick(v,shift,position); } }); } } } //告訴RecyclerView有多少條數(shù)據(jù) @Override public int getItemCount() { return mListData.size(); } /** * 對外方法,用于分頁添加數(shù)據(jù) * @param listAdd 要添加的數(shù)據(jù) * @param loadPage 加載的頁數(shù) */ public void addItem(List<ShiftInFormationBean> listAdd){ //如果是第一頁 需要先清空數(shù)據(jù)列表 this.mListData.clear(); //添加數(shù)據(jù) if(listAdd != null){ this.mListData.addAll(listAdd); } //通知RecyclerView進(jìn)行改變--整體 notifyDataSetChanged(); } //獲取列表數(shù)據(jù)控件 static class ShiftInForMation extends RecyclerView.ViewHolder{ TextView tvShiftName ;//班次名稱 public ShiftInForMation(@NonNull View itemView) { super(itemView); tvShiftName = itemView.findViewById(R.id.tv_item_result_roomTypeInfo); } } }
5.使用RecyclerView 初始化
//===1、設(shè)置布局控制器 //=1.1、創(chuàng)建布局管理器 LinearLayoutManager layoutManager = new LinearLayoutManager(mActivityContext); //=1.2、設(shè)置為垂直排列,用setOrientation方法設(shè)置(默認(rèn)為垂直布局) layoutManager.setOrientation(LinearLayoutManager.VERTICAL); //=1.3、設(shè)置recyclerView的布局管理器 rvOrderList.setLayoutManager(layoutManager); //==2、實(shí)例化適配器 //=2.1、初始化適配器 List<TicketPurchaseVoBean> mListData = new ArrayList<>(); ticketPurchaseAdapter = new TicketPurchaseAdapter(mActivityContext,mListData); //=2.2設(shè)置列表項(xiàng)點(diǎn)擊事件 //=2.2設(shè)置列表項(xiàng)點(diǎn)擊事件 ticketPurchaseAdapter.setOnItemClickListener(new TicketPurchaseAdapter.OnItemClickListener() { //方法里的參數(shù)都是可以在適配器選擇的 @Override public void onItemClick(View view, TicketPurchaseVoBean data, int position) { //寫需求 } }); //=2.3、設(shè)置recyclerView的適配器 rvOrderList.setAdapter(ticketPurchaseAdapter);
到這里就完成了,點(diǎn)擊事件 加載數(shù)據(jù)的方法里面都是可以按需求自定義的,布局也是可按需要來布局的,這里給的只是一個(gè)模板。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android RecyclerView實(shí)現(xiàn)多種item布局的方法
- Android RecyclerView顯示Item布局不一致解決辦法
- Android RecyclerView加載不同布局簡單實(shí)現(xiàn)
- Android開發(fā)中RecyclerView模仿探探左右滑動布局功能
- Android RecyclerView布局就這么簡單
- Android RecyclerView加載兩種布局的方法
- Android RecyclerView網(wǎng)格布局示例解析
- Android RecyclerView多類型布局卡片解決方案
- Android實(shí)現(xiàn)RecyclerView嵌套流式布局的詳細(xì)過程
相關(guān)文章
flutter中的網(wǎng)絡(luò)請求數(shù)據(jù)獲取詳解
這篇文章主要為大家介紹了flutter中的網(wǎng)絡(luò)請求數(shù)據(jù)獲取示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01flutter實(shí)現(xiàn)掃碼槍獲取數(shù)據(jù)源禁止系統(tǒng)鍵盤彈窗示例詳解
這篇文章主要為大家介紹了flutter實(shí)現(xiàn)掃碼槍獲取數(shù)據(jù)源禁止系統(tǒng)鍵盤彈窗示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01android中colors.xml顏色設(shè)置資源文件的方法
這篇文章主要介紹了android中colors.xml顏色設(shè)置資源文件,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03Win10下Android App安裝配置開發(fā)環(huán)境
這篇文章主要為大家詳細(xì)介紹了Win10下Android App安裝配置開發(fā)環(huán)境,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07使用PlatformView將?Android?控件view制作成Flutter插件
這篇文章主要為大家介紹了使用PlatformView將?Android?控件view制作成Flutter插件實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11如何使用Matrix對bitmap的旋轉(zhuǎn)與鏡像水平垂直翻轉(zhuǎn)
本篇文章是對使用Matrix對bitmap的旋轉(zhuǎn)與鏡像水平垂直翻轉(zhuǎn)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06Android自定義控件(實(shí)現(xiàn)視圖樹繪制指示器)
本文主要介紹了Android視圖樹繪制指示器的實(shí)現(xiàn)原理和具體步驟。具有一定的參考價(jià)值,下面跟著小編一起來看下吧2017-01-01Android編程實(shí)現(xiàn)懸浮窗獲取并顯示當(dāng)前內(nèi)存使用量的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)懸浮窗獲取并顯示當(dāng)前內(nèi)存使用量的方法,涉及Android針對窗口及內(nèi)存的相關(guān)操作技巧,需要的朋友可以參考下2017-07-07Android Activity 與Service進(jìn)行數(shù)據(jù)交互詳解
這篇文章主要介紹了Android Activity 與Service進(jìn)行數(shù)據(jù)交互的相關(guān)資料,在開發(fā)Android App的時(shí)候經(jīng)常會使用這樣的功能,需要的朋友可以參考下2016-10-10