用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - blackberry代码库

listview全选、反选

2017-04-04 作者: 老四和老三的老二都老大了举报

[blackberry]代码库

//main布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="全选"
            android:onClick="checkAll"
            android:layout_height="wrap_content" />
        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="反选"
            android:onClick="checkNone"
            android:layout_height="wrap_content" />
    </LinearLayout>



    <ListView
        android:id="@+id/listView"
        android:layout_weight="1"
        android:layout_height="0dp"
        android:layout_width="match_parent"
         />
    <TextView
        android:layout_width="wrap_content"
        android:id="@+id/tv_sum"
        android:text="总价格"

        android:layout_height="wrap_content" />
</LinearLayout>

//条目布局

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

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/tv_price"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/tv_count"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

</LinearLayout>

//bean包
public class Product {
    private String title;
    private int price;
    private int count;
    private boolean isChecked;

    public Product(String title, int price, int count) {
        this.title = title;
        this.price = price;
        this.count = count;
    }

    public Product() {
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public boolean isChecked() {
        return isChecked;
    }

    public void setChecked(boolean checked) {
        isChecked = checked;
    }
}

public class MainActivity extends AppCompatActivity implements AbsListView.OnScrollListener {


    private ArrayList<Product> productList = new ArrayList<>();;
    private ProductAdapter productAdapter;
    private TextView tv_sum;
    //滑动到底部的时候,添加30条


    private int  currentIndex=0;
    private  int each_count=30;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView listView = (ListView) findViewById(R.id.listView);
        tv_sum = (TextView) findViewById(R.id.tv_sum);
        //初始化数据
        initData();
        //设置数据适配器
        productAdapter = new ProductAdapter(this, productList);
        listView.setAdapter(productAdapter);

        //设置滑动的监听
        listView.setOnScrollListener(this);
    }

    private void initData() {
        for (int i = currentIndex; i < currentIndex+each_count; i++) {
            productList.add(new Product("商品" + i, 10 + i, i));
        }
    }

    /**
     * 全选按钮
     * @param view
     */
    public void checkAll(View view) {
        //只需要对象
        for (int i = 0; i < productList.size(); i++) {
            //全选
            productList.get(i).setChecked(true);
        }
        //刷新
        productAdapter.notifyDataSetChanged();
        setPrice();
    }

    /**
     * 反选
     * @param view
     */
    public void checkNone(View view) {
        //只需要对象
        for (int i = 0; i < productList.size(); i++) {
            //全选
//            productList.get(i).setChecked(false);
            productList.get(i).setChecked(!productList.get(i).isChecked());
        }
        //刷新
        productAdapter.notifyDataSetChanged();
        setPrice();
    }

    /**
     * 修改价格
     */
    public void setPrice() {
        int price = 0;
        //只需要对象
        for (int i = 0; i < productList.size(); i++) {
            //全选
//            productList.get(i).setChecked(false);
            boolean checked = productList.get(i).isChecked();
            if (checked) {
                price = price + productList.get(i).getPrice() * productList.get(i).getCount();
            }
        }
        tv_sum.setText("总价格:"+price);
    }

    /**
     * 滑动状态改变
     * @param absListView
     * @param
     */
    @Override
    public void onScrollStateChanged(AbsListView absListView, int state) {
        //静止状态
        if(state== AbsListView.OnScrollListener.SCROLL_STATE_IDLE){
            //并且当前可以看到的条目的最后一个是集合的最后一个
            int lastVisiblePosition = absListView.getLastVisiblePosition();
            if(lastVisiblePosition==productList.size()-1){
                //加载数据
                currentIndex=currentIndex+each_count;
                //加载数据
                initData();
                //刷新适配器
                productAdapter.notifyDataSetChanged();
            }
        }
    }

    /**
     * 滑动的过程
     * @param absListView
     * @param i
     * @param i1
     * @param i2
     */
    @Override
    public void onScroll(AbsListView absListView, int i, int i1, int i2) {

    }
}

 @Override
    public View getView(final int i, View convertView, ViewGroup viewGroup) {
        //代码优化
        final ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = View.inflate(context, R.layout.lv_item, null);
            //找控件
            viewHolder.tv_count = (TextView) convertView.findViewById(R.id.tv_count);
            viewHolder.tv_price = (TextView) convertView.findViewById(R.id.tv_price);
            viewHolder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
            //设置标记
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }


        viewHolder.tv_title.setText(productList.get(i).getTitle());
        viewHolder.tv_count.setText(productList.get(i).getCount() + "");//ResouceNotfoundException
        viewHolder.tv_price.setText(productList.get(i).getPrice() + "");

//        viewHolder.checkBox.setOnCheckedChangeListener();
        viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //判断一下当前是否选中
                boolean checked = viewHolder.checkBox.isChecked();
                productList.get(i).setChecked(checked);
                //重新设置价格
                MainActivity mainActivity = (MainActivity) context;
                mainActivity.setPrice();
            }
        });
        //设置是否选中
        viewHolder.checkBox.setChecked(productList.get(i).isChecked());
        return convertView;
    }


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...