公共类ImgAdapter扩展了RecyclerView.Adapter {
Context context;
List <Shop.DataBean.Ad1Bean> list;
public ImgAdapter(Context context,List <Shop.DataBean.Ad1Bean> list){
this.context = context;
this.list =列表;
} @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType){ //引入轮播图布局 View view = LayoutInflater.from(parent.getContext())。inflate(R.layout.img_item,parent,false); 返回新的MyViewHolder(查看); } @覆盖 公共无效onBindViewHolder(RecyclerView.ViewHolder保持器,INT位置){
MyViewHolder myViewHolder =(MyViewHolder)持有者; //为轮播图赋图片myViewHolder.item_sdv.setImageURI(list.get(位置%则为则为list.size())的的getImage()); } @Override public int getItemCount(){return Integer.MAX_VALUE; } MyViewHolder类扩展了RecyclerView.ViewHolder {private final SimpleDraweeView item_sdv; public MyViewHolder(View view){super(view); //找到布局文件中的ID item_sdv = view.findViewById(R.id.item_sdv); }}} ```111 ```0公共类MsAdapter延伸RecyclerView.Adapter {语境上下文; List <Shop.DataBean.DefaultGoodsListBean> list; public MsAdapter(Context context,List <Shop.DataBean.DefaultGoodsListBean> list){ this.context = context; this.list =列表; } @Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//引入秒杀布局文件
View view = View.inflate(context,R.layout.ms_item,null);
MyViewHolder myViewHolder = new MyViewHolder(view); 返回myViewHolder; } @覆盖 公共无效onBindViewHolder(RecyclerView.ViewHolder保持器,INT位置){ MyViewHolder myViewHolder =(MyViewHolder)保持器; //为秒杀赋值 myViewHolder.rcv_tv.setText( “¥” + list.get(位置).getShop_price()); myViewHolder.rcv_sdv.setImageURI(list.get(位置).getGoods_img());
} @Override public int getItemCount(){return list.size(); } MyViewHolder类扩展了RecyclerView.ViewHolder {private final SimpleDraweeView rcv_sdv; 私人决赛TextView rcv_tv; public MyViewHolder(View view){super(view); //找到秒杀布局中的控件ID rcv_sdv = view.findViewById(R.id.rcv_sdv); rcv_tv = view.findViewById(R.id.rcv_tv); }}} ```222 ```0 公共类应用程序的应用延伸{ @覆盖 公共无效的onCreate(){ super.onCreate(); //初始化壁画使用默认配置 Fresco.initialize(本); } } ```
333
```0
public interface DataModel {
//请求数据的方法
void getData(String url, DataPresenter presenter);
}
public class MyDataModel implements DataModel {
//请求数据的方法
@Override
public void getData(String url, final DataPresenter presenter) {
OkHttpUtils.doGet(url, new Callback() {
@Override
public void onFailure(Call call, IOException e) {}
@Override
public void onResponse(Call call, Response response) throws IOException {
//接收返回的数据
String json = response.body()。string(); //进行解析得到集合Gson gson = new Gson(); 购买myDataBean = gson.fromJson(json,Shop.class); Shop.DataBean data = myDataBean.getData(); //调用P层方法将数据传给P层presenter.Success(数据); }}; }} ```444 ```1 公共接口DataPresenter { //成功与失败的方法 空隙成功(Shop.DataBean数据); void Error(); } 公共类MyDataPresenter实现DataPresenter { DataView的数据视图; private final MyDataModel myDataModel; public MyDataPresenter(DataView dataView){
this.dataView =数据视图;
myDataModel = new MyDataModel();
} //成功 @Override public void Success(Shop.DataBean data){ dataView.toBackHome(data); } //失败 @Override public void Error(){} public void netWork(String url){ myDataModel.getData(url,this); } } 公共接口数据视图{ //接收数据的方法 的空隙toBackHome(Shop.DataBean数据); } ```
555
```0
public class MainActivity extends Activity implements DataView {
String url = "http://result.eolinker.com/umIPmfS6c83237d9c70c7c9510c9b0f97171a308d13b611?uri=homepage";
private List<Shop.DataBean.Ad1Bean> imglist;
private int item=5;
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
//接收数据
Shop.DataBean data = (Shop.DataBean) msg.obj;
//设置轮播图 进入轮播图适配器
imglist = data.getAd1();
ImgAdapter imgAdapter = new ImgAdapter(MainActivity.this, imglist);
final LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, false);
imgRcv.setLayoutManager(layoutManager);
imgRcv.setHasFixedSize(true);
imgRcv.setAdapter(imgAdapter);
PagerSnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(imgRcv);
imgRcv.scrollToPosition(imglist.size() * 10);
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
imgRcv.smoothScrollToPosition(layoutManager.findFirstVisibleItemPosition() + 1);
}
}, 2000, 2000, TimeUnit.MILLISECONDS);
//设置秒杀,进入秒杀适配器
List<Shop.DataBean.DefaultGoodsListBean> defaultGoodsList = data.getDefaultGoodsList();
MsAdapter adapter = new MsAdapter(MainActivity.this, defaultGoodsList);
myRcv.setAdapter(adapter);
}
};
Handler handler2 = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what==0){
item--;
/*tvItem.setText("秒杀剩余时间"+item+"s");
handler2.sendEmptyMessageDelayed(0,1000);*/
}
/*if(item==0){
//实现页面跳转
Intent intent = new Intent(MainActivity.this,MsActivity.class);
startActivity(intent);
finish();
}*/
}
};
@BindView(R.id.my_rcv)
RecyclerView myRcv;
@BindView(R.id.img_rcv)
RecyclerView imgRcv;
@BindView(R.id.tv_item)
TextView tvItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
//实例化P层
MyDataPresenter myDataPresenter = new MyDataPresenter(this);
myDataPresenter.netWork(url);
//设置管理器
myRcv.setLayoutManager(new LinearLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, false));
//设置倒计时跳转页面
handler2.sendEmptyMessageDelayed(0,1000);
}
//接收数据的方法
@Override
public void toBackHome(Shop.DataBean data) {
Message msg = new Message();
msg.obj = data;
handler.sendMessage(msg);
}
}
666
<?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:orientation="vertical">
<!--轮播图的RecyclerView布局-->
<android.support.v7.widget.RecyclerView
android:id="@+id/img_rcv"
android:layout_width="match_parent"
android:layout_height="300px">
</android.support.v7.widget.RecyclerView>
<!--倒计时布局-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="京东秒杀"
android:textSize="28px"
android:textColor="#FF0000"
android:textStyle="bold"/>
<TextView
android:id="@+id/tv_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30px"
android:text="秒杀剩余时间5s"
/>
</LinearLayout>
<!--秒杀的RecyclerView布局-->
<android.support.v7.widget.RecyclerView
android:id="@+id/my_rcv"
android:layout_width =“match_parent”
android:layout_height =“match_parent”
> </ android.support.v7.widget.RecyclerView> </ LinearLayout> ```
777
```0
<?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:orientation="vertical">
<!--轮播图的图片控件-->
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/item_sdv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop">
</com.facebook.drawee.view.SimpleDraweeView>
</LinearLayout>
<?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:orientation="vertical"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:padding="10px">
<!--秒杀图的图片控件-->
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/rcv_sdv"
android:layout_width="200px"
android:layout_height="200px"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
fresco:actualImageScaleType="centerCrop"
fresco:placeholderImageScaleType="centerCrop"
fresco:roundAsCircle="true"
fresco:placeholderImage =“@ drawable / ic_launcher_background”/> <! - 秒杀显示价格的控件 - > <TextView android:id =“@ + id / rcv_tv” android:layout_width =“wrap_content” android:layout_height =“wrap_content “ android:padding =”10px“ android:layout_marginLeft =”30px“ android:layout_marginTop =”20px“ android:textColor =”#FF0000“ android:text =”¥49.90“/> </ LinearLayout> ```