首先是依赖
implementation 'jp.wasabeef:recyclerview-animators:2.3.0'
implementation 'com.facebook.fresco:fresco:1.9.0'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'com.youth.banner:banner:1.4.10'
implementation 'com.github.bumptech.glide:glide:4.4.0'
接口
public interface IMainView {
void showSkin(List<SkinBean.DataBean.SubjectsBean> subjects);
void showError(String error);
}
public interface IModel {
void getSkin(String uri);
}
隶属于model下的注解请求
public interface MyService {
@GET("umIPmfS6c83237d9c70c7c9510c9b0f97171a308d13b611")
Call<SkinBean> getSkin(@Query("uri") String uri);
}
public interface IPresenter {
void showSkinToView(IModel iModel, IMainView iMainView);
void getData(List<SkinBean.DataBean.SubjectsBean> subjects);
void getError(String error);
}
Model层的请求数据
public class ModelFusion implements IModel {
private final IPresenter iPresenter;
public ModelFusion(IPresenter iPresenter) {
this.iPresenter = iPresenter;
}
@Override
public void getSkin(String uri) {
RetrofitUtil retrofitUtil = RetrofitUtil.getInstance();
MyService myService = retrofitUtil.createRequest(MyService.class);
Call<SkinBean> skin = myService.getSkin(uri);
skin.enqueue(new Callback<SkinBean>() {
@Override
public void onResponse(Call<SkinBean> call, Response<SkinBean> response) {
if (response.isSuccessful()){
SkinBean body = response.body();
List<SkinBean.DataBean.SubjectsBean> subjects = body.getData().getSubjects();
iPresenter.getData(subjects);
}
}
@Override
public void onFailure(Call<SkinBean> call, Throwable t) {
}
});
}
}
P层的逻辑
public class PresenterFusion implements IPresenter {
private IMainView iMainView;
private String uri = "homepage";
@Override
public void showSkinToView(IModel iModel, IMainView iMainView) {
this.iMainView = iMainView;
iModel.getSkin(uri);
}
@Override
public void getData(List<SkinBean.DataBean.SubjectsBean> subjects) {
iMainView.showSkin(subjects);
}
@Override
public void getError(String error) {
iMainView.showError(error);
}
}
View层的视图展示
public class MainActivity extends AppCompatActivity implements IMainView {
private Banner main_banner;
private RecyclerView main_miao;
private PresenterFusion presenterFusion;
private MyAdapter myAdapter;
private TextView tvHour;
private TextView tvMinute;
private TextView tvSecond;
static long mHour = 02;
static long mMin = 15;
boolean isRun = true;
static long mSecond = 36;
private Handler timeHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 1) {
computeTime();
if (mHour < 10) {
tvHour.setText("0" + mHour + "");
} else {
tvHour.setText(mHour + "");
}
if (mMin < 10) {
tvMinute.setText("0" + mMin + "");
} else {
tvMinute.setText(mMin + "");
}
if (mSecond < 10) {
tvSecond.setText("0" + mSecond + "");
} else {
tvSecond.setText(mSecond + "");
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
startRun();
initDatas();
initBanner();
}
private void initViews() {
main_banner = findViewById(R.id.main_banner);
main_miao = findViewById(R.id.main_miao);
tvHour = findViewById(R.id.tv_hour);
tvMinute = findViewById(R.id.tv_minute);
tvSecond = findViewById(R.id.tv_second);
}
public void initDatas(){
presenterFusion = new PresenterFusion();
presenterFusion.showSkinToView(new ModelFusion(presenterFusion),this);
}
private void initBanner() {
main_banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
main_banner.setImageLoader(new GlideImageLoader());
main_banner.isAutoPlay(true);
main_banner.setDelayTime(3000);
main_banner.setIndicatorGravity(BannerConfig.CENTER);
}
@Override
public void showSkin(List<SkinBean.DataBean.SubjectsBean> subjects) {
List<String> imgs = new ArrayList<>();
for (int i = 0; i < subjects.size(); i++) {
String image = subjects.get(i).getImage();
imgs.add(image);
}
main_banner.setImages(imgs);
main_banner.start();
main_miao.setLayoutManager(new LinearLayoutManager(MainActivity.this,LinearLayoutManager.VERTICAL,false));
myAdapter = new MyAdapter(MainActivity.this,subjects);
main_miao.setAdapter(myAdapter);
}
@Override
public void showError(String error) {
}
private void startRun() {
new Thread(new Runnable() {
@Override
public void run() {
while (isRun) {
try {
Thread.sleep(1000);
Message message = Message.obtain();
message.what = 1;
timeHandler.sendMessage(message);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}).start();
}
private static void computeTime() {
mSecond--;
if (mSecond < 0) {
mMin--;
mSecond = 59;
if (mMin < 0) {
mMin = 59;
mHour--;
}
}
}
}
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="5dp"/>
<size android:width="10dp" android:height="10dp"/>
<solid android:color="#000"/>
</shape>
列表的适配器
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private List<SkinBean.DataBean.SubjectsBean> data;
private int ONE = 0;
private int TWO = 1;
public MyAdapter(Context context, List<SkinBean.DataBean.SubjectsBean> data) {
this.context = context;
this.data = data;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (viewType == ONE) {
View oview = LayoutInflater.from(context).inflate(R.layout.one_item, parent, false);
MyHolderone myHolderone = new MyHolderone(oview);
return myHolderone;
} else if (viewType == TWO) {
View tview = LayoutInflater.from(context).inflate(R.layout.two_item, parent, false);
MyHoldertwo myHoldertwo = new MyHoldertwo(tview);
return myHoldertwo;
}
return null;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof MyHolderone) {
((MyHolderone) holder).one_text.setText(data.get(position).getDetail());
Uri uri = Uri.parse(data.get(position).getDescImage());
((MyHolderone) holder).one_img.setImageURI(uri);
} else if (holder instanceof MyHoldertwo) {
((MyHoldertwo) holder).two_text.setText(data.get(position).getDetail());
Uri uri = Uri.parse(data.get(position).getDescImage());
((MyHoldertwo) holder).two_img.setImageURI(uri);
}
}
@Override
public int getItemViewType(int position) {
if (position % 2 == 0) {
return ONE;
} else {
return TWO;
}
}
@Override
public int getItemCount() {
return data.size();
}
}
布局
//一个有倒计时的主布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".view.MainActivity">
<com.youth.banner.Banner
android:id="@+id/main_banner"
android:layout_width="match_parent"
android:layout_height="300dp">
</com.youth.banner.Banner>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="16dp">
<TextView
android:textColor="#FF3848"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="17sp"
android:text="京东秒杀"/>
<TextView
android:textColor="#222222"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="14sp"
android:text=" 12点场 "/>
<TextView
android:textStyle="bold"
android:id="@+id/tv_hour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:background="@drawable/daojishi_shape"
android:textColor="@android:color/white"
android:textSize="11sp"
android:text="02"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textStyle="bold"
android:textColor="@android:color/black"
android:text=":"/>
<TextView
android:textStyle="bold"
android:id="@+id/tv_minute"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:background="@drawable/daojishi_shape"
android:textColor="@android:color/white"
android:textSize="11sp"
android:text="15"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textStyle="bold"
android:textColor="@android:color/black"
android:text=":"/>
<TextView
android:textStyle="bold"
android:id="@+id/tv_second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:background="@drawable/daojishi_shape"
android:textColor="@android:color/white"
android:textSize="11sp"
android:text="36"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/main_miao"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</LinearLayout>