仿购物车模块,点击编辑(完成)删除一个或者多个内容,上拉加载,下拉刷新

首先创建一个Activity
主界面布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_user_collection"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/bg"
    android:orientation="vertical"
    tools:context="com.sanbanhui.helper.activity.UserCollectionActivity">

    <RelativeLayout
        android:id="@+id/collection_title"
        android:layout_width="fill_parent"
        android:layout_height="@dimen/margin45"
        android:background="@color/title_bg"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/btn_navigation_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="@dimen/margin10"
            android:background="@drawable/icon_back" />

        <TextView
            android:id="@+id/tv_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="@dimen/margin5"
            android:layout_toRightOf="@id/btn_navigation_back"
            android:text="@string/back"
            android:textColor="@color/white"
            android:textSize="@dimen/margin18" />

        <TextView
            android:id="@+id/tv_navigation_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/user_collection_title"
            android:textColor="@color/white"
            android:textSize="@dimen/margin20" />

        <TextView
            android:id="@+id/title_edit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="@dimen/margin10"
            android:text="@string/user_collection_edit"
            android:textColor="@color/white"
            android:textSize="@dimen/textSize15" />
    </RelativeLayout>


    <!--暂无数据 -->

    <LinearLayout
        android:id="@+id/layout_empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:layout_marginBottom="@dimen/margin10"
        android:visibility="gone"
        >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/icon_empty" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/margin15"
            android:text="@string/empty_text"
            android:textColor="@color/c_999"
            android:textSize="@dimen/margin15" />
    </LinearLayout>


    <!--加载数据失败 -->
    <LinearLayout
        android:id="@+id/layout_error"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:visibility="gone">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_marginTop="@dimen/margin140"
            android:gravity="center_horizontal"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/icon_error" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/margin15"
                android:text="@string/error_1"
                android:textColor="@color/c_333"
                android:textSize="@dimen/margin15" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/margin5"
                android:text="@string/error_2"
                android:textColor="@color/c_333"
                android:textSize="@dimen/margin15" />

            <LinearLayout
                android:id="@+id/layout_refresh"
                android:layout_width="@dimen/margin150"
                android:layout_height="@dimen/margin40"
                android:layout_marginTop="@dimen/margin20"
                android:background="@drawable/error_drw_bg"
                android:orientation="horizontal">

                <View
                    android:layout_width="@dimen/margin25"
                    android:layout_height="@dimen/margin25"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="@dimen/margin10"
                    android:background="@drawable/icon_refresh" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text="@string/refresh"
                    android:textColor="@color/white"
                    android:textSize="@dimen/margin20" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/layout_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/collection_title">


        <LinearLayout
            android:id="@+id/collection_delete"
            android:layout_width="match_parent"
            android:layout_height="@dimen/margin50"
            android:layout_alignParentBottom="true"
            android:background="@color/white"
            android:orientation="horizontal"
            android:visibility="gone">

            <CheckBox
                android:id="@+id/collection_checkbox"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="@dimen/margin15"
                android:button="@drawable/checkbox" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="@dimen/margin10"
                android:layout_weight="1"
                android:text="全选"
                android:textColor="@color/collection_check"
                android:textSize="@dimen/textSize13" />

            <LinearLayout
                android:id="@+id/user_delete"
                android:layout_width="@dimen/margin100"
                android:layout_height="@dimen/margin35"
                android:layout_gravity="center"
                android:layout_marginRight="@dimen/margin15"
                android:background="@drawable/common_btn_drw_bg2"
                android:orientation="horizontal">

                <View
                    android:layout_width="@dimen/margin10"
                    android:layout_height="@dimen/margin15"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="@dimen/margin30"
                    android:background="@drawable/home_trash" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="@dimen/margin10"
                    android:gravity="center"
                    android:text="@string/user_collection_delete"
                    android:textColor="@color/white"
                    android:textSize="@dimen/textSize15" />
            </LinearLayout>

        </LinearLayout>

         <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_above="@id/collection_delete"
            >

            <com.handmark.pulltorefresh.library.PullToRefreshListView
                android:id="@+id/layout_scrollView"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:divider="@null"
                android:scrollbars="none"></com.handmark.pulltorefresh.library.PullToRefreshListView>
        </LinearLayout>

    </RelativeLayout>

    <ProgressBar
        android:id="@+id/progress"
        android:layout_width="@dimen/margin35"
        android:layout_height="@dimen/margin35"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="@dimen/margin90"
        android:indeterminateBehavior="repeat"
        android:indeterminateDrawable="@drawable/progressbar_drw_bg"
        android:indeterminateOnly="true"
        android:visibility="gone" />
</RelativeLayout>

上面的PullToRefreshListView是上拉加载,下拉刷新
下面是主Activity

package com.sanbanhui.helper.activity;

import android.app.Activity;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.sanbanhui.helper.R;
import com.sanbanhui.helper.adapter.UserCollectionAdapter;
import com.sanbanhui.helper.bean.CollectionBean;
import com.sanbanhui.helper.presenterimpl.CollectDeleteInfoPresenterImpl;
import com.sanbanhui.helper.presenterimpl.UserCollectionPresenterImpl;
import com.sanbanhui.helper.utils.ToastUtils;
import com.sanbanhui.helper.view.CollectDeleteView;
import com.sanbanhui.helper.view.UserCollectionView;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
* 收藏列表
* */
public class UserCollectionActivity extends Activity implements UserCollectionView, CollectDeleteView {

    @BindView(R.id.btn_navigation_back)
    ImageView btnBack;
    @BindView(R.id.tv_back)
    TextView tvBack;
    @BindView(R.id.layout_scrollView)
    PullToRefreshListView listView;

    @BindView(R.id.collection_checkbox)
    CheckBox collectionCheckbox;
    @BindView(R.id.user_delete)
    LinearLayout userDelete;
    @BindView(R.id.title_edit)
    TextView titleEdit;
    @BindView(R.id.collection_delete)
    LinearLayout collectionDelete;
    @BindView(R.id.progress)
    ProgressBar progressBar;
    @BindView(R.id.layout_empty)
    View layoutEmpty;
    @BindView(R.id.layout_error)
    View layoutError;
    @BindView(R.id.layout_refresh)
    View layoutRefresh;
    @BindView(R.id.layout_content)
    View layoutContent;

    private ArrayList<CollectionBean> list = new ArrayList<>();
    private boolean flg = true;
    private UserCollectionAdapter collectionAdapter;
    private int page;
    private UserCollectionPresenterImpl userCollectionPresenterImpl;
    private CollectDeleteInfoPresenterImpl collectDeleteInfoPresenter;
    private ArrayList<String> list2 = new ArrayList<String>();


    @OnClick(R.id.btn_navigation_back)
    void onBtnBack() {
        finish();
    }

    @OnClick(R.id.tv_back)
    void onTvBack() {
        finish();
    }

    @OnClick(R.id.collection_checkbox)
    void onCheckbox() {
        boolean flag = collectionCheckbox.isChecked();

        for (int i = 0; i < collectionAdapter.getSelect().size(); i++) {
            collectionAdapter.getSelect().set(i, flag);
        }
        collectionAdapter.notifyDataSetChanged();
    }

    @OnClick(R.id.title_edit)
    void onEdit() {
        if (flg) {
            titleEdit.setText(R.string.user_collection_finish);
            collectionDelete.setVisibility(View.VISIBLE);
            flg = false;
            collectionAdapter = new UserCollectionAdapter(this, list, flg, collectionCheckbox);
            listView.setAdapter(collectionAdapter);
            collectionAdapter.notifyDataSetChanged();
        } else {
            titleEdit.setText(R.string.user_collection_edit);
            collectionDelete.setVisibility(View.GONE);
            flg = true;
            collectionAdapter = new UserCollectionAdapter(this, list, flg, collectionCheckbox);
            listView.setAdapter(collectionAdapter);

            if (list2.size() != 0) {
                collectDeleteInfoPresenter.execute(getParams(list2));
            }
            collectionAdapter.notifyDataSetChanged();
            collectionCheckbox.setChecked(false);
        }
    }

    @OnClick(R.id.user_delete)
    void onUserDelete() {
        if (collectionAdapter.getSelect().contains(true)) {
            deleteItem();
        } else {
            Toast.makeText(UserCollectionActivity.this, "请选择你要删除的内容", Toast.LENGTH_SHORT).show();
        }

    }

    @OnClick(R.id.layout_refresh)
    void onRefresh() {
        initData();
    }

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

        ButterKnife.bind(this);

        listView.setMode(PullToRefreshBase.Mode.BOTH);
        listView.setOnRefreshListener(onRefreshListener2);

        //加载数据
        initData();

        collectionAdapter = new UserCollectionAdapter(this, list, flg, collectionCheckbox);
        listView.setAdapter(collectionAdapter);
    }

    private void initData() {
        userCollectionPresenterImpl = new UserCollectionPresenterImpl(this);
        page = 1;
        userCollectionPresenterImpl.execute(page);

        collectDeleteInfoPresenter = new CollectDeleteInfoPresenterImpl(this);

    }


    @Override
    public void showProgress() {
        if (list.size() == 0) {
            progressBar.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void hideProgress() {
        progressBar.setVisibility(View.GONE);
    }

    @Override
    public void showFailure(String message) {
        layoutContent.setVisibility(View.GONE);
        layoutEmpty.setVisibility(View.GONE);
        layoutError.setVisibility(View.VISIBLE);
        listView.onRefreshComplete();
    }

    //删除成功
    @Override
    public void onDeleteSuccess() {
        page = 1;
        userCollectionPresenterImpl.execute(page);
        Toast.makeText(UserCollectionActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
        list2.clear();
    }

    @Override
    public void showError(String message) {
        ToastUtils.show(this, message, Toast.LENGTH_SHORT);
    }

    @Override
    public void onLoadSuccess(ArrayList<CollectionBean> listData) {


        if (page == 1) {
            this.list.clear();
        }

        this.list.addAll(listData);
        if (!flg) {
            collectionAdapter.addLinked(listData);
        }

        collectionAdapter.notifyDataSetChanged();
        listView.onRefreshComplete();

        layoutContent.setVisibility(View.VISIBLE);
        layoutEmpty.setVisibility(View.GONE);
        layoutError.setVisibility(View.GONE);

        if (this.list.size() == 0) {
            layoutContent.setVisibility(View.GONE);
            layoutError.setVisibility(View.GONE);
            layoutEmpty.setVisibility(View.VISIBLE);
        }
    }


    //删除
    public void deleteItem() {

        ArrayList<CollectionBean> collectionBeanArrayList = new ArrayList<>();
        ArrayList<Boolean> removeList = new ArrayList<>();
        int count = list.size() - 1;
        for (int i = count; i >= 0; i--) {

            if (collectionAdapter.getSelect().get(i).equals(true)) {

                list2.add(list.get(i).getId());
                collectionBeanArrayList.add(list.get(i));
                removeList.add(collectionAdapter.getSelect().get(i));
                //list.remove(i);
                //collectionAdapter.getSelect().remove(i);
            }
        }

        list.removeAll(collectionBeanArrayList);
        collectionAdapter.getSelect().removeAll(removeList);

        //如果是全部删除,全选框设为未选中
        if (list.size() == 0) {
            collectionCheckbox.setChecked(false);
        }

        collectionAdapter.notifyDataSetChanged();


    }

    protected void onPause() {
        super.onPause();

        MobclickAgent.onPageEnd("UserCollectionActivity");
        MobclickAgent.onPause(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("UserCollectionActivity");
        MobclickAgent.onResume(this);
    }

    private String getParams(ArrayList<String> list) {
        String result = "[";
        for (int i = 0; i < list.size(); i++) {

            result += "\"" + list.get(i) + "\"";
            if (i != list.size() - 1) {
                result += ",";
            }
        }
        result += "]";

        return result;
    }

    private PullToRefreshBase.OnRefreshListener2 onRefreshListener2 = new PullToRefreshBase.OnRefreshListener2() {
        @Override
        public void onPullDownToRefresh(PullToRefreshBase refreshView) {
            listView.getLoadingLayoutProxy().setRefreshingLabel("正在刷新");
            listView.getLoadingLayoutProxy().setPullLabel("下拉刷新");
            listView.getLoadingLayoutProxy().setReleaseLabel("释放开始刷新");

            String label = DateUtils.formatDateTime(UserCollectionActivity.this, System.currentTimeMillis(),
                    DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
            listView.getLoadingLayoutProxy().setLastUpdatedLabel("最后刷新时间: " + label);
            progressBar.setVisibility(View.GONE);
            page = 1;
            userCollectionPresenterImpl.execute(page);
        }

        @Override
        public void onPullUpToRefresh(PullToRefreshBase refreshView) {
            listView.getLoadingLayoutProxy().setRefreshingLabel("正在加载");
            listView.getLoadingLayoutProxy().setPullLabel("上拉加载更多");
            listView.getLoadingLayoutProxy().setReleaseLabel("释放开始加载");

            String label = DateUtils.formatDateTime(UserCollectionActivity.this, System.currentTimeMillis(),
                    DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
            listView.getLoadingLayoutProxy().setLastUpdatedLabel("最后加载时间: " + label);
            progressBar.setVisibility(View.GONE);
            page++;
            userCollectionPresenterImpl.execute(page);
        }
    };
}

自定义适配器

package com.sanbanhui.helper.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.sanbanhui.helper.R;
import com.sanbanhui.helper.bean.CollectionBean;
import com.sanbanhui.helper.utils.IntentUtils;

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

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by liugang on 2017/3/8.
 */

public class UserCollectionAdapter extends BaseAdapter {

    private final Activity activity;
    private final ArrayList<CollectionBean> list;
    private boolean flg;
    private CheckBox collectionCheckbox;
    ViewHolder holder = null;
    private LinkedList<Boolean> linkedList = new LinkedList<Boolean>();

    public UserCollectionAdapter(Activity activity, ArrayList<CollectionBean> list, boolean flg, CheckBox collectionCheckbox) {

        this.activity = activity;
        this.list = list;
        this.flg = flg;
        this.collectionCheckbox = collectionCheckbox;

        linkedList.clear();
        // 初始化
        addLinked(list);
    }

    @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;
    }

    public List<Boolean> getSelect() {
        return linkedList;
    }

    public void addLinked(ArrayList<CollectionBean> list) {
        // 初始化
        for (int i = 0; i < list.size(); i++) {
            getSelect().add(false);
        }
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = LayoutInflater.from(activity).inflate(R.layout.user_collection_list, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.collectionListContent.setText(list.get(position).getTitle());
        holder.collectionListName.setText(list.get(position).getSource());
        holder.collectionListData.setText(list.get(position).getDate());

        if (flg == true) {
            holder.collectionListCheckbox.setVisibility(View.GONE);

        } else {
            holder.collectionListCheckbox.setVisibility(View.VISIBLE);
        }

        if (linkedList.size() != 0) {
            holder.collectionListCheckbox.setChecked(linkedList.get(position));
        }
        // 不能用onCheckChangedListner()复用的时候
        holder.checkboxBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                linkedList.set(position, !linkedList.get(position));

                if (linkedList.contains(false)) {
                    collectionCheckbox.setChecked(false);
                } else {
                    collectionCheckbox.setChecked(true);
                }

                // 刷新
                notifyDataSetChanged();
            }
        });
        holder.collectionListCheckbox.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                linkedList.set(position, !linkedList.get(position));

                if (linkedList.contains(false)) {
                    collectionCheckbox.setChecked(false);
                } else {
                    collectionCheckbox.setChecked(true);
                }

                // 刷新
                notifyDataSetChanged();
            }
        });

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CollectionBean zixunNewsBean = list.get(position);
                IntentUtils.showNewsContentActivity(activity, zixunNewsBean.getShareTitle(), zixunNewsBean.getShareSummary(), zixunNewsBean.getShareLogo(), zixunNewsBean.getShareLink(), "", "", zixunNewsBean.getId());
            }
        });

        return convertView;
    }

    class ViewHolder {
        @BindView(R.id.collection_list_content)
        TextView collectionListContent;
        @BindView(R.id.collection_list_name)
        TextView collectionListName;
        @BindView(R.id.collection_list_data)
        TextView collectionListData;
        @BindView(R.id.collection_list_checkbox)
        CheckBox collectionListCheckbox;
        @BindView(R.id.checkbox_btn)
        View checkboxBtn;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }


}

适配器布局文件

<?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="match_parent"
    android:background="@color/bg"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/margin110"
        android:layout_marginBottom="@dimen/margin5"
        android:background="@drawable/common_drw2_bg">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/margin100"
            android:layout_marginLeft="@dimen/margin11"
            android:layout_marginRight="@dimen/margin20"
            android:layout_marginTop="@dimen/margin12"
            android:orientation="vertical">

            <TextView
                android:id="@+id/collection_list_content"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="按时大大大大大是方法对方的答复呵呵当时"
                android:textColor="@color/collection_list_content"
                android:textSize="@dimen/textSize14" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:orientation="horizontal"
                android:layout_marginBottom="@dimen/margin11"
                >
                <TextView
                    android:id="@+id/collection_list_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="asd"
                    android:textSize="@dimen/textSize12"
                    android:textColor="@color/collection_list_data"
                    />
                <TextView
                    android:id="@+id/collection_list_data"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="@dimen/textSize12"
                    android:textColor="@color/collection_list_data"
                    android:text="2017-03-08"
                    android:layout_marginLeft="@dimen/margin30"
                    android:layout_weight="1"
                    />
                <CheckBox
                    android:id="@+id/collection_list_checkbox"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:button="@drawable/checkbox"
                    android:visibility="gone"
                    />
            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>

</LinearLayout>

这样就能点击编辑按钮出现删除按钮,点击删除删掉本地数据,点击完成隐藏checkbox保存删除掉的数据,点击编辑显示。

上拉加载下拉刷新连接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值