一、布局
第一个布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="fragment.bwie.com.monilianxi.MainActivity">
<ImageView
android:id="@+id/img"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/timg"
android:layout_centerHorizontal="true"
/>
<fragment.bwie.com.monilianxi.CountDownView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/cdv"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
第二个布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main2"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="fragment.bwie.com.monilianxi.Main2Activity">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
></android.support.v7.widget.RecyclerView>
</RelativeLayout>
第三个布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="fragment.bwie.com.monilianxi.Main3Activity">
<WebView
android:id="@+id/web"
android:layout_width="match_parent"
android:layout_height="match_parent"></WebView>
</RelativeLayout>
子条目布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image"
android:layout_width="150dp"
android:layout_height="150dp"
android:src="@mipmap/ic_launcher"
android:layout_margin="10dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:text="孤独如鲸之搁浅"
android:lines="1"
android:textSize="18sp"
/>
<TextView
android:id="@+id/xiangqing"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:text="孤独如鲸之搁浅"
android:lines="1"
android:textSize="15sp"
/>
</LinearLayout>
<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="价格:"/>
<TextView
android:id="@+id/price"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="999"
android:textColor="#ff1111"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
二、自定义控件的类
public class CountDownView extends View {
//圆轮颜色
private int mRingColor;
//圆轮宽度
private float mRingWidth;
//圆轮进度值文本大小
private int mRingProgessTextSize;
//宽度
private int mWidth;
//高度
private int mHeight;
private Paint mPaint;
//圆环的矩形区域
private RectF mRectF;
//
private int mProgessTextColor;
private int mCountdownTime;
private float mCurrentProgress;
private OnCountDownFinishListener mListener;
public CountDownView(Context context) {
this(context, null);
}
public CountDownView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CountDownView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CountDownView);
mRingColor = a.getColor(R.styleable.CountDownView_ringColor, context.getResources().getColor(R.color.colorAccent));
mRingWidth = a.getFloat(R.styleable.CountDownView_ringWidth, 40);
// mRingProgessTextSize = a.getDimensionPixelSize(R.styleable.CountDownView_progressTextSize, LayoutParams.WRAP_CONTENT);
mProgessTextColor = a.getColor(R.styleable.CountDownView_progressTextColor, context.getResources().getColor(R.color.colorAccent));
mCountdownTime = a.getInteger(R.styleable.CountDownView_countdownTime, 3);
a.recycle();
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setAntiAlias(true);
this.setWillNotDraw(false);
}
public void setCountdownTime(int mCountdownTime) {
this.mCountdownTime = mCountdownTime;
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
mWidth = getMeasuredWidth();
mHeight = getMeasuredHeight();
mRectF = new RectF(0 + mRingWidth / 2, 0 + mRingWidth / 2,
mWidth - mRingWidth / 2, mHeight - mRingWidth / 2);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
/**
*圆环
*/
//颜色
mPaint.setColor(mRingColor);
//空心
mPaint.setStyle(Paint.Style.STROKE);
//宽度
mPaint.setStrokeWidth(mRingWidth);
canvas.drawArc(mRectF, -90, mCurrentProgress - 360, false, mPaint);
// //绘制文本
// Paint textPaint = new Paint();
// textPaint.setAntiAlias(true);
// textPaint.setTextAlign(Paint.Align.CENTER);
// String text = mCountdownTime - (int) (mCurrentProgress / 360f * mCountdownTime) + "";
// textPaint.setTextSize(mRingProgessTextSize);
// textPaint.setColor(mProgessTextColor);
// //文字居中显示
// Paint.FontMetricsInt fontMetrics = textPaint.getFontMetricsInt();
// int baseline = (int) ((mRectF.bottom + mRectF.top - fontMetrics.bottom - fontMetrics.top) / 2);
// canvas.drawText(text, mRectF.centerX(), baseline, textPaint);
//
}
private ValueAnimator getValA(long countdownTime) {
ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 100);
valueAnimator.setDuration(countdownTime);
valueAnimator.setInterpolator(new LinearInterpolator());
valueAnimator.setRepeatCount(0);
return valueAnimator;
}
/**
* 开始倒计时
*/
public void startCountDown() {
setClickable(false);
ValueAnimator valueAnimator = getValA(mCountdownTime * 1000);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float i = Float.valueOf(String.valueOf(animation.getAnimatedValue()));
mCurrentProgress = (int) (360 * (i / 100f));
invalidate();
}
});
valueAnimator.start();
valueAnimator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
//倒计时结束回调
if (mListener != null) {
mListener.countDownFinished();
}
setClickable(true);
}
});
}
public void setAddCountDownListener(OnCountDownFinishListener mListener) {
this.mListener = mListener;
}
public interface OnCountDownFinishListener {
void countDownFinished();
}
}
三、适配器
public class Myadapter extends RecyclerView.Adapter<Myadapter.ViewHolder> implements View.OnClickListener{
Context context;
List<Newsbean.DataBean> list;
private OnItemClickListener mItemClickListener;
public interface OnItemClickListener{
void onItemClick(int position);
}
public Myadapter(Context context, List<Newsbean.DataBean> list) {
this.context = context;
this.list = list;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, null);
ViewHolder holder=new ViewHolder(view);
view.setOnClickListener(this);
return holder;
}
public void setItemClickListener(OnItemClickListener itemClickListener) {
mItemClickListener = itemClickListener;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.itemView.setTag(position);
holder.xiangqing.setText(list.get(position).getSubhead());
holder.title.setText(list.get(position).getTitle());
holder.price.setText(list.get(position).getPrice());
String pic1=list.get(position).getImages();
String[] split1=pic1.split("\\|");
Glide.with(context).load(split1[1]).into(holder.image);
}
@Override
public int getItemCount() {
return list.size();
}
//item点击事件
@Override
public void onClick(View v) {
if (mItemClickListener!=null){
mItemClickListener.onItemClick((Integer) v.getTag());
}
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView image;
TextView title,xiangqing,price;
public ViewHolder(View itemView) {
super(itemView);
image=itemView.findViewById(R.id.image);
title=itemView.findViewById(R.id.title);
xiangqing=itemView.findViewById(R.id.xiangqing);
price=itemView.findViewById(R.id.price);
}
}
}
四、activity
activity1
public class MainActivity extends AppCompatActivity {
private ImageView img;
CountDownView cdv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找控件
img= (ImageView) findViewById(R.id.img);
cdv= (CountDownView) findViewById(R.id.cdv);
//属性动画
ObjectAnimator moveIn = ObjectAnimator.ofFloat(img, "translationY", 200f, 300f);
ObjectAnimator animator = ObjectAnimator.ofFloat(img, "scaleY", 1f, 2f, 1f);
AnimatorSet animSet = new AnimatorSet();
animSet.play(animator).after(moveIn);
animSet.setDuration(3000);
animSet.start();
cdv.startCountDown();
cdv.setAddCountDownListener(new CountDownView.OnCountDownFinishListener() {
@Override
public void countDownFinished() {
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
}
});
}
}
activity2
public class Main2Activity extends AppCompatActivity implements NetCallBack<Newsbean>{
private RecyclerView rv;
private List<Newsbean.DataBean> list;
private Myadapter myadapter;
String sui;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
//全局捕获异常
// System.out.println(sui.equals("sdfa"));
rv= (RecyclerView) findViewById(R.id.rv);
list=new ArrayList<>();
myadapter = new Myadapter(this,list);
rv.setAdapter(myadapter);
OkHttpUtils okHttpUtils=new OkHttpUtils();
okHttpUtils.getLoadData("http://120.27.23.105/product/getProducts?pscid=2",this,Newsbean.class);
LinearLayoutManager layoutManager=new LinearLayoutManager(this);
rv.setLayoutManager(layoutManager);
myadapter.setItemClickListener(new Myadapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
Intent intent=new Intent(Main2Activity.this,Main3Activity.class);
startActivity(intent);
}
});
}
@Override
public void successNet(Newsbean newsbean) {
list.addAll(newsbean.getData());
myadapter.notifyDataSetChanged();
}
@Override
public void errorNet(String errorMsg, int errorCode) {
}
}
activity3
public class Main3Activity extends AppCompatActivity {
private WebView web;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
web= (WebView) findViewById(R.id.web);
web.loadUrl("https://item.m.jd.com/product/2542855.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends");
}
}
----crash
属性 attrs
<resources>
<declare-styleable name="CountDownView">
<!--颜色-->
<attr name="ringColor" format="color" />
<!-- 进度文本的字体大小 -->
<attr name="progressTextSize" format="dimension" />
<!-- 圆环宽度 -->
<attr name="ringWidth" format="float" />
<!--进度文本颜色-->
<attr name="progressTextColor" format="color"/>
<!--倒计时-->
<attr name="countdownTime" format="integer"/>
</declare-styleable>
</resources>