implementation 'com.fyales.android:library:1.0.2'
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
implementation 'jp.wasabeef:recyclerview-animators:2.3.0'
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
implementation 'com.google.code.gson:gson:2.2.4'
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'org.greenrobot:eventbus:3.1.1'
数据库
public class Sqliet extends SQLiteOpenHelper {
public Sqliet(Context context) {
super(context, "User.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table users(id integer primary key autoincrement," +
"name text)");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
添加数据库
public class UserDao {
private Sqliet sqliet;
private SQLiteDatabase database;
public UserDao(Context context){
sqliet = new Sqliet(context);
database = sqliet.getReadableDatabase();
}
public void add(String name){
ContentValues value = new ContentValues();
value.put("name",name);
database.insert("users",null,value);
}
public String select(){
database.beginTransaction();
StringBuffer buffer = new StringBuffer();
try {
Cursor query = database.query("users", null, null, null, null, null, null);
while (query.moveToNext()){
String name = query.getString(query.getColumnIndex("name"));
buffer.append(name);
}
database.setTransactionSuccessful();
}finally {
database.endTransaction();
}
return buffer.toString();
}
}
OKHttpUtils
public class OKHttpUtils {
public static OKHttpUtils instance;
public OkHttpClient okHttpClient;
public OKHttpUtils(){
if (null == instance){
synchronized (OkHttpClient.class){
if (null == instance){
okHttpClient = new OkHttpClient.Builder().addInterceptor(new MyInterceptor()).build();
}
}
}
}
public static OKHttpUtils getInstance() {
if (null == instance){
synchronized (OKHttpUtils.class){
if (null == instance){
instance = new OKHttpUtils();
}
}
}
return instance;
}
public void get(String urlString, Callback callback){
Request request = new Request.Builder().url(urlString).build();
okHttpClient.newCall(request).enqueue(callback);
}
public void post(String urlString, FormBody formBody,Callback callback){
Request request = new Request.Builder().method("POST",formBody).url(urlString).build();
okHttpClient.newCall(request).enqueue(callback);
}
class MyInterceptor implements Interceptor{
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Request newRequest = request.newBuilder().addHeader("source", "android").build();
Response response = chain.proceed(newRequest);
return response;
}
}
}
流式布局
public class MainActivity extends AppCompatActivity {
@BindView(R.id.container)
TagCloudLayout container;
@BindView(R.id.edit_sou)
EditText editSou;
@BindView(R.id.container1)
TagCloudLayout container1;
@BindView(R.id.add_but)
Button addBut;
private ArrayList mList;
private ArrayList lList;
private TagBaseAdapter mAdapter;
private TagBaseAdapter lAdapter;
private UserDao userDao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
mList = new ArrayList<>();
lList = new ArrayList<>();
userDao = new UserDao(this);
lList.add("中华人民共和国");
lList.add("大韩民国");
lList.add("日本");
lList.add("朝鲜");
lList.add("台湾");
lList.add("香港特别行政区");
lList.add("澳门特别行政区");
lList.add("越南");
lList.add("老挝");
lList.add("柬埔寨");
lList.add("泰国");
lList.add("缅甸");
lList.add("马来西亚");
lList.add("新加坡");
lList.add("印度尼西亚");
lList.add("文莱");
lList.add("菲律宾");
lAdapter = new TagBaseAdapter(this, lList);
mAdapter = new TagBaseAdapter(this, mList);
findViewById(R.id.add_but).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mList.add(editSou.getText().toString());
userDao.add(editSou.getText().toString());
mAdapter.notifyDataSetChanged();
}
});
String select = userDao.select();
mList.add(select);
container1.setAdapter(lAdapter);
container.setAdapter(mAdapter);
container.setItemClickListener(new TagCloudLayout.TagItemClickListener() {
@Override
public void itemClick(int position) {
startActivity(new Intent(MainActivity.this,ShoppingActivity.class));
}
});
}
}
流式布局xml
购物车页面
public class ShoppingActivity extends AppCompatActivity implements ICorter.IView, SubAdapter.OnItemCheckedListener, SubAdapter.OnItemClickListener {
@BindView(R.id.rv_container)
RecyclerView rvContainer;
@BindView(R.id.cb_all)
CheckBox cbAll;
@BindView(R.id.tv_price)
TextView tvPrice;
@BindView(R.id.tv_count)
TextView tvCount;
private ICorter.IPresenter presenter;
private List data;
private SubAdapter subAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shopping);
ButterKnife.bind(this);
presenter = new Presenter();
presenter.actachView(this);
presenter.requestInfo();
}
@Override
public void showData(final String responseMsg) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Gson gson = new Gson();
ItemBean itemBean = gson.fromJson(responseMsg, ItemBean.class);
data = itemBean.getData();
LinearLayoutManager manager = new LinearLayoutManager(ShoppingActivity.this, LinearLayoutManager.VERTICAL, false);
rvContainer.setLayoutManager(manager);
subAdapter = new SubAdapter(ShoppingActivity.this, (ArrayList) data);
rvContainer.setAdapter(subAdapter);
subAdapter.setOnItemCheckedListener(ShoppingActivity.this);
subAdapter.setOnItemClickListener(ShoppingActivity.this);
}
});
}
@Override
public void showDatas(String responseMsg) {
}
@OnClick({R.id.cb_all, R.id.tv_price, R.id.tv_count})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.cb_all:
if (cbAll.isChecked()){
for (int i = 0; i < data.size() ; i++) {
data.get(i).setChecked(true);
}
subAdapter.notifyDataSetChanged();
}else {
for (int i = 0; i < data.size() ; i++) {
data.get(i).setChecked(false);
}
subAdapter.notifyDataSetChanged();
}
break;
case R.id.tv_price:
break;
case R.id.tv_count:
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
presenter.detachView(this);
}
@Override
public void onItemChecked(int position, boolean checked) {
data.get(position).setChecked(checked);
boolean tag = true;
for (int i = 0; i < data.size() ; i++) {
if (data.get(i).getChecked() == false){
tag = false;
}
}
cbAll.setChecked(tag);
subAdapter.notifyDataSetChanged();
for (int i = 0; i < data.size() ; i++) {
if (data.get(i).getChecked()==false){
}else {
tvPrice.setText(data.get(position).getList().get(0).getPrice()+"");
}
}
}
@Override
public void onItemClick(int position) {
startActivity(new Intent(ShoppingActivity.this,OrderActivity.class));
}
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {
Toast.makeText(ShoppingActivity.this, "数量是:" + event.getNum() + "价格是" + event.getPrice(), Toast.LENGTH_SHORT).show();
}
}
购物车布局
购物车适配器
public class SubAdapter extends RecyclerView.Adapter {
private Context context;
private ArrayList list;
private int shnagpinnumber;
public SubAdapter(Context context, ArrayList list) {
this.context = context;
this.list = list;
}
@Override
public SubViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View rootView = LayoutInflater.from(context).inflate(R.layout.subadapter, parent, false);
SubViewHolder subViewHolder = new SubViewHolder(rootView);
return subViewHolder;
}
@Override
public void onBindViewHolder(final SubViewHolder holder, final int position) {
String images = list.get(position).getList().get(0).getImages();
if (images != null && images.contains("|")){
String[] split = images.split("\\|");
Picasso.get().load(split[0]).into(holder.iv_showIcon);
}else {
Picasso.get().load(images).into(holder.iv_showIcon);
}
holder.tv_showInfo.setText(list.get(position).getList().get(0).getTitle());
holder.cb_choose.setChecked(list.get(position).getChecked());
holder.text_price.setText(list.get(position).getList().get(0).getPrice()+"");
holder.tb_toogle.setOnNumCallBack(new ToggleButton.OnNumCallBack() {
@Override
public void onShowNum(int number) {
shnagpinnumber = number;
}
});
holder.cb_choose.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (isChecked){
MessageEvent messageEvent = new MessageEvent();
messageEvent.setNum(shnagpinnumber);
messageEvent.setPrice(list.get(holder.getLayoutPosition()).getList().get(holder.getLayoutPosition()).getPrice());
EventBus.getDefault().post(messageEvent);
}
}
});
}
@Override
public int getItemCount() {
return list.size();
}
class SubViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,View.OnLongClickListener{
private ImageView iv_showIcon;
private TextView tv_showInfo;
private CheckBox cb_choose;
private ToggleButton tb_toogle;
private TextView text_price;
public SubViewHolder(View itemView) {
super(itemView);
iv_showIcon = itemView.findViewById(R.id.iv_showIcon);
tv_showInfo = itemView.findViewById(R.id.tv_showInfo);
cb_choose = itemView.findViewById(R.id.cb_choose);
tb_toogle = itemView.findViewById(R.id.tb_toogle);
text_price = itemView.findViewById(R.id.text_price);
cb_choose.setOnClickListener(this);
itemView.setOnLongClickListener(this);
}
@Override
public void onClick(View view) {
int position = getLayoutPosition();
boolean checked = cb_choose.isChecked();
onItemCheckedListener.onItemChecked(position,checked);
}
@Override
public boolean onLongClick(View view) {
int position = getLayoutPosition();
onItemClickListener.onItemClick(position);
return false;
}
}
public interface OnItemClickListener{
void onItemClick(int position);
}
OnItemClickListener onItemClickListener;
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
public interface OnItemCheckedListener{
void onItemChecked(int position, boolean checked);
}
OnItemCheckedListener onItemCheckedListener;
public void setOnItemCheckedListener(OnItemCheckedListener onItemCheckedListener) {
this.onItemCheckedListener = onItemCheckedListener;
}
}
购物车适配器布局
自定义加减器
public class ToggleButton extends LinearLayout implements View.OnClickListener{
private TextView txt_delete;
private TextView et_number;
private TextView txt_add;
public ToggleButton(Context context) {
super(context);
}
public ToggleButton(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initView(context);
}
private void initView(Context context) {
LayoutInflater.from(context).inflate(R.layout.jiajian, this);
txt_delete = findViewById(R.id.txt_delete);
et_number = findViewById(R.id.et_number);
txt_add = findViewById(R.id.txt_add);
txt_delete.setOnClickListener(this);
txt_add.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String numberString = et_number.getText().toString();
int number = Integer.parseInt(numberString);
switch (v.getId()) {
case R.id.txt_delete:
if (number < 1) {
//todo
Toast.makeText(getContext(), "最小不能小于1", Toast.LENGTH_SHORT).show();
return;
}
number -= 1;
et_number.setText(String.valueOf(number));
//点击减号按钮时回传数字
onNumCallBack.onShowNum(number);
break;
case R.id.txt_add:
number += 1;
et_number.setText(String.valueOf(number));
//点击加号按钮时回传数字
onNumCallBack.onShowNum(number);
break;
}
}
public interface OnNumCallBack {
void onShowNum(int number);
}
public OnNumCallBack onNumCallBack;
public void setOnNumCallBack(OnNumCallBack onNumCallBack) {
this.onNumCallBack = onNumCallBack;
}
}
加减器布局
订单页面
public class OrderActivity extends AppCompatActivity {
@BindView(R.id.group)
RadioGroup group;
@BindView(R.id.pager)
ViewPager pager;
@BindView(R.id.but_pop)
Button butPop;
private List list;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
ButterKnife.bind(this);
context = this;
list = new ArrayList<>();
list.add(new FragmentOne());
list.add(new FragmentTwo());
list.add(new FragmentThree());
pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int i) {
return list.get(i);
}
@Override
public int getCount() {
return list.size();
}
});
pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
switch (i) {
case 0:
group.check(R.id.but1);
break;
case 1:
group.check(R.id.but2);
break;
case 2:
group.check(R.id.but3);
break;
default:
break;
}
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i) {
case R.id.but1:
pager.setCurrentItem(0);
break;
case R.id.but2:
pager.setCurrentItem(1);
break;
case R.id.but3:
pager.setCurrentItem(2);
break;
}
}
});
}
@OnClick(R.id.but_pop)
public void onViewClicked() {
View rootView = LayoutInflater.from(context).inflate(R.layout.popup, null, false);
TextView text_dzf = rootView.findViewById(R.id.text_dzf);
TextView text_yqx = rootView.findViewById(R.id.text_yqx);
TextView text_yzf = rootView.findViewById(R.id.text_yzf);
PopupWindow popupWindow = new PopupWindow(rootView, 100, 140, true);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
popupWindow.setTouchable(true);
popupWindow.showAsDropDown(butPop,0,10);
text_dzf.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
}
订单页面布局
订单页面适配器
public class DingDanSubAdapter extends RecyclerView.Adapter {
private Context context;
private ArrayList list;
public DingDanSubAdapter(Context context, ArrayList list) {
this.context = context;
this.list = list;
}
@Override
public DingDanViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View rootView = LayoutInflater.from(context).inflate(R.layout.dingdanadapter, parent, false);
DingDanViewHolder dingDanViewHolder = new DingDanViewHolder(rootView);
return dingDanViewHolder;
}
@Override
public void onBindViewHolder(DingDanViewHolder holder, int position) {
holder.text_title.setText(list.get(position).getTitle());
holder.text_price.setText(list.get(position).getPrice()+"");
holder.text_time.setText(list.get(position).getCreatetime());
if (list.get(position).getStatus() == 0){
holder.text_zt.setText("待支付");
holder.but_xy.setText("取消订单");
}else if (list.get(position).getStatus() == 1){
holder.text_zt.setText("已支付");
holder.but_xy.setText("查看订单");
}else if (list.get(position).getStatus() == 2){
holder.text_zt.setText("已取消");
holder.but_xy.setText("查看订单");
}
}
@Override
public int getItemCount() {
return list.size();
}
class DingDanViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private TextView text_title;
private TextView text_price;
private TextView text_time;
private TextView text_zt;
private Button but_xy;
public DingDanViewHolder(View itemView) {
super(itemView);
text_title = itemView.findViewById(R.id.text_title);
text_price = itemView.findViewById(R.id.text_price);
text_time = itemView.findViewById(R.id.text_time);
text_zt = itemView.findViewById(R.id.text_zt);
but_xy = itemView.findViewById(R.id.but_xy);
but_xy.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int position = getLayoutPosition();
String but = but_xy.getText().toString();
onClickLisenter.onClick(position,but);
}
}
public interface OnClickLisenter{
void onClick(int position, String but);
}
OnClickLisenter onClickLisenter;
public void setOnClickLisenter(OnClickLisenter onClickLisenter) {
this.onClickLisenter = onClickLisenter;
}
}
订单页面适配器布局
第一个Frament页面
public class FragmentOne extends Fragment implements ICorter.IView {
Unbinder unbinder;
@BindView(R.id.ding_rv)
RecyclerView dingRv;
private ICorter.IPresenter presenter;
private static final String DAIZIFU = "https://www.zhaoapi.cn/product/updateOrder?uid=71";
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragmentone, container, false);
unbinder = ButterKnife.bind(this, view);
presenter = new Presenter();
presenter.actachView(this);
presenter.requestInfos();
return view;
}
@Override
public void showData(String responseMsg) {
}
@Override
public void showDatas(final String responseMsg) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
final Gson gson = new Gson();
DingdanBean dingdanBean = gson.fromJson(responseMsg, DingdanBean.class);
final List data = dingdanBean.getData();
LinearLayoutManager manager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
dingRv.setLayoutManager(manager);
final DingDanSubAdapter dingDanSubAdapter = new DingDanSubAdapter(getActivity(), (ArrayList) data);
dingRv.setAdapter(dingDanSubAdapter);
dingDanSubAdapter.setOnClickLisenter(new DingDanSubAdapter.OnClickLisenter() {
@Override
public void onClick(int position, String but) {
if (data.get(position).getStatus()==0){
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("确认删除么?");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getActivity(), "订单已取消", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.show();
}else if (data.get(position).getStatus()==1){
Toast.makeText(getActivity(), "查看订单", Toast.LENGTH_SHORT).show();
}else if (data.get(position).getStatus()==2){
Toast.makeText(getActivity(), "查看订单", Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
@Override
public void onDestroy() {
super.onDestroy();
presenter.detachView(this);
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
}
布局
MessageEvent 数据
public class MessageEvent {
private int num;
private double price;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}