日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android studio 微信APP之Fragment中使用ReclerView

發(fā)布時間:2024/3/26 Android 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android studio 微信APP之Fragment中使用ReclerView 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Android studio 微信APP之Fragment中使用ReclerView

如題,本次實驗的內(nèi)容就是在已經(jīng)創(chuàng)建好的微信程序的首頁處,在fragment控件中增加ReclerView控件,實現(xiàn)首頁內(nèi)容的多樣化(微信首頁的制作參考:微信程序首頁)
首先還是對布局進行一個說明:

在fragment對應的layout中添加ReclerView控件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="wrap_content"><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/rv_main"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="#BBBBBB"/></LinearLayout>

這一步僅僅是將控件添加到fragment中,而并沒有對ReclerView進行布局。
因此,我們還要對ReclerView進行布局

ReclerView的布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="#ffffff"> <!--左側(cè)的圖片布局盒子--><LinearLayoutandroid:id="@+id/ll_1"android:layout_width="wrap_content"android:layout_height="125dp"android:gravity="center"><ImageViewandroid:id="@+id/item_goods_img"android:layout_width="100dp"android:layout_height="100dp"android:layout_margin="10dp"android:src="@mipmap/picture1"android:background="#4D2BD5"/></LinearLayout> <!--右側(cè)文字盒子布局--><LinearLayoutandroid:id="@+id/ll_2"android:layout_width="match_parent"android:layout_height="125dp"android:orientation="vertical"><!--名字TextView所在盒子布局--><LinearLayoutandroid:id="@+id/ll_2_name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:layout_marginBottom="10dp"><TextViewandroid:id="@+id/item_goods_nametitle"android:layout_width="90dp"android:layout_height="25dp"android:text=""android:textSize="16sp"android:textColor="#000000" /><TextViewandroid:id="@+id/item_goods_name"android:layout_width="190dp"android:layout_height="25dp"android:text=""android:textSize="20sp"android:textColor="#000000" /></LinearLayout><!--價格TextView所在盒子布局--><LinearLayoutandroid:id="@+id/ll_2_price"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"><TextViewandroid:id="@+id/item_goods_pricetitle"android:layout_width="90dp"android:layout_height="25dp"android:text=""android:textSize="16sp"android:textColor="#000000" /><TextViewandroid:id="@+id/item_goods_price"android:layout_width="190dp"android:layout_height="25dp"android:text=""android:textSize="20sp"android:textColor="#000000" /></LinearLayout></LinearLayout> </LinearLayout>

接下來就是對控件的函數(shù)控制了

首先,我們需要為需要引用的數(shù)據(jù)建立一個類,來提供數(shù)據(jù)修改的接口

GoodsEntity.java:
class GoodsEntity implements Serializable {public String imgPath;//圖片地址public String goodsName;//貨物名稱public String goodsPrice;//貨物價格public String goodsNameTitle;//商品名稱標簽public String goodsPriceTitle;//商品價格標簽public GoodsEntity() {}public GoodsEntity(String imgPath, String goodsName, String goodsPrice,String goodsNameTitle,String goodsPriceTitle) {this.imgPath = imgPath;this.goodsName = goodsName;this.goodsPrice = goodsPrice;this.goodsNameTitle = goodsNameTitle;this.goodsPriceTitle = goodsPriceTitle;}//圖片的路徑獲取方法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 getGoodsNameTitle(){return goodsNameTitle;}public void setGoodsNameTitle(String goodsNameTitle){this.goodsNameTitle = goodsNameTitle;}//商品價格標簽的獲取方法public String getGoodsPriceTitle(){return goodsPriceTitle;}public void setGoodsPriceTitle(String goodsPriceTitle){this.goodsPriceTitle = goodsPriceTitle;}//商品價格的獲取方法public String getGoodsPrice() {return goodsPrice;}public void setGoodsPrice(String goodsPrice) {this.goodsPrice = goodsPrice;}@Overridepublic String toString() {return "GoodsEntity{" +"imgPath='" + imgPath + '\'' +", goodsName='" + goodsName + '\'' +", goodsNameTitle='" + goodsNameTitle + '\'' +", goodsPrice='" + goodsPrice + '\'' +", goodsPriceTitle='" + goodsPriceTitle + '\'' +'}';}}

沒有什么好說的,就是定義變量,get變量,set變量,別命名錯了就行了

隨后,便是重點,我們要創(chuàng)建ReclerView的adapter了
(也就是這里,我們可以控制ReclerView的展現(xiàn)形式,我這里采用的最簡單的線性排布)

LinearAdapter:
class LinearAdapter extends RecyclerView.Adapter<LinearAdapter.myViewHolder> {private OnItemClickListener onItemClickListener;private Context context;private ArrayList<GoodsEntity> goodsEntities;public LinearAdapter(Context context,ArrayList<GoodsEntity> goodsEntities){this.context = context;this.goodsEntities = goodsEntities;}@NonNull@Overridepublic LinearAdapter.myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {View itemView = View.inflate(context, R.layout.layout_linear_item,null);return new myViewHolder(itemView);}@Overridepublic void onBindViewHolder(@NonNull LinearAdapter.myViewHolder holder, int position) {GoodsEntity data = goodsEntities.get(position);holder.mItemGoodsName.setText(data.goodsName);holder.mItemGoodsPrice.setText(data.goodsPrice);holder.mItemGoodsNameTitle.setText(data.goodsNameTitle);holder.mItemGoodsPriceTitle.setText(data.goodsPriceTitle);}@Overridepublic int getItemCount() {return goodsEntities.size();}class myViewHolder extends RecyclerView.ViewHolder {//定義控件private ImageView mItemGoodsImg;private TextView mItemGoodsName;private TextView mItemGoodsPrice;private TextView mItemGoodsNameTitle;private TextView mItemGoodsPriceTitle;public myViewHolder(@NonNull View itemView) {super(itemView);//找到控件mItemGoodsImg = itemView.findViewById(R.id.item_goods_img);mItemGoodsName = itemView.findViewById(R.id.item_goods_name);mItemGoodsPrice = itemView.findViewById(R.id.item_goods_price);mItemGoodsNameTitle = itemView.findViewById(R.id.item_goods_nametitle);mItemGoodsPriceTitle = itemView.findViewById(R.id.item_goods_pricetitle);//設置點擊事件itemView.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if (onItemClickListener!=null){onItemClickListener.OnItemClick(v,goodsEntities.get(getLayoutPosition()));}}});}}//設置點擊事件監(jiān)聽器public interface OnItemClickListener {public void OnItemClick(View view, GoodsEntity data);}public void setOnItemClickListener(OnItemClickListener onItemClickListener) {this.onItemClickListener = onItemClickListener;} }

最后就是對fragment的類的函數(shù)編寫

weixinFragment:
public class weixinFragment extends Fragment {private RecyclerView mRvMain; //定義ReclerView控件private View view;//定義view來設置fragment中的layoutprivate ArrayList<GoodsEntity> goodsEntities = new ArrayList<GoodsEntity>();private LinearAdapter mLinearAdapter;public weixinFragment() {// Required empty public constructor}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// Inflate the layout for this fragmentview = inflater.inflate(R.layout.tab01, container, false);initRecyclerView();initData();return view;}//private void initRecyclerView() {mRvMain = (RecyclerView)view.findViewById(R.id.rv_main);mLinearAdapter = new LinearAdapter(getActivity(),goodsEntities);mRvMain.setAdapter(mLinearAdapter);mRvMain.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));mRvMain.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL));mLinearAdapter.setOnItemClickListener(new LinearAdapter.OnItemClickListener() {@Overridepublic void OnItemClick(View view, GoodsEntity data) {Toast.makeText(getActivity(),"圖片售罄",Toast.LENGTH_SHORT).show();}});}//private void initData(){for (int i=0;i<10;i++){GoodsEntity goodsEntity = new GoodsEntity();goodsEntity.setGoodsNameTitle(" 圖片名稱:");goodsEntity.setGoodsName("圖片序號"+i);goodsEntity.setGoodsPriceTitle(" 圖片價格:");goodsEntity.setGoodsPrice("1"+i*100+"RMB");goodsEntities.add(goodsEntity);}}}

至此 大功告成,值得一提的是,在這個項目中,我設置的圖片是直接去一張圖片來顯示,也可以在fragment中設置文件路徑,然后我們對圖片的命名采用迭代的命名方式(就是img1、img2這樣子一直下去),然后在initData函數(shù)中使用goodsEntity.setImgPath("…/mipmap/"+i);的方式來一次獲取圖片。。。
但是圖片太難找了。。。emmm所以這里就直接用一張圖片放上去意思一下算了
那么最后,附上整個項目的源碼,需要的碼云自取
碼云倉庫

總結(jié)

以上是生活随笔為你收集整理的Android studio 微信APP之Fragment中使用ReclerView的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 欧美在线观看一区二区三区 | 狠狠人妻久久久久久综合蜜桃 | 永久免费在线 | 狠狠丁香| 日韩精品中字 | 岛国午夜视频 | www麻豆视频 | 成人一级在线 | 青青视频在线播放 | 沈樵精品国产成av片 | 伊人欧美| 欧美一区二区在线播放 | 91色精品 | 性欧美长视频 | 国产99视频在线 | 亚洲你懂得 | 亚洲综合视频在线播放 | 国产做a | 天堂va蜜桃 | 免费黄色视屏 | 黑人巨大精品欧美一区二区 | 国产极品美女在线 | 男生插女生视频在线观看 | 精品无码人妻一区二区三区 | 国产亚洲精品自拍 | 星空大象在线观看免费播放 | 国产中文字幕精品 | 日韩一二三四五区 | 懂色av一区二区三区四区五区 | 亚洲天堂高清 | 伊人久久久久久久久 | 欧美一区二区三区成人片在线 | 少妇无码吹潮 | 色播网址 | 亚洲精品久久久久久一区二区 | 粗喘呻吟撞击猛烈疯狂 | 男女互操视频 | 全黄一级裸片视频 | 免费在线不卡av | 亚洲精品一区二区三区蜜桃久 | 国产一二在线 | 娇妻被老王脔到高潮失禁视频 | 成人午夜激情 | 欧美午夜一区二区三区 | 欧美日韩你懂的 | 国产一级特黄毛片 | 尤物视频免费在线观看 | 亚洲视频在线观看一区 | 午夜一区二区三区 | 天堂视频在线观看免费 | 欧美a级肉欲大片xxx | 奇米影视盒| 亚洲av毛片一区二二区三三区 | a级在线观看 | 国产精品亚洲第一 | 嫩草视频在线观看 | 自由 日本语 热 亚洲人 | 国产精品自产拍高潮在线观看 | 丁香花电影免费播放电影 | brazzers精品成人一区 | 一区二区三区四区亚洲 | 精品久草 | 日韩插插插| 黄色av中文字幕 | 亚洲欧美日韩色图 | 人人干狠狠干 | 九色蝌蚪视频 | 丁五月| 国产九九 | 97在线观看免费高清 | 无遮挡的裸体按摩的视频 | 日韩网站免费观看 | 午夜福利一区二区三区 | 欧美高清大白屁股ass18 | 国产一级美女 | 成人午夜在线免费观看 | 亚洲综合区 | 久久精品国产亚洲av蜜臀色欲 | 日本一区二区在线 | 久久久久久69 | 天堂аⅴ在线最新版在线 | 综合色在线观看 | 亚洲ⅴ国产v天堂a无码二区 | 欧美一区二区三区激情啪啪 | 国产精品自拍第一页 | 天天插天天操天天干 | 亚洲成熟少妇视频在线观看 | 国产精品久久久影院 | 国产一区二区三区在线观看视频 | 嫩草导航 | 成人看片在线 | 国产精品久久久久久久久免费软件 | 狠狠操一区二区 | 日韩美女视频19 | jizz成熟丰满老女人 | 国产精品欧美久久久久久 | 丰满人妻一区二区三区53 | 韩日黄色片 | 国产精品久久久久久久久久免费看 |