原标题:安卓当下最流行的吸顶效果的实现(上)
开始逐渐领略到ItemDecoration的美~
今天让我 使用 ItemDecoration 来完成 可推动的悬浮导航栏的效果,最终实现的效果如下图:
具体实现步骤如下:
根据我前面的文章所讲的RecyclerView的基本使用,我们先来完成基本的recyclerView:
第一步:布局里写一个RecyclerView
第二步:实例化
recyclerView=(RecyclerView)findViewById(R.id.recyclerView);
第三步:获取所需的数据 (这里我们来个真实点的情景,去联网请求数据)
/**
* 联网请求所需的url
*/
publicStringurl="http://api.meituan.com/mmdb/movie/v2/list/rt/order/coming.json?ci=1&limit=12&token=&__vhost=api.maoyan.com&utm_campaign=AmovieBmovieCD-1&movieBundleVersion=6801&utm_source=xiaomi&utm_medium=android&utm_term=6.8.0&utm_content=868030022327462&net=255&dModel=MI%205&uuid=0894DE03C76F6045D55977B6D4E32B7F3C6AAB02F9CEA042987B380EC5687C43&lat=40.100673&lng=116.378619&__skck=6a375bce8c66a0dc293860dfa83833ef&__skts=1463704714271&__skua=7e01cf8dd30a179800a7a93979b430b2&__skno=1a0b4a9b-44ec-42fc-b110-ead68bcc2824&__skcy=sXcDKbGi20CGXQPPZvhCU3%2FkzdE%3D";
//联网获取数据
getDataFromNet();
/**
* 使用okhttpUtils进行联网请求数据
*/
privatevoidgetDataFromNet(){
OkHttpUtils.
get()
.url(url)
.build()
.execute(newStringCallback(){
@Override
publicvoid(okhttp3.Callcall,Exceptione,intid){
Log.e("TAG","联网失败"+e.getMessage());
}
@Override
publicvoidonResponse(Stringresponse,intid){
Log.e("TAG","联网成功=="+response);
//联网成功后使用fastjson解析
processData(response);
}
});
}
/**
* 使用fastjson进行解析
*
* @param json
*/
privatevoidprocessData(Stringjson){
//这里使用GsonFormat生成对应的bean类
JSONObjectjsonObject=parseObject(json);
Stringdata=jsonObject.getString("data");
JSONObjectdataObj=JSON.parseObject(data);
Stringcoming=dataObj.getString("coming");
Listcomingslist=parseArray(coming,WaitMVBean.DataBean.ComingBean.class);
//测试是否解析数据成功
// String strTest = comingslist.get(0).getCat();
// Log.e("TAG", strTest + "222");
//解析数据成功,设置适配器-->
}
}
第四步:解析数据成功后,创建并设置适配器,并传递相关数据
//解析数据成功,设置适配器
MyRecyclerAdapteradapter=newMyRecyclerAdapter(mContext,comingslist);
recyclerView.setAdapter(adapter);
适配器:
publicclassMyRecyclerAdapterextendsRecyclerView.Adapter{
privatefinalListcomingslist;
privatefinalContextmContext;
privatefinalLayoutInflatermLayoutInflater;
publicMyRecyclerAdapter(ContextmContext,Listcomingslist){
this.mContext=mContext;
this.comingslist=comingslist;
mLayoutInflater=LayoutInflater.from(mContext);
}
@Override
publicRecyclerView.ViewHolder onCreateViewHolder(ViewGroupparent,intviewType){
returnnewMyViewHolder(mLayoutInflater.inflate(R.layout.date_item,null));
}
@Override
publicvoidonBindViewHolder(RecyclerView.ViewHolderholder,intposition){
MyViewHoldermyholder=(MyViewHolder)holder;
myholder.setData(position);
}
@Override
publicintgetItemCount(){
returncomingslist.size();
}
classMyViewHolderextendsRecyclerView.ViewHolder{
privateTextViewmv_name;
privateTextViewmv_dec;
privateTextViewmv_date;
privateImageViewimageView;
publicMyViewHolder(ViewitemView){
super(itemView);
mv_name=(TextView)itemView.findViewById(R.id.mv_name);
mv_dec=(TextView)itemView.findViewById(R.id.mv_dec);
mv_date=(TextView)itemView.findViewById(R.id.mv_date);
imageView=(ImageView)itemView.findViewById(R.id.image);
}
publicvoidsetData(intposition){
WaitMVBean.DataBean.ComingBeancoming=comingslist.get(position);
Stringname=coming.getNm();
mv_name.setText(name);
Stringdate=coming.getShowInfo();
mv_date.setText(date);
Stringdec=coming.getScm();
mv_dec.setText(dec);
//注:当你发下图片无法打开是,做个字符串替换即可
StringimagUrl=coming.getImg();
StringnewImagUrl=imagUrl.replaceAll("w.h","50.80");
//使用Glide加载图片
Glide.with(mContext)
.load(newImagUrl)
.into(imageView);
}
}
}
item的布局:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff"
android:gravity="center_vertical"
android:orientation="horizontal">
android:id="@+id/image"
android:layout_width="70dp"
android:layout_height="110dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="8dp"
android:layout_marginTop="5dp"/>
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:layout_weight="1"
android:orientation="vertical">
android:id="@+id/mv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="神奇動物在哪裏"
android:textColor="#000000"
android:textSize="15sp"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="观众"
android:textColor="#55000000"
android:textSize="14sp"/>
android:id="@+id/tv_people"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9.0 "
android:textColor="#FFCE42"
android:textSize="18sp"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" | 专业"
android:textColor="#55000000"
android:textSize="14sp"/>
android:id="@+id/tv_professional"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6.7"
android:textColor="#FFCE42"
android:textSize="18sp"/>
android:id="@+id/mv_dec"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="神奇動物城,法師顯超能"
android:textColor="#99000000"
android:textSize="11sp"/>
android:id="@+id/mv_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="今天165家影院放映2088场"
android:textColor="#99000000"
android:textSize="11sp"/>
第五步:一定不能忘!!!
recycleView不仅要设置适配器还要设置布局管理者,否则图片不显示
GridLayoutManagermanager=newGridLayoutManager(this,1);
recyclerView.setLayoutManager(manager);
此时RecyclerView简单的完成效果如下:
下面开始做 可推动的 悬浮导航栏:
来自:李诗雨
http://blog.csdn.net/cjm2484836553/article/details/53453982
程序员大咖整理发布,转载请联系作者获得授权
责任编辑: