Gou

1.Fragment

public class MainActivity extends AppCompatActivity {

    private ViewPager pager;
    private RadioGroup radioGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pager = findViewById(R.id.pager);
        radioGroup = findViewById(R.id.radioGroup);
        weixin();
    }

    private void weixin() {
        ArrayList<Fragment> data = new ArrayList<>();
        data.add(new Frag1());
        data.add(new Frag2());
        //适配器
        FragAdpter fragAdpter = new FragAdpter(getSupportFragmentManager(),data);
        pager.setAdapter(fragAdpter);

        radioGroup.check(radioGroup.getChildAt(0).getId());
        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                radioGroup.check(radioGroup.getChildAt(i).getId());
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.radioButton1:
                        pager.setCurrentItem(0);
                        break;
                    case R.id.radioButton2:
                        pager.setCurrentItem(1);
                        break;
                }
            }
        });
    }
}

2.Fragment,适配器


    public class FragAdpter extends FragmentPagerAdapter {

    public ArrayList<Fragment> data;

    public FragAdpter(FragmentManager fm, ArrayList<Fragment> data) {
        super(fm);
        this.data = data;
    }

    @Override
    public Fragment getItem(int i) {
        return data.get(i);
    }

    @Override
    public int getCount() {
        return data.size();
    }
}

3.Fragment碎片1

public class Frag1 extends Fragment implements ContentInterface.VInterface {

    public List<JsonBean.DataBean> list = new ArrayList<>();
    public int uid = 51;
    public int pager = 0;
    public XRecyclerView rec_id;
    public ContentInterface.PInterface pInterface;
    public Frag1Adapter frag1Adapter;
    public CheckBox frag_CheckBox;
    public TextView frag_zong;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frag1,container,false);

        rec_id = view.findViewById(R.id.rec_id);
        frag_CheckBox = view.findViewById(R.id.frag_CheckBox);
        frag_zong = view.findViewById(R.id.frag_zong);
        pInterface = new MyPenster(this);
        init();


        return view;
    }

    private void init() {

        //实例化布局管理器
        LinearLayoutManager linear = new LinearLayoutManager(getActivity());
        linear.setOrientation(LinearLayoutManager.VERTICAL);
        rec_id.setLayoutManager(linear);

        pInterface.onZhan(uid);

        //适配器
        frag1Adapter = new Frag1Adapter(list,getActivity(),this);
        rec_id.setAdapter(frag1Adapter);

        //上拉下拉,刷新
        rec_id.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                list.clear();
                pager=1;
                pInterface.onZhan(uid);
                rec_id.refreshComplete();
            }

            @Override
            public void onLoadMore() {
                pager++;
                pInterface.onZhan(uid);
                rec_id.loadMoreComplete();
            }
        });

        //全选反选
        frag_CheckBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (frag_CheckBox.isChecked()){
                    setChecAll(true);
                    total();
                }else{
                    setChecAll(false);
                    frag_zong.setText(0+"");
                }
                frag1Adapter.notifyDataSetChanged();
            }
        });
    }
    //全选反选
    public void setChecAll(boolean b){
        for (int i = 0; i < list.size(); i++) {
            list.get(i).isSelect = b;
            for (int j = 0; j < list.get(i).getList().size(); j++) {
                list.get(i).getList().get(j).itemSelect = b;
            }
        }
        frag1Adapter.notifyDataSetChanged();
    }

    //计算总价
    public void total(){
        int num_pice = 0;
        for (int i = 0; i < list.size(); i++) {
            for (int j = 0; j < list.get(i).getList().size(); j++) {
                 if (list.get(i).getList().get(j).itemSelect){
                     int pice = (int)list.get(i).getList().get(j).getPrice();
                     int selected = list.get(i).getList().get(j).getNum();
                     int to = pice*selected;
                     num_pice += to;
                 }
            }
        }
        frag_zong.setText(num_pice+"");
    }

    //商家选中或者不选中
    /*
    i,商家的下标
    tab,孩子的下标,-1默认值
    b,是否选中状态
     */
    public void setSChecAll(int i,int tab,boolean b){
        list.get(i).isSelect = b;

         if (tab != -1){
             list.get(i).getList().get(tab).itemSelect = false;
         }else{
             for (int j = 0; j < list.get(i).getList().size(); j++) {
                 list.get(i).getList().get(j).itemSelect = b;
             }
         }
         total();
         frag1Adapter.notifyDataSetChanged();
    }

    //商家孩子下的选中
    public int setSHChecAll(boolean b,Object object){
        int n = 0;
        JsonBean.DataBean.ListBean listBean = (JsonBean.DataBean.ListBean)object;
        for (int i = 0; i < list.size(); i++) {
            for (int j = 0; j < list.get(i).getList().size(); j++) {
                JsonBean.DataBean.ListBean listBean1 = list.get(i).getList().get(j);
                if (listBean1.equals(listBean)){
                    listBean1.itemSelect = b;
                    n=i;
                }
            }
        }
        total();
        frag1Adapter.notifyDataSetChanged();
        return n;
    }

    //通过商家孩子下的下标状态,来获得商家的的下标
    public int getSHChecAll2(boolean b,Object object){
        int n = 0;
        JsonBean.DataBean.ListBean listBean = (JsonBean.DataBean.ListBean)object;
        for (int i = 0; i < list.size(); i++) {
            for (int j = 0; j < list.get(i).getList().size(); j++) {
                JsonBean.DataBean.ListBean listBean1 = list.get(i).getList().get(j);
                if (listBean1.equals(listBean)){
                    listBean1.itemSelect = b;
                    n=i;
                }
            }
        }
        return n;
    }



    @Override
    public void showShu(Object object) {
        JsonBean jsonBean = (JsonBean)object;
        List<JsonBean.DataBean> data = jsonBean.getData();
        data.remove(0);
        list.addAll(data);
        frag1Adapter.notifyDataSetChanged();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        pInterface.onDsply();
        pInterface = null;
    }
}

4.Fragment碎片1,适配器1

public class Frag1Adapter extends XRecyclerView.Adapter<Frag1Adapter.Holder> {

    public List<JsonBean.DataBean> list;
    public Context mContext;
    public Frag1 frag1;

    public Frag1Adapter(List<JsonBean.DataBean> list, Context mContext, Frag1 frag1) {
        this.list = list;
        this.mContext = mContext;
        this.frag1 = frag1;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.frag1_item, null);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final Holder holder, final int i) {
        boolean select = list.get(i).isSelect();
        holder.main_item_checkBox.setChecked(select);

        String sellerName = list.get(i).getSellerName();
        holder.main_item_shang.setText(sellerName);

        //布局管理器
        LinearLayoutManager linear = new LinearLayoutManager(mContext);
        linear.setOrientation(LinearLayoutManager.VERTICAL);
        holder.main_item_rec.setLayoutManager(linear);

        //适配器
        Frag1_2Adapter frag1_2Adapter = new Frag1_2Adapter(list.get(i).getList(),mContext,frag1);
        holder.main_item_rec.setAdapter(frag1_2Adapter);

        //商家的全选反选,价格变化
        holder.main_item_checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               frag1.setSChecAll(i,-1,holder.main_item_checkBox.isChecked());
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class Holder extends XRecyclerView.ViewHolder {

        public final CheckBox main_item_checkBox;
        public final RecyclerView main_item_rec;
        public final TextView main_item_shang;

        public Holder(@NonNull View itemView) {
            super(itemView);
            main_item_checkBox = itemView.findViewById(R.id.main_item_CheckBox);
            main_item_rec = itemView.findViewById(R.id.main_item_rec);
            main_item_shang = itemView.findViewById(R.id.main_item_shang);
        }
    }
}

5.Fragment碎片1,适配器2

public class Frag1_2Adapter extends XRecyclerView.Adapter<Frag1_2Adapter.Holder> {

    public int sum=0;
    public List<JsonBean.DataBean.ListBean> list;
    public Context mContext;
    public Frag1 frag1;

    public Frag1_2Adapter(List<JsonBean.DataBean.ListBean> list, Context mContext, Frag1 frag1) {
        this.list = list;
        this.mContext = mContext;
        this.frag1 = frag1;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.frag2_item, null);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final Holder holder, final int i) {
        final boolean select = list.get(i).isItemSelect();
        holder.frag_item2_CheckBox.setChecked(select);

        String sellerName = list.get(i).getTitle();
        holder.frag_item2_name.setText(sellerName);

        String price = list.get(i).getPrice()+"";
        holder.frag_item2_pice.setText(price);

        final int num = list.get(i).getNum();
        holder.he.setText(num+"");

        String images = list.get(i).getImages();
       // Glide.with(mContext).load(images).into(holder.frag_item2_ImageView);

        RoundedCorners roundedCorners= new RoundedCorners(100);
        Glide.with(mContext).load(images).apply(RequestOptions.bitmapTransform(roundedCorners)).into(holder.frag_item2_ImageView);


        //选中商家下的条目,总价变化
        holder.frag_item2_CheckBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.frag_item2_CheckBox.isChecked() == false){
                    int n1 = frag1.getSHChecAll2(holder.frag_item2_CheckBox.isChecked(), list.get(i));
                    frag1.setSChecAll(n1,i,false);
                    frag1.frag_CheckBox.setChecked(false);
                }else{
                    int n2 = frag1.setSHChecAll(holder.frag_item2_CheckBox.isChecked(), list.get(i));
                }
            }
        });

        //加
        holder.jia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int n1 = list.get(i).getNum();
                n1+=1;
                list.get(i).setNum(n1);
                frag1.total();
                notifyDataSetChanged();
            }
        });

        //减
        holder.jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int n2 = list.get(i).getNum();
                if (n2>=1){
                    n2-=1;
                }else{
                    n2=0;
                }
                list.get(i).setNum(n2);
                frag1.total();
                notifyDataSetChanged();
            }
        });

    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    public class Holder extends XRecyclerView.ViewHolder {

        public  CheckBox frag_item2_CheckBox;
        public  ImageView frag_item2_ImageView;
        public  TextView frag_item2_name,frag_item2_pice;
        public Button jia;
        public TextView he;
        public Button jian;

        public Holder(@NonNull View itemView) {
            super(itemView);
            frag_item2_CheckBox = itemView.findViewById(R.id.frag_item2_CheckBox);
            frag_item2_ImageView = itemView.findViewById(R.id.frag_item2_ImageView);
            frag_item2_name = itemView.findViewById(R.id.frag_item2_name);
            frag_item2_pice = itemView.findViewById(R.id.frag_item2_pice);
            jia = itemView.findViewById(R.id.jia);
            he = itemView.findViewById(R.id.he);
            jian = itemView.findViewById(R.id.jian);
        }
    }
}

6.Fragment碎片2,MyView

public class MyView extends View {

    public Paint paint;

    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(50,50,50,paint);
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

7.Fragment碎片2,适配器中的布局

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginLeft="50dp"
        >

    <CheckBox
        android:id="@+id/frag_item2_CheckBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="30dp"
        >
           <ImageView
               android:id="@+id/frag_item2_ImageView"
               android:layout_width="150dp"
               android:layout_height="150dp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"

            >
        <TextView
            android:id="@+id/frag_item2_name"
            android:text="55555555"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />
        <TextView
            android:id="@+id/frag_item2_pice"
            android:text="99999"
            android:textColor="#FF00"
            android:layout_marginTop="50dp"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />
        </LinearLayout>
    </LinearLayout>

       <LinearLayout
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:orientation="horizontal"
           android:layout_marginLeft="190dp"
           android:layout_marginTop="-20dp"
           >
           <Button
               android:text="+"
               android:id="@+id/jia"
               android:layout_width="50dp"
               android:layout_height="50dp" />
           <TextView
               android:id="@+id/he"
               android:text="1"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content" />
           <Button
               android:text="-"
               android:id="@+id/jian"
               android:layout_width="50dp"
               android:layout_height="50dp" />
       </LinearLayout>


    </LinearLayout>
</LinearLayout>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值