如何跨UI实现功能

  
package com.qf.anong.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.qf.anong.adapter.DownLoadInfoAdapter;
import com.qf.anong.anongproject.MainActivity;
import com.qf.anong.anongproject.R;
import com.qf.anong.bean.DownLoadInfo;
import com.qf.anong.inter.AllNet;
import com.qf.anong.inter.CallbackCheckedInfo;
import com.qf.anong.inter.OnSelectedItemChanged;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wang on 2016/5/20.
 */
public class ShowFragment extends Fragment implements View.OnClickListener {
    List<DownLoadInfo> list;
    private ListView down_load_list;
    String url = AllNet.url;
    ImageView back_last_act;
    TextView editId, add_money, close_account;
    CheckBox choose_allId;
    private DownLoadInfoAdapter adapter;
    private OnSelectedItemChanged onSelectItemChanged;

    private TypeToken<List<DownLoadInfo>> tabContentTypeToken;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
            savedInstanceState) {
        View v = inflater.inflate(R.layout.shopping_box, null);
        back_last_act = (ImageView) v.findViewById(R.id.back_last_act);
        down_load_list = (ListView) v.findViewById(R.id.down_load_list);
        editId = (TextView) v.findViewById(R.id.editId);


        choose_allId = (CheckBox) v.findViewById(R.id.choose_allId);//全选
        add_money = (TextView) v.findViewById(R.id.add_money);//合计总钱数
        close_account = (TextView) v.findViewById(R.id.close_account);

        setListener();
        return v;

    }

    private void setListener() {
        back_last_act.setOnClickListener(this);
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        getAllInfo();

        RequestParams params = new RequestParams(url);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                try {
                    JSONArray arry = new JSONObject(result).optJSONArray("products");
                    if (arry == null) return;

                    for (int i = 0; i < arry.length(); i++) {
                        JSONObject ob = arry.optJSONObject(i);
                        String title = ob.optString("title");
                        String price = ob.optString("price");
                        String picture = ob.optString("picture");
                        DownLoadInfo info = new DownLoadInfo(title, price, picture);
                        list.add(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                adapter.notifyDataSetChanged();
            }


            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {
                // Log.d("test",cex.toString());
            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void getAllInfo() {
        list = new ArrayList<>();
        adapter = new DownLoadInfoAdapter(list, getActivity(), onSelectItemChanged);
        down_load_list.setAdapter(adapter);
        adapter.setCallback(new CallbackCheckedInfo() {
            @Override
            public void checkedInfo(boolean isCheckedAll, float price) {
                choose_allId.setChecked(isCheckedAll);
                add_money.setText(price + "");

            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_last_act:
                Intent intent = new Intent(getActivity(), MainActivity.class);
                startActivity(intent);
                break;
//            case R.id.choose_allId://全选
//                choose_allId.setChecked(true);
//                adapter.selectAll();
//                choose_allId.setChecked(false);
//                adapter.disSelectAll();
//                break;

        }
    }


//    // 反选
//    public void switchSelect(View v) {
//        adapter.switchSelect();
//    }

}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/ll"
              xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="130dp"
              android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <CheckBox
            android:id="@+id/home_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="  **的农家院"
            android:textSize="20sp"
            />


        <TextView
            android:id="@+id/compile_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:paddingLeft="50dp"
            android:text="编辑"
            android:textSize="20sp"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:orientation="horizontal">

        <CheckBox
            android:id="@+id/singleId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="false"/>

        <ImageView
            android:id="@+id/produce_img"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="@mipmap/ic_launcher"/>

        <LinearLayout
            android:id="@+id/detail_info_ll"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/produce_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="name"
                android:textSize="15sp"
                />

            <TextView
                android:id="@+id/produce_type"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="类型"
                android:textSize="25sp"
                />

            <LinearLayout
                android:layout_width="100dp"
                android:layout_height="match_parent"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/price_id"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="price"
                    android:textSize="20sp"
                    />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="x"
                    android:textSize="20sp"/>

                <TextView
                    android:id="@+id/num_add"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingLeft="5dp"
                    android:text="1"
                    android:textColor="#00f"
                    android:textSize="20sp"/>

            </LinearLayout>

            <TextView
                android:id="@+id/display_price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="48.00"
                android:textColor="#eeff00"
                android:textSize="20sp"/>

        </LinearLayout>


    </LinearLayout>

</LinearLayout>
package com.qf.anong.inter;

/**
 * Created by Claudia on 2016/5/23 0023.
 */
public interface CallbackCheckedInfo {

    void checkedInfo( boolean isCheckedAll, float price);
}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
package com.qf.anong.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.qf.anong.anongproject.R;
import com.qf.anong.bean.DownLoadInfo;
import com.qf.anong.inter.CallbackCheckedInfo;
import com.qf.anong.inter.OnSelectedItemChanged;

import org.xutils.x;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/5/21 0021.
 */
public class DownLoadInfoAdapter extends BaseAdapter {
    List<DownLoadInfo> list;
    Context context;
    private OnSelectedItemChanged onSelectedItemChanged;
    private LayoutInflater ll_detail;
    Map<Integer, Boolean> map;
    private CallbackCheckedInfo callback;

    public void setCallback(CallbackCheckedInfo callback) {
        this.callback = callback;

    }

    public DownLoadInfoAdapter(List<DownLoadInfo> list, Context context, OnSelectedItemChanged onSelectedItemChanged) {
        this.onSelectedItemChanged = onSelectedItemChanged;
        this.list = list;
        this.context = context;
        ll_detail = LayoutInflater.from(context);
        map = new HashMap<Integer, Boolean>();
        for (int i = 0; i < list.size(); i++) {
            map.put(i, true);//默认都没有被选中
        }

    }

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

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View v;
        final viewHolder holder;
        if (convertView == null) {
            v = LayoutInflater.from(context).inflate(R.layout.download_detail, null);
            holder = new viewHolder();
//            holder.home_name = (TextView) v.findViewById(R.id.home_name);
//            holder.compile_id = (TextView) v.findViewById(R.id.compile_id);//编辑
            holder.singleId = (CheckBox) v.findViewById(R.id.singleId);
            holder.produce_name = (TextView) v.findViewById(R.id.produce_name);
            holder.price_id = (TextView) v.findViewById(R.id.price_id);
            holder.display_price = (TextView) v.findViewById(R.id.display_price);
//            holder.num_add = (TextView) v.findViewById(R.id.num_add);
            holder.produce_img = (ImageView) v.findViewById(R.id.produce_img);
            v.setTag(holder);
        } else {
            v = convertView;
            holder = (viewHolder) v.getTag();

        }
        DownLoadInfo info = list.get(position);
        holder.produce_name.setText(info.getTitile());
        holder.price_id.setText(info.getPrice());
        holder.display_price.setText(info.getPrice());
//        holder.radId.setChecked(map.get(position));
        holder.singleId.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                map.put(position, isChecked);
                //此时全选为false
                callBackInfo();

            }
        });

        x.image().bind(holder.produce_img, info.getPicture());
        return v;
    }

//    public int getSelectedCount(Map<Integer, Boolean> map) {
//        int i = 0;
//        for (Map.Entry<Integer, Boolean> entry : map.entrySet()) {
//            if (entry.getValue()) {
//                i++;
//            }
//        }
//
//        return i;
//    }

    class viewHolder {
        TextView home_name, compile_id, produce_name, produce_type, price_id, num_add, display_price;
        ImageView produce_img;
        CheckBox singleId;
    }

    private void callBackInfo() {
        float sumPrice = 0;
        boolean isAllChecked = true;

        for (Map.Entry<Integer, Boolean> entry : map.entrySet()) {
            if (!entry.getValue()) {
                isAllChecked = false;

            } else {
                String pr = list.get(entry.getKey()).getPrice();
                sumPrice += Float.parseFloat(pr);
//                String price = String.format("%.2f",sumPrice);
            }
        }

        if (callback != null) {
            callback.checkedInfo(isAllChecked, sumPrice);
//            String price = String.format("%.2f",sumPrice);
        }
    }
//
//    public void selectAll() { // 全选
//        for (int i = 0; i < list.size(); i++) {
//            map.put(i, true);
//        }
//        notifyDataSetChanged();
//    }
//
//    public void disSelectAll() { // 全不选
//        for (int i = 0; i < list.size(); i++) {
//            map.put(i, false);
//        }
//        notifyDataSetChanged();
//    }
//
//    public void switchSelect() { // 反选
//        for (int i = 0; i < list.size(); i++) {
//            boolean select = map.get(i);
//            map.put(i, !select);
//        }
//        notifyDataSetChanged();
//    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值