一、ViewPage:
1.xml
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_width="match_parent"
android:layout_height="48dp"
android:orientation="horizontal">
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="match_parent"
android:button="@null"
android:gravity="center"
android:background="@drawable/buttonclick"
android:text="首页"/>
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="match_parent"
android:button="@null"
android:gravity="center"
android:background="@drawable/buttonclick"
android:text="购物车"/>
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="match_parent"
android:button="@null"
android:gravity="center"
android:background="@drawable/buttonclick"
android:text="我的"/>
2.activity
public class MainActivity extends AppCompatActivity {
@BindView(R.id.pager)
ViewPager pager;
@BindView(R.id.radiogroup)
RadioGroup group;
@BindView(R.id.homepage)
RadioButton homepage;
@BindView(R.id.shopcart)
RadioButton shopcart;
@BindView(R.id.mine)
RadioButton mine;
private HomePageFragment homePageFragment;
private ShopcartFragment shopcartFragment;
private MineFragment mineFragment;
private ArrayList
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
homePageFragment = new HomePageFragment();
shopcartFragment = new ShopcartFragment();
mineFragment = new MineFragment();
list = new ArrayList<>();
list.add(homePageFragment);
list.add(shopcartFragment);
list.add(mineFragment);
pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int i) {
return list.get(i);
}
@Override
public int getCount() {
return list.size();
}
});
group.check(group.getChildAt(0).getId());
pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
group.check(group.getChildAt(i).getId());
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
pager.setCurrentItem(0);
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.homepage:
pager.setCurrentItem(0);
break;
case R.id.shopcart:
pager.setCurrentItem(1);
break;
case R.id.mine:
pager.setCurrentItem(2);
break;
}
}
});
}
}
二、首页:
1.xml
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.activity.MainActivity">
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_marginRight="16dp"
android:textSize="16sp"
android:text="搜索"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_marginTop="10dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
2.activity
public class Frag_home extends Fragment implements Frag_homeInterface {
private int page = 1;
private int count = 20;
@BindView(R.id.editquery)
EditText editText;
@BindView(R.id.query)
TextView query;
@BindView(R.id.xrecycler)
XRecyclerView recycler;
private MyAdapter_query myAdapter_query;
private Frag_homePresenter frag_homePresenter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_home, container, false);
ButterKnife.bind(this,view);
frag_homePresenter = new Frag_homePresenter();
frag_homePresenter.setView(this);
//查询点击事件
query.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = editText.getText().toString();
if(s==null || s.equals("")){
Toast.makeText(getActivity(), "搜索商品不能为空", Toast.LENGTH_SHORT).show();
}else{
frag_homePresenter.queryData(s,page,count);
}
}
});
return view;
}
//实现接口方法
@Override
public void onSuccess(Object o, int isWhat) {
if(isWhat==0){
QueryDataBean bean = (QueryDataBean) o;
Toast.makeText(getActivity(), ""+bean.getMessage(), Toast.LENGTH_SHORT).show();
List
recycler.setLayoutManager(new GridLayoutManager(getActivity(),2));
myAdapter_query = new MyAdapter_query(getActivity());
myAdapter_query.setData(list);
recycler.setAdapter(myAdapter_query);
recycler.setLoadingListener(new XRecyclerView.LoadingListener() {
@Override
public void onRefresh() {
String s = editText.getText().toString();
frag_homePresenter.queryData(s,page,count);
recycler.refreshComplete();
}
@Override
public void onLoadMore() {
}
});
}
}
@Override
public void onFail(String err) {
}
//懒加载
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
frag_homePresenter = new Frag_homePresenter();
frag_homePresenter.setView(this);
super.setUserVisibleHint(isVisibleToUser);
}
@Override
public void onDestroy() {
super.onDestroy();
frag_homePresenter.detachView();
}
}
3.adapter
public class MyAdapter_query extends RecyclerView.Adapter
private Context context;
private List
public MyAdapter_query(Context context) {
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = View.inflate(context, R.layout.listitem_query, null);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
viewHolder.image.setImageURI(Uri.parse(list.get(i).getMasterPic()));
viewHolder.name.setText(list.get(i).getCommodityName());
viewHolder.yishou.setText("已售"+list.get(i).getSaleNum());
viewHolder.price.setText("¥:"+list.get(i).getPrice()+".00");
}
@Override
public int getItemCount() {
return list.size();
}
public void setData(List
this.list = list;
}
public class ViewHolder extends RecyclerView.ViewHolder {
private final SimpleDraweeView image;
private final TextView name;
private final TextView yishou;
private final TextView price;
public ViewHolder(@NonNull View itemView) {
super(itemView);
image = itemView.findViewById(R.id.query_image);
name = itemView.findViewById(R.id.query_name);
yishou = itemView.findViewById(R.id.query_yishou);
price = itemView.findViewById(R.id.query_price);
}
}
}
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_height="match_parent">
app:cardCornerRadius="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_width="match_parent"
android:layout_height="150dp" />
android:id="@+id/query_name"
android:textSize="14sp"
android:text="name"
android:layout_marginLeft="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:id="@+id/query_price"
android:text="price"
android:layout_marginLeft="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_alignParentRight="true"
android:text="已售0件"
android:layout_marginRight="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
4.presenter
public class Frag_homePresenter extends BasePresenter
private final HttpUtils httpUtils;
public Frag_homePresenter(){
httpUtils = HttpUtils.getInstance();
}
public void queryData(String s, int page, int count) {
httpUtils.response.getQueryData(s,""+page,""+count)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer
@Override
public void accept(QueryDataBean queryDataBean) throws Exception {
getView().onSuccess(queryDataBean,0);
}
});
}
}
5.utils
public final RetrofitResponse response;
//构造方法私有化
private HttpUtils(){
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response response = chain.proceed(request);
Log.e("httputils",response.toString());
return response;
}
})
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://172.17.8.100/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.build();
response = retrofit.create(RetrofitResponse.class);
}
//单例模式
public static HttpUtils getInstance(){
return HttpUtilsGetInstance.httpUtils;
}
private static class HttpUtilsGetInstance{
private static HttpUtils httpUtils = new HttpUtils();
}
6.interface
public interface RetrofitResponse {
@GET("small/commodity/v1/findCommodityByKeyword")
Observable
@GET("ks/product/getCarts?uid=51")
Observable
}
public interface Frag_homeInterface
void onSuccess(T t,int isWhat);
void onFail(String err);
}
7.application
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
DiskCacheConfig config = DiskCacheConfig.newBuilder(this)
.setBaseDirectoryName("images")
.setBaseDirectoryPath(Environment.getExternalStorageDirectory())
.build();
Fresco.initialize(this);
}
}
三、购物车
1.xml
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.activity.MainActivity">
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="48dp">
android:layout_centerVertical="true"
android:text="全选"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:textColor="#E05858"
android:textSize="12sp"
android:layout_centerInParent="true"
android:text="总价:¥0.00"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:gravity="center"
android:layout_alignParentRight="true"
android:background="#E66565"
android:textColor="#fff"
android:text="去结算"
android:layout_width="100dp"
android:layout_height="match_parent" />
2.activity
public class Frag_shopcart extends Fragment implements Frag_shopcartInterface {
private Frag_shopcartPresenter presenter;
@BindView(R.id.exlistview)
ExpandableListView listview;
@BindView(R.id.checkall)
CheckBox checkAll;
@BindView(R.id.allprice)
TextView allPrice;
@BindView(R.id.jiesuan)
TextView jiesuan;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_shopcart, container, false);
ButterKnife.bind(this,view);
presenter = new Frag_shopcartPresenter();
presenter.setView(this);
presenter.getShopData();
return view;
}
//实现接口方法
@Override
public void onSuccess(Object o, int isWhat) {
if(isWhat==0){
ShopDataBean bean = (ShopDataBean) o;
List
list.remove(0);
MyAdapter_shopcart myAdapter_shopcart = new MyAdapter_shopcart(getActivity());
myAdapter_shopcart.setData(list);
myAdapter_shopcart.setView(checkAll,allPrice);
listview.setAdapter(myAdapter_shopcart);
}
}
@Override
public void onFail(String err) {
}
//懒加载
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
presenter = new Frag_shopcartPresenter();
presenter.setView(this);
presenter.getShopData();
super.setUserVisibleHint(isVisibleToUser);
}
@Override
public void onDestroy() {
super.onDestroy();
presenter.detachView();
}
}
3.adapter
public class MyAdapter_shopcart extends BaseExpandableListAdapter {
private Context context;
private List
private CheckBox checkAll;
private TextView allPrice;
private int goodsSum = 0;
public MyAdapter_shopcart(Context context) {
this.context = context;
}
public void setData(List
this.list = list;
}
@Override
public int getGroupCount() {
return list.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return list.get(groupPosition).getList().size();
}
@Override
public Object getGroup(int groupPosition) {
return null;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
convertView = View.inflate(context, R.layout.listitem_group,null);
CheckBox check = convertView.findViewById(R.id.checkGroup);
check.setChecked(list.get(groupPosition).isCheckGroup());
check.setText(list.get(groupPosition).getSellerName());
//商家点击事件
check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox checkBox = (CheckBox) v;
list.get(groupPosition).setCheckGroup(checkBox.isChecked());
//选中当前商家所有商品
checkAllChild(groupPosition,checkBox.isChecked());
//是否全选
boolean checkedAll = isCheckAll();
checkAll.setChecked(checkedAll);
notifyDataSetChanged();
}
});
return convertView;
}
private void checkAllChild(int groupPosition, boolean checked) {
for (int i = 0; i < list.get(groupPosition).getList().size(); i++) {
list.get(groupPosition).getList().get(i).setCheckChild(checked);
}
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
convertView = View.inflate(context,R.layout.listitem_child,null);
CheckBox check = convertView.findViewById(R.id.childCheck);
SimpleDraweeView image = convertView.findViewById(R.id.childImage);
TextView name = convertView.findViewById(R.id.childName);
TextView price = convertView.findViewById(R.id.childPrice);
TextView jian = convertView.findViewById(R.id.jian);
final TextView sum = convertView.findViewById(R.id.sum);
TextView jia = convertView.findViewById(R.id.jia);
check.setChecked(list.get(groupPosition).getList().get(childPosition).isCheckChild());
image.setImageURI(Uri.parse(list.get(groupPosition).getList().get(childPosition).getImages()));
name.setText(list.get(groupPosition).getList().get(childPosition).getTitle());
price.setText("¥:"+list.get(groupPosition).getList().get(childPosition).getPrice()+".00");
sum.setText(""+list.get(groupPosition).getList().get(childPosition).getNum());
//商品点击事件
check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox checkBox = (CheckBox) v;
list.get(groupPosition).getList().get(childPosition).setCheckChild(checkBox.isChecked());
int goodsPrice = list.get(groupPosition).getList().get(childPosition).getPrice();
int goodsCount = list.get(groupPosition).getList().get(childPosition).getNum();
//是否选中商家
boolean checkGroup = isCheckGroup(groupPosition);
list.get(groupPosition).setCheckGroup(checkGroup);
//是否全选
boolean checkedAll = isCheckAll();
checkAll.setChecked(checkedAll);
//计算总价
if(checkBox.isChecked()){
goodsSum+=goodsPrice*goodsCount;
allPrice.setText("总价:¥"+goodsSum);
notifyDataSetChanged();
}else{
goodsSum-=goodsPrice*goodsCount;
allPrice.setText("总价:¥"+goodsSum);
notifyDataSetChanged();
}
notifyDataSetChanged();
}
});
//减少
jian.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("adapter","减少");
int goodsNum = list.get(groupPosition).getList().get(childPosition).getNum();
if(goodsNum>1){
goodsNum--;
sum.setText(""+goodsNum);
list.get(groupPosition).getList().get(childPosition).setNum(goodsNum);
boolean checkChild = list.get(groupPosition).getList().get(childPosition).isCheckChild();
if(checkChild){
int price = list.get(groupPosition).getList().get(childPosition).getPrice();
goodsSum-=price;
allPrice.setText("总价:¥"+goodsSum);
}
notifyDataSetChanged();
}
}
});
//增加
jia.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("adapter","增加");
int goodsNum = list.get(groupPosition).getList().get(childPosition).getNum();
goodsNum++;
list.get(groupPosition).getList().get(childPosition).setNum(goodsNum);
sum.setText(""+goodsNum);
/*goodsSum=0;
for (int i = 0; i < list.size(); i++) {
for (int j = 0; j < list.get(i).getList().size(); j++) {
boolean checkChild = list.get(i).getList().get(j).isCheckChild();
if(checkChild){
int price = list.get(i).getList().get(j).getPrice();
int num = list.get(i).getList().get(j).getNum();
goodsSum+=price*goodsNum;
allPrice.setText("总价:¥"+goodsSum);
notifyDataSetChanged();
}
}
}*/
boolean checkChild = list.get(groupPosition).getList().get(childPosition).isCheckChild();
if (checkChild){
int price = list.get(groupPosition).getList().get(childPosition).getPrice();
goodsSum+=price;
allPrice.setText("总价:¥"+goodsSum);
}
notifyDataSetChanged();
}
});
return convertView;
}
private boolean isCheckAll() {
for (int i = 0; i < list.size(); i++) {
if (!list.get(i).isCheckGroup()) {
return false;
}
}
return true;
}
//是否选中商家
private boolean isCheckGroup(int groupPosition) {
for (int i = 0; i < list.get(groupPosition).getList().size(); i++) {
if (!list.get(groupPosition).getList().get(i).isCheckChild()){
return false;
}
}
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
public void setView(CheckBox checkAll, final TextView allPrice) {
this.checkAll = checkAll;
this.allPrice = allPrice;
//全选
checkAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox checkBox = (CheckBox) v;
goodsSum=0;
if(checkBox.isChecked()){
for (int i = 0; i < list.size(); i++) {
list.get(i).setCheckGroup(checkBox.isChecked());
for (int j = 0; j < list.get(i).getList().size(); j++) {
list.get(i).getList().get(j).setCheckChild(checkBox.isChecked());
int price = list.get(i).getList().get(j).getPrice();
int num = list.get(i).getList().get(j).getNum();
goodsSum+=price*num;
}
}
allPrice.setText("总价:¥"+goodsSum);
}else{
for (int i = 0; i < list.size(); i++) {
list.get(i).setCheckGroup(checkBox.isChecked());
for (int j = 0; j < list.get(i).getList().size(); j++) {
list.get(i).getList().get(j).setCheckChild(checkBox.isChecked());
int price = list.get(i).getList().get(j).getPrice();
int num = list.get(i).getList().get(j).getNum();
goodsSum+=price*num;
}
}
goodsSum=0;
allPrice.setText("总价:¥"+goodsSum);
}
notifyDataSetChanged();
}
});
}
}
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_height="match_parent">
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="48dp">
android:focusable="false"
android:clickable="true"
android:text="商家1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_height="match_parent">
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_gravity="center_vertical"
android:id="@+id/childCheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_width="80dp"
android:layout_height="80dp" />
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:id="@+id/childName"
android:text="name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:textColor="#E06363"
android:textSize="12sp"
android:text="price"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_marginLeft="150dp"
android:textSize="28sp"
android:text="-"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_marginLeft="10dp"
android:textSize="16sp"
android:text="sum"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_marginLeft="10dp"
android:textSize="28sp"
android:text="+"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
4.presenter
public class Frag_shopcartPresenter extends BasePresenter
private final HttpUtils httpUtils;
public Frag_shopcartPresenter(){
httpUtils = HttpUtils.getInstance();
}
public void getShopData() {
httpUtils.response.getShopData()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer
@Override
public void accept(ShopDataBean shopDataBean) throws Exception {
getView().onSuccess(shopDataBean,0);
}
});
}
}
5.
public interface Frag_shopcartInterface
void onSuccess(T t, int isWhat);
void onFail(String err);
}