先添加依赖:
greendao
在最外层grandou添加
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.0'
再正常添加依赖
在最上面添加:
apply plugin: 'org.greenrobot.greendao'
dependencies {
implementation 'org.greenrobot:greendao:3.2.2' //流式布局依赖 implementation 'com.fynn.fluidlayout:fluidlayout:1.0'}
greendao路径:
greendao { //数据库的schema版本,也可以理解为数据库版本号 schemaVersion 2 //设置DaoMaster、DaoSession、Dao包名,也就是要放置这些类的包的全路径。 daoPackage 'chencheng.bwie.com.yuekaodeom' //设置DaoMaster、DaoSession、Dao目录 targetGenDir 'src/main/java' }
自定义搜索事件:
public class MyTileView extends RelativeLayout { private EditText editText; private Button but_sousuo; public MyTileView(Context context) { this(context,null); } public MyTileView(Context context, AttributeSet attrs) { this(context, attrs,0); } public MyTileView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context,attrs); } private void init(Context context,AttributeSet attrs){ View view=View.inflate(context, R.layout.shousuo,this); editText=view.findViewById(R.id.sousuo_ed); but_sousuo=view.findViewById(R.id.but_sousuo); but_sousuo.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { listened.toString(editText.getText().toString()); editText.setText(""); } }); } Listened listened; public void setListened(Listened listened){ this.listened=listened; } public interface Listened{ void toString (String editText); } }搜索的布局:
<?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="60dp" android:background="#fff"> <EditText android:id="@+id/sousuo_ed" android:layout_width="200dp" android:layout_height="40dp" android:layout_marginLeft="50dp" android:layout_marginTop="10dp" android:hint="请输入搜索内容" android:background="@drawable/etxt" /> <Button android:id="@+id/but_sousuo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/but" android:layout_margin="10dp" android:text="搜索" android:textColor="#ffffff"/> </LinearLayout>
搜索主界面:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private MyTileView mMytitle; private FluidLayout mFluidLayout; /** * 清除历史记录 */ private Button mButDel; BeanDao beanDao; List<Bean> stringList=new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); //关联数据库控件 DaoMaster.DevOpenHelper helper=new DaoMaster.DevOpenHelper(this,"sport-db",null); SQLiteDatabase db=helper.getWritableDatabase(); DaoMaster daoMaster=new DaoMaster(db); DaoSession daoSession=daoMaster.newSession(); beanDao=daoSession.getBeanDao(); } private void initView() { mMytitle = (MyTileView) findViewById(R.id.mytitle); mFluidLayout = (FluidLayout) findViewById(R.id.fluidLayout); mButDel = (Button) findViewById(R.id.but_del); mButDel.setOnClickListener(this); mMytitle.setListened(new MyTileView.Listened() { @Override public void toString(String editText) { if (!TextUtils.isEmpty(editText)){ //存入数据库 beanDao.insertOrReplaceInTx(new Bean(null,editText)); genTag(); startActivity(new Intent(MainActivity.this,ShopingAvtivity.class)); }else { Toast.makeText(MainActivity.this,"不能为空",Toast.LENGTH_LONG).show(); } } }); } //查询数据库 private void genTag(){ List<Bean> beans=beanDao.loadAll(); stringList.clear(); stringList.addAll(beans); mFluidLayout.removeAllViews(); for (int x=0;x<beans.size();x++){ TextView tv=new TextView(MainActivity.this); tv.setText(stringList.get(x).getName()); tv.setTextSize(13); final FluidLayout.LayoutParams params = new FluidLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); params.setMargins(12,12,12,12); mFluidLayout.addView(tv,params); } } @Override public void onClick(View v) { switch (v.getId()) { default: break; case R.id.but_del: //删除数据库 beanDao.deleteAll(); //查询数据库 genTag(); break; } } }
搜索主界面布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".view.MainActivity"> <chencheng.bwie.com.yuekaodeom.view.MyTileView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/mytitle"></chencheng.bwie.com.yuekaodeom.view.MyTileView> <com.fynn.fluidlayout.FluidLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/fluidLayout"></com.fynn.fluidlayout.FluidLayout> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/but_del" android:text="清除历史记录"/> </LinearLayout>
实现购物车界面:
主类:
public class ShopingAvtivity extends AppCompatActivity implements ICartsView { String uid = "4215"; MyAdapter adapter; MainPresenter presenter; private ExpandableListView mElv; private SwipeRefreshLayout mShopiing; private CheckBox mCheckbox; /** * 0 */ private TextView mTvPrice; /** * 结算(0) */ private TextView mTvNum; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_shoping_avtivity); initView(); presenter = new MainPresenter(this); presenter.carts(uid); EventBus.getDefault().register(this); mCheckbox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { adapter.changeAllListCbState(mCheckbox.isChecked()); } }); } @Override public void showCarts(List<CartsBean.DataBean> group, List<List<CartsBean.DataBean.ListBean>> child, final String uid) { adapter = new MyAdapter(ShopingAvtivity.this, group, child); mElv.setAdapter(adapter); mElv.setGroupIndicator(null); for (int i = 0; i < group.size(); i++) { mElv.expandGroup(i); } //删除接口 adapter.setOnCliks(new MyAdapter.OnCliks() { @Override public void onclikId(int pid) { presenter.delete(uid, pid + ""); } }); mShopiing.setColorSchemeColors(Color.GRAY); mShopiing.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { mShopiing.setRefreshing(false); presenter.carts(uid); } }); } @Override public void showDelete(DeleteBean deleteBean) { final String code = deleteBean.getCode(); int i = Integer.parseInt(code); if (i == 0) { adapter.notifyDataSetChanged(); Toast.makeText(ShopingAvtivity.this, deleteBean.getCode(), Toast.LENGTH_LONG).show(); } } @Subscribe public void onMessageEvent(MessageEvent event) { mCheckbox.setChecked(event.isChecked()); } @Subscribe public void onMessageEvent(PriceAndCountEvent event) { mTvNum.setText("结算(" + event.getCount() + ")"); mTvPrice.setText(event.getPrice() + ""); } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); } private void initView() { mElv = (ExpandableListView) findViewById(R.id.elv); mShopiing = (SwipeRefreshLayout) findViewById(R.id.shopiing); mCheckbox = (CheckBox) findViewById(R.id.checkbox); mTvPrice = (TextView) findViewById(R.id.tv_price); mTvNum = (TextView) findViewById(R.id.tv_num); } }购物车适配器:
public class MyAdapter extends BaseExpandableListAdapter { private Context context; private List<CartsBean.DataBean> groupList; private List<List<CartsBean.DataBean.ListBean>> childList; private final LayoutInflater inflater; OnClinks onClink; //接口回调 public interface OnClinks { void onclikId(int pid); } public void setOnClink(OnClinks onClink) { this.onClink = onClink; } public MyAdapter(Context context, List<CartsBean.DataBean> groupList, List<List<CartsBean.DataBean.ListBean>> childList) { this.context = context; this.groupList = groupList; this.childList = childList; inflater = LayoutInflater.from(context); } @Override public int getGroupCount() { return groupList.size(); } @Override public int getChildrenCount(int groupPosition) { return childList.get(groupPosition).size(); } @Override public Object getGroup(int groupPosition) { return groupList.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return childList.get(groupPosition).get(childPosition); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return true; } @Override public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { View view; final GroupViewHolder holder; if (convertView == null) { holder = new GroupViewHolder(); view = inflater.inflate(R.layout.group, null); holder.cbGroup = view.findViewById(R.id.cb_parent); holder.tv_number = view.findViewById(R.id.tv_number); view.setTag(holder); } else { view = convertView; holder = (GroupViewHolder) view.getTag(); } final CartsBean.DataBean dataBean = groupList.get(groupPosition); holder.cbGroup.setChecked(dataBean.isChecked()); holder.tv_number.setText(dataBean.getSellerName()); //一级checkbox holder.cbGroup.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dataBean.setChecked(holder.cbGroup.isChecked()); changeChildCbState(groupPosition, holder.cbGroup.isChecked()); EventBus.getDefault().post(compute()); changeAllCbState(isAllGroupCbSelected()); notifyDataSetChanged(); } }); return view; } @Override public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { View view; final ChildViewHolder holder; if (convertView == null) { holder = new ChildViewHolder(); view = inflater.inflate(R.layout.child, null); holder.cbChild = view.findViewById(R.id.cb_child); holder.tv_tel = view.findViewById(R.id.tv_tel); holder.tv_price = view.findViewById(R.id.tv_pri); holder.ll=view.findViewById(R.id.ll); holder.iv_add = view.findViewById(R.id.iv_add); holder.iv_del = view.findViewById(R.id.iv_del); holder.tv_num = view.findViewById(R.id.tv_num); holder.sim=view.findViewById(R.id.sim); view.setTag(holder); } else { view = convertView; holder = (ChildViewHolder) view.getTag(); } final CartsBean.DataBean.ListBean datasBean = childList.get(groupPosition).get(childPosition); holder.cbChild.setChecked(datasBean.isChecked()); holder.tv_tel.setText(datasBean.getTitle()); //设置图片显示 String uri = datasBean.getImages().split("\\|")[0]; DraweeController controller = Fresco.newDraweeControllerBuilder() .setUri(uri) .setAutoPlayAnimations(true) .build(); holder.sim.setController(controller); holder.tv_price.setText("¥:"+datasBean.getPrice() + ""); holder.tv_num.setText(datasBean.getNum() + ""); //二级checkbox holder.cbChild.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //设置该条目对象里的checked属性值 datasBean.setChecked(holder.cbChild.isChecked()); PriceAndCountEvent priceAndCountEvent = compute(); EventBus.getDefault().post(priceAndCountEvent); if (holder.cbChild.isChecked()) { //当前checkbox是选中状态 if (isAllChildCbSelected(groupPosition)) { changGroupCbState(groupPosition, true); changeAllCbState(isAllGroupCbSelected()); } } else { changGroupCbState(groupPosition, false); changeAllCbState(isAllGroupCbSelected()); } notifyDataSetChanged(); } }); //加号 holder.iv_add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int num = datasBean.getNum(); holder.tv_num.setText(++num + ""); datasBean.setNum(num); if (holder.cbChild.isChecked()) { PriceAndCountEvent priceAndCountEvent = compute(); EventBus.getDefault().post(priceAndCountEvent); } } }); //减号 holder.iv_del.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int num = datasBean.getNum(); if (num == 1) { Toast.makeText(context,"以是最小值了",Toast.LENGTH_SHORT).show(); return; } holder.tv_num.setText(--num + ""); datasBean.setNum(num); if (holder.cbChild.isChecked()) { PriceAndCountEvent priceAndCountEvent = compute(); EventBus.getDefault().post(priceAndCountEvent); } } }); holder.ll.setOnClickListener(new View.OnClickListener() { private AlertDialog dialog; @Override public void onClick(View v) { final List<CartsBean.DataBean.ListBean> listBeans = childList.get(childPosition); AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("提示"); builder.setMessage("确认删除?"); builder.setPositiveButton("确认", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { /*QueryBean.DataBean.ListBean remove = listBeans.remove(childPosition);*/ int pid = childList.get(groupPosition).get(childPosition).getPid(); Log.e("pid------", pid + ""); onClink.onclikId(pid); if (listBeans.size() == 0) { childList.remove(groupPosition); groupList.remove(groupPosition); } PriceAndCountEvent priceAndCountEvent = compute(); EventBus.getDefault().post(priceAndCountEvent); notifyDataSetChanged(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialog = builder.create(); dialog.show(); } }); notifyDataSetChanged(); return view; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } class GroupViewHolder { CheckBox cbGroup; TextView tv_number; } class ChildViewHolder { CheckBox cbChild; TextView tv_tel; LinearLayout ll; TextView tv_price; TextView iv_del; TextView iv_add; TextView tv_num; SimpleDraweeView sim; } class Other{ Button but; } /** * 改变全选的状态 * * @param flag */ private void changeAllCbState(boolean flag) { MessageEvent messageEvent = new MessageEvent(); messageEvent.setChecked(flag); EventBus.getDefault().post(messageEvent); notifyDataSetChanged(); } /** * 改变一级列表checkbox状态 * * @param groupPosition */ private void changGroupCbState(int groupPosition, boolean flag) { CartsBean.DataBean dataBean = groupList.get(groupPosition); dataBean.setChecked(flag); } /** * 改变二级列表checkbox状态 * * @param groupPosition * @param flag */ private void changeChildCbState(int groupPosition, boolean flag) { List<CartsBean.DataBean.ListBean> datasBeen = childList.get(groupPosition); for (int i = 0; i < datasBeen.size(); i++) { CartsBean.DataBean.ListBean datasBean = datasBeen.get(i); datasBean.setChecked(flag); } } /** * 判断一级列表是否全部选中 * * @return */ private boolean isAllGroupCbSelected() { for (int i = 0; i < groupList.size(); i++) { CartsBean.DataBean dataBean = groupList.get(i); if (!dataBean.isChecked()) { return false; } } return true; } /** * 判断二级列表是否全部选中 * * @param groupPosition * @return */ private boolean isAllChildCbSelected(int groupPosition) { List<CartsBean.DataBean.ListBean> datasBeen = childList.get(groupPosition); for (int i = 0; i < datasBeen.size(); i++) { CartsBean.DataBean.ListBean datasBean = datasBeen.get(i); if (!datasBean.isChecked()) { return false; } } return true; } /** * 计算列表中,选中的钱和数量 */ private PriceAndCountEvent compute() { int count = 0; int price = 0; for (int i = 0; i < childList.size(); i++) { List<CartsBean.DataBean.ListBean> datasBeen = childList.get(i); for (int j = 0; j < datasBeen.size(); j++) { CartsBean.DataBean.ListBean datasBean = datasBeen.get(j); if (datasBean.isChecked()) { price += datasBean.getNum() * datasBean.getPrice(); count += datasBean.getNum(); } } } PriceAndCountEvent priceAndCountEvent = new PriceAndCountEvent(); priceAndCountEvent.setCount(count); priceAndCountEvent.setPrice(price); return priceAndCountEvent; } /** * 设置全选、反选 * * @param flag */ public void changeAllListCbState(boolean flag) { for (int i = 0; i < groupList.size(); i++) { changGroupCbState(i, flag); changeChildCbState(i, flag); } EventBus.getDefault().post(compute()); notifyDataSetChanged(); }}EventBus的Bean类:
实在checkboxbean类:
public class MessageEvent { private boolean checked; public boolean isChecked() { return checked; } public void setChecked(boolean checked) { this.checked = checked; } }
计算总价和总数的bean类:
public class PriceAndCountEvent { private int price; private int count; public int getCount() { return count; } public void setCount(int count) { this.count = count; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } }
购物车页面布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".view.ShopingAvtivity"> <TextView android:layout_width="match_parent" android:layout_height="40dp" android:background="#990000ff" android:gravity="center" android:text="购物车" android:textSize="25sp" android:textColor="#ff3660"/> <android.support.v4.widget.SwipeRefreshLayout android:layout_weight="1" android:id="@+id/shopiing" android:layout_width="wrap_content" android:layout_height="wrap_content" > <ExpandableListView android:id="@+id/elv" android:layout_width="match_parent" android:layout_height="match_parent"></ExpandableListView> </android.support.v4.widget.SwipeRefreshLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentBottom="true" > <CheckBox android:id="@+id/checkbox" android:layout_width="30dp" android:layout_height="30dp" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:focusable="false"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@+id/checkbox" android:text="全选/反选" android:textSize="15sp"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="合计:"/> <TextView android:id="@+id/tv_price" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_marginLeft="10dp" android:gravity="center_vertical" android:paddingRight="10dp" android:text="0" android:textColor="#ffffff"/> <TextView android:id="@+id/tv_num" android:layout_width="wrap_content" android:layout_height="50dp" android:background="#ff33" android:gravity="center" android:padding="10dp" android:text="结算(0)" android:textColor="#ffffff"/> </LinearLayout> </RelativeLayout> </LinearLayout>父类group布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="100dp" android:gravity="center_vertical"> <CheckBox android:id="@+id/cb_parent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="30dp" android:layout_marginLeft="20dp" android:layout_marginTop="30dp" android:focusable="false"/> <TextView android:id="@+id/tv_sign" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:text="标记"/> <TextView android:id="@+id/tv_number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:text="asdadadasda"/> </LinearLayout>子类child布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ll" android:layout_width="match_parent" android:layout_height="250dp" android:orientation="horizontal" > <CheckBox android:id="@+id/cd_child" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="30dp" android:layout_marginLeft="40dp" android:layout_marginTop="40dp" android:layout_gravity="center_vertical" android:focusable="false"/> <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/sim" android:layout_width="100dp" android:layout_height="100dp" android:layout_gravity="center_vertical"></com.facebook.drawee.view.SimpleDraweeView> <LinearLayout android:layout_width="match_parent" android:layout_height="120dp" android:layout_gravity="center_vertical" android:layout_marginLeft="10dp" android:orientation="vertical"> <TextView android:id="@+id/tv_tel" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="asd"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:id="@+id/tv_pri" android:gravity="center_vertical" android:layout_width="200dp" android:layout_height="wrap_content" android:textColor="#ff33" android:text="¥:3000.00" /> <LinearLayout android:layout_weight="1" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical"> <TextView android:id="@+id/iv_del" android:layout_width="20dp" android:layout_height="20dp" android:text="-" android:gravity="center" android:background="@drawable/jiajian"/> <TextView android:id="@+id/tv_num" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:background="@drawable/beijing" android:paddingBottom="2dp" android:paddingLeft="20dp" android:paddingRight="20dp" android:paddingTop="2dp" android:text="1"/> <TextView android:id="@+id/iv_add" android:layout_width="20dp" android:layout_height="20dp" android:text="+" android:gravity="center" android:background="@drawable/jiajian"/> </LinearLayout> </LinearLayout> </LinearLayout> </LinearLayout>布局里背景:
搜索框背景:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="100dp"></corners> <solid android:color="#fff"></solid> <stroke android:color="#f0f2f5" android:width="1dp"></stroke> </shape>搜索按钮背景:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="50dp"></corners> <solid android:color="#ff3300"></solid> <size android:width="60dp" android:height="40dp"></size> </shape>购物车加减进行数字变换的背景:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#fff"></solid> <size android:height="20dp" android:width="30dp"></size> </shape>加减号圆形背景:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <stroke android:width="1dp" android:color="#ff33"></stroke> <size android:width="20dp" android:height="20dp"></size> </shape>