SurfaceView——繪製背景
效果展示
SurfaceView繪製白色橢圓背景
SurfaceView繪製粉色橢圓背景
SurfaceView繪製加號
SurfaceView繪製對號
SurfaceView繪製刷新符號
白色橢圓背景
public class WhiteView extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder surfaceHolder;
private String text;
public void setText(String text) {
this.text = text;
}
public WhiteView(Context context) {
super(context);
}
public WhiteView(Context context, AttributeSet attrs) {
super(context, attrs);
surfaceHolder = getHolder();
surfaceHolder.addCallback(this);
}
public WhiteView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public WhiteView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
int width = getWidth();//寬度
int height = getHeight();//高度
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
Canvas canvas = surfaceHolder.lockCanvas();
canvas.drawColor(getResources().getColor(R.color.colorBlue));
canvas.drawOval(0, 0, height, height, paint);
canvas.drawRect(height / 2, 0, width - height / 2, height, paint);
canvas.drawOval(width - height, 0, width, height, paint);
//TODO:繪製加號
paint.setStrokeWidth(10);
paint.setColor(getResources().getColor(R.color.colorBlue));
canvas.drawLine(width-height/2-30,height/2,width-height/2+30,height/2,paint);
canvas.drawLine(width-height/2,height/2-30,width-height/2,height/2+30,paint);
//TODO:繪製文字
paint.setTextSize(50);
canvas.drawText(text,height/2,height/2+20,paint);
surfaceHolder.unlockCanvasAndPost(canvas);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.example.imitationmonthexam_month12_b.view.WhiteView
android:id="@+id/white_view"
android:layout_width="200dp"
android:layout_height="70dp">
</com.example.imitationmonthexam_month12_b.view.WhiteView>
</LinearLayout>
粉色橢圓背景
public class PinkView extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder surfaceHolder;
private String text;
public void setText(String text) {
this.text = text;
}
public PinkView(Context context) {
super(context);
}
public PinkView(Context context, AttributeSet attrs) {
super(context, attrs);
surfaceHolder = getHolder();
surfaceHolder.addCallback(this);
}
public PinkView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public PinkView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
int width = getWidth();//寬度
int height = getHeight();//高度
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(getResources().getColor(R.color.colorPink));
Canvas canvas = surfaceHolder.lockCanvas();
canvas.drawColor(getResources().getColor(R.color.colorBlue));
canvas.drawOval(0, 0, height, height, paint);
canvas.drawRect(height / 2, 0, width - height / 2, height, paint);
canvas.drawOval(width - height, 0, width, height, paint);
//TODO:繪製對勾
paint.setStrokeWidth(10);
paint.setColor(Color.WHITE);
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.ok),width-height/2,height/2-50,paint);
//TODO:文字
paint.setTextSize(50);
canvas.drawText(text,height/2,height/2+20,paint);
surfaceHolder.unlockCanvasAndPost(canvas);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.example.imitationmonthexam_month12_b.view.PinkView
android:id="@+id/pink_view"
android:layout_width="200dp"
android:layout_height="70dp">
</com.example.imitationmonthexam_month12_b.view.PinkView>
</LinearLayout>
將每條數據的標題存儲
點擊加號添加一個標題
點擊標題跳轉到對應條目
看過一次的標題顯示粉色
看過一次的標題顯示對號
點擊刷新按鈕所有條目顯示白色和加號
多佈局適配
public class MyAdapter extends BaseMultiItemQuickAdapter<ItemEntity.DataBean, BaseViewHolder> {
/**
* Same as QuickAdapter#QuickAdapter(Context,int) but with
* some initialization data.
*
* @param data A new list is created out of this one to avoid mutable list
*/
public MyAdapter(List<ItemEntity.DataBean> data) {
super(data);
addItemType(0, R.layout.white_item);
addItemType(1, R.layout.pink_item);
}
@Override
protected void convert(BaseViewHolder helper, ItemEntity.DataBean item) {
switch (helper.getItemViewType()){
case 0:
WhiteView whiteView = helper.getView(R.id.white_view);
whiteView.setText(item.getTitle());
break;
case 1:
PinkView pinkView = helper.getView(R.id.pink_view);
pinkView.setText(item.getTitle());
break;
}
}
}
實體類
實現MultiItemEntity重寫方法
@Override
public int getItemType() {
return type;
}
Activity代碼
public class MainActivity extends AppCompatActivity {
private RecyclerView rv;
private List<ItemEntity.DataBean> list = new ArrayList<>();
private MyAdapter myAdapter;
private List<ItemEntity.DataBean> data;//已點擊集合
private int position = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initData() {
OkGo.<String>get("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1d")
.execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
String json = response.body();
data = new Gson().fromJson(json, ItemEntity.class).getData();
list.add(data.get(position++));
myAdapter.notifyDataSetChanged();
}
});
}
private void initView() {
rv = (RecyclerView) findViewById(R.id.rv);
myAdapter = new MyAdapter(list);
rv.setAdapter(myAdapter);
rv.setLayoutManager(new LinearLayoutManager(this));
//TODO:點擊添加一個
myAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
list.add(data.get(++position));
myAdapter.notifyDataSetChanged();
}
});
//TODO:長按看過
myAdapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
list.get(position).setType(1);
myAdapter.notifyItemChanged(position);
return true;
}
});
}
}