Android練手小項(xiàng)目(KTReader)基于mvp架構(gòu)(六)

上路傳送眼:

Android練手小項(xiàng)目(KTReader)基于mvp架構(gòu)(五)

GIthub地址: https://github.com/yiuhet/KTReader

上篇文章中我們完成了圖片模塊。
而這次我們要做的的就是歷史記錄和收藏功能。

慣例上圖

效果圖啊圖

這次我們完成的功能有:

  • 建立數(shù)據(jù)庫(kù)保存歷史紀(jì)錄和收藏記錄
  • 清空歷史紀(jì)錄
  • 足跡板塊記錄了知乎日?qǐng)?bào)的歷史記錄,點(diǎn)擊可進(jìn)入詳情頁(yè)
  • 收藏板塊記錄了收藏的文章和圖片,點(diǎn)擊可進(jìn)入詳情頁(yè)

所用到的知識(shí)點(diǎn)有:

  • SQLiteOpenHelper
  • 數(shù)據(jù)庫(kù)的增刪查
  • ExpandableListView

可完善和加強(qiáng)的內(nèi)容或功能有:

  • 收藏頁(yè)圖片的排列美化

1. 數(shù)據(jù)庫(kù)的建立

我們?cè)跀?shù)據(jù)庫(kù)中建立三個(gè)表儲(chǔ)存數(shù)據(jù):

  • History
  • Collect
  • Unsplash

三個(gè)表里為了簡(jiǎn)單我們儲(chǔ)存同樣的數(shù)據(jù)類型:
model.entity.HistoryCollect

public class HistoryCollect {

    private String title;
    private String url;
    private String time;

    public HistoryCollect(String title, String url, String time) {
        this.title = title;
        this.url = url;
        this.time = time;
    }

    public String getTitle() {
        return title;
    }

    public String getUrl() {
        return url;
    }

    public String getTime() {
        return time;
    }
}

我們寫個(gè)MyDataBaseHelper繼承自SQLiteOpenHelper,使用這個(gè)類來(lái)建立數(shù)據(jù)庫(kù)和表。

utils.MyDataBaseHelper

public class MyDataBaseHelper extends SQLiteOpenHelper {

    public static final String HISTORY = "History";
    public static final String COLLECT = "Collect";
    public static final String UNSPLASH = "Unsplash";


    public static final String CREATE_TABLE = "create table %s ("
            + "id integer primary key autoincrement, "
            + "title text, "
            + "url text, "
            + "time text)";

    private Context mContext;

    public MyDataBaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
        mContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(String.format(CREATE_TABLE,HISTORY));
        db.execSQL(String.format(CREATE_TABLE,COLLECT));
        db.execSQL(String.format(CREATE_TABLE,UNSPLASH));
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

2. M層(操作數(shù)據(jù)庫(kù))

由于上面三個(gè)表中的數(shù)據(jù)為同一類型,所以我們的M層可以通用在歷史記錄和收藏記錄上。我們先寫一個(gè)工具類來(lái)統(tǒng)一操作數(shù)據(jù)庫(kù),這個(gè)工具類我們使其為單例,類里有以下方法:

  • insertData(String table, String title, String url)
  • isExist(String table, String url)
  • deleteDataCollect(String table, String id)
  • getData(String table)
  • clearHistory()
    方法的功能如方法名所示,根據(jù)傳進(jìn)來(lái)的不同表名來(lái)對(duì)不同表進(jìn)行增刪查改的操作。

utils.DBUtils

public class DBUtils {
    private static DBUtils sDBUtis;
    private SQLiteDatabase mSQLiteDatabase;

    private DBUtils(Context context) {
        mSQLiteDatabase = new MyDataBaseHelper(context, "KTReader.db", null, 1).getWritableDatabase();
    }

    public static DBUtils getInstence(Context context) {
        if (sDBUtis == null) {
            synchronized (DBUtils.class) {
                if (sDBUtis == null) {
                    sDBUtis = new DBUtils(context);
                }
            }
        }
        return sDBUtis;
    }

    public void insertData(String table, String title, String url) {

        //每個(gè)表儲(chǔ)存數(shù)量上限為20條,超過(guò)就刪除最舊的記錄
        Cursor cursor = mSQLiteDatabase.query(table, null, null, null, null, null, "id asc");
        if (cursor.getCount() > 20 && cursor.moveToNext()) {
            mSQLiteDatabase.delete(table, "id=?", new String[]{String.valueOf(cursor.getInt(cursor.getColumnIndex("id")))});
        }
        cursor.close();
        //插入數(shù)據(jù)
        ContentValues contentValues = new ContentValues();
        contentValues.put("title", title);
        contentValues.put("url", url);
        Calendar c = Calendar.getInstance();
        String time = String.format("%d月%d日",c.get(Calendar.MONTH)+1,c.get(Calendar.DATE));
        contentValues.put("time", time);

        mSQLiteDatabase.insertWithOnConflict(table, null, contentValues, SQLiteDatabase.CONFLICT_REPLACE);
    }

    public boolean isExist(String table, String url) {
        boolean isRead = false;
        Cursor cursor = mSQLiteDatabase.query(table, null, null, null, null, null, null);
        while (cursor.moveToNext()) {
            if (cursor.getString(cursor.getColumnIndex("url")).equals(url)) {
                isRead = true;
                break;
            }
        }
        cursor.close();
        return isRead;
    }

    public void deleteDataCollect(String table, String id) {
        mSQLiteDatabase.delete(table,"url=?", new String[] {id});
    }

    public List<HistoryCollect> getData(String table) {

        List<HistoryCollect> historyCollectList = new ArrayList<>();

        Cursor cursor = mSQLiteDatabase.query(table, null, null, null, null, null, "id desc");
        while (cursor.moveToNext()) {
            String title;
            String url;
            String time;
            title = cursor.getString(cursor.getColumnIndex("title"));
            url = cursor.getString(cursor.getColumnIndex("url"));
            time = cursor.getString(cursor.getColumnIndex("time"));
            HistoryCollect historyCollect = new HistoryCollect(title, url, time);
            historyCollectList.add(historyCollect);
        }
        cursor.close();
        return historyCollectList;
    }

    public void clearHistory() {
        mSQLiteDatabase.delete(MyDataBaseHelper.HISTORY, null, null);
    }
}


按照mvp的結(jié)構(gòu),開始寫m層的接口,接口簡(jiǎn)單的實(shí)現(xiàn)讀取三個(gè)表和清空歷史的方法:
model.HistoryCollectModel

public interface HistoryCollectModel {
    void loadHistory(OnHistoryCollectListener listener);
    void loadCollect(OnHistoryCollectListener listener);
    void loadUnsplash(OnHistoryCollectListener listener);
    void clearHistory();
}

最后就是model的實(shí)現(xiàn)類,實(shí)現(xiàn)類里分別創(chuàng)建三個(gè)數(shù)組保存不同表的數(shù)據(jù):
model.imp1.HistoryCollectModelImp1

public class HistoryCollectModelImp1 implements HistoryCollectModel {

    private List<HistoryCollect> mHistoryList;
    private List<HistoryCollect> mCollectList;
    private List<HistoryCollect> mUnsplashList;

    @Override
    public void loadHistory(OnHistoryCollectListener listener) {
        mHistoryList = DBUtils.getInstence(MyApplication.getContext()).getData(MyDataBaseHelper.HISTORY);
        if (mHistoryList.size() > 0) {
            listener.onHistorySuccess(mHistoryList);
        } else {
            listener.onError("未找到數(shù)據(jù)");
        }

    }

    @Override
    public void loadCollect(OnHistoryCollectListener listener) {
        mCollectList = DBUtils.getInstence(MyApplication.getContext()).getData(MyDataBaseHelper.COLLECT);
        if (mCollectList.size() > 0) {
            listener.onCollectSuccess(mCollectList);
        } else {
            listener.onError("未找到數(shù)據(jù)");
        }
    }

    @Override
    public void loadUnsplash(OnHistoryCollectListener listener) {
        mUnsplashList = DBUtils.getInstence(MyApplication.getContext()).getData(MyDataBaseHelper.UNSPLASH);
        if (mUnsplashList.size() > 0) {
            listener.onUnsplashSuccess(mUnsplashList);
        } else {
            listener.onError("未找到數(shù)據(jù)");
        }
    }
    
    @Override
    public void clearHistory() {
        DBUtils.getInstence(MyApplication.getContext()).clearHistory();
    }
}

其中OnHistoryCollectListener這一回調(diào)接口在p層實(shí)現(xiàn):
presenter.listener.OnHistoryCollectListener

public interface OnHistoryCollectListener {
    void onHistorySuccess(List<HistoryCollect> historyList);
    void onCollectSuccess(List<HistoryCollect> collectList);
    void onUnsplashSuccess(List<HistoryCollect> unsplashList);
    void onError(String error);
}

3. V層

為了通用,我們直接在接口中實(shí)現(xiàn)所有的方法:
view.HistoryCollectView

public interface HistoryCollectView {

    void onStartGetData();
    void onGetHistorySuccess(List<HistoryCollect> history);
    void onGetCollectSuccess(List<HistoryCollect> collect);
    void onGetUnsplashSuccess(List<HistoryCollect> unsplash);
    void onGetDataFailed(String error);
}
足跡功能

歷史記錄界面可以只簡(jiǎn)單的使用RecyclerView,M層通過(guò)P層把數(shù)據(jù)傳進(jìn)activity后,更新adapter,實(shí)現(xiàn)歷史記錄的展示,同時(shí)在toolbar上做個(gè)清空歷史的按鈕,點(diǎn)擊就清空History表。
HistoryActivity代碼里并沒(méi)有什么新的東西,之前好多activity都有相似的代碼.這里也就不贅述了。
ui.activity.HistoryActivity

public class HistoryActivity extends MVPBaseActivity<HistoryCollectView, HistoryCollectPresenterImp1> implements HistoryCollectView {

    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.recycle_history)
    RecyclerView mRecycleHistory;
    @BindView(R.id.prograss)
    ProgressBar mPrograss;

    List<HistoryCollect> mHistoryCollects = new ArrayList<>();
    HistoryAdapter mHistoryAdapter;

    @Override
    protected HistoryCollectPresenterImp1 createPresenter() {
        return new HistoryCollectPresenterImp1(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        initToolbar();
        mPresenter.getData(MyDataBaseHelper.HISTORY);
        mRecycleHistory.setLayoutManager(new LinearLayoutManager(this));
        mRecycleHistory.setHasFixedSize(true);
        mRecycleHistory.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        mRecycleHistory.setItemAnimator(new DefaultItemAnimator());
        mHistoryAdapter = new HistoryAdapter(this, mHistoryCollects);
        mHistoryAdapter.setOnItemClickListener(mOnItemClickListener);
        mRecycleHistory.setAdapter(mHistoryAdapter);
    }

    HistoryAdapter.OnItemClickListener mOnItemClickListener = new HistoryAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(String id, String title) {
            Intent intent = new Intent(getContext(), ZhihuDetailActivity.class);
            intent.putExtra("ZHIHUID",id);
            intent.putExtra("ZHIHUTITLE",title);
            startActivity(intent);
        }
    };

    private void initToolbar() {
        mToolbar.setTitle("足跡");
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_history;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_history, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_clear) {
            showClearDialog();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void showClearDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle("是否確定清空歷史紀(jì)錄")
                .setPositiveButton("確定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mPresenter.clearHistory();
                        toast("記錄已清空");
                        mHistoryCollects.clear();
                        mHistoryAdapter.notifyDataSetChanged();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
        builder.show();
    }

    @Override
    public void onStartGetData() {
        if (mPrograss != null) {
            mPrograss.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onGetHistorySuccess(List<HistoryCollect> historyCollects) {
        if (mPrograss != null) {
            mPrograss.setVisibility(View.GONE);
        }
        mHistoryCollects = historyCollects;
        if (mHistoryAdapter != null) {
            mHistoryAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onGetCollectSuccess(List<HistoryCollect> historyCollects) {

    }

    @Override
    public void onGetUnsplashSuccess(List<HistoryCollect> historyCollects) {

    }


    @Override
    public void onGetDataFailed(String error) {
        if (mPrograss != null) {
            mPrograss.setVisibility(View.GONE);
        }
        toast(error);
    }
}

其中adapter的代碼:
adapter.HistoryAdapter

public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.HistoryViewHolder>{


    private OnItemClickListener mItemClickListener;
    private List<HistoryCollect> mHistoryCollects = new ArrayList<>();
    private Context mContext;

    public HistoryAdapter(Context context, List<HistoryCollect> historyCollects) {
        mHistoryCollects = historyCollects;
        mContext = context;
    }


    @Override
    public HistoryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        HistoryItem historyItem = new HistoryItem(mContext);
        return new HistoryViewHolder(historyItem);
    }

    @Override
    public void onBindViewHolder(final HistoryViewHolder holder, int position) {
        final HistoryCollect historyCollect = mHistoryCollects.get(position);
        holder.historyItem.bindView(historyCollect);
        holder.historyItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mItemClickListener != null) {
                    mItemClickListener.onItemClick(historyCollect.getUrl(),historyCollect.getTitle());
                }
            }
        });
    }


    public class HistoryViewHolder extends RecyclerView.ViewHolder {
        public HistoryItem historyItem;

        public HistoryViewHolder(HistoryItem itemView) {
            super(itemView);
            historyItem = itemView;
        }
    }

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


    public interface OnItemClickListener {
        void onItemClick(String id, String title);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mItemClickListener = listener;
    }
}
收藏功能

收藏界面我們使用了折疊列表,列表內(nèi)有知乎日?qǐng)?bào)和圖片精選兩項(xiàng),其中知乎日?qǐng)?bào)的視圖和歷史界面一樣,圖片精選也只是簡(jiǎn)單的imageview+textview顯示圖片和保存日期。這里主要講講ExpandableListView的適配器寫法。
CollectAdapter是為ExpandableListView寫的適配器,繼承自BaseExpandableListAdapter,里面需要重寫的方法有:

  • int getGroupCount()
    **獲得父項(xiàng)的數(shù)量 **
  • int getChildrenCount(int groupPosition)
    獲得某個(gè)父項(xiàng)的子項(xiàng)數(shù)目
  • Object getGroup(int groupPosition)
    獲得某個(gè)父項(xiàng)
  • Object getChild(int groupPosition, int childPosition)
    獲得某個(gè)父項(xiàng)的某個(gè)子項(xiàng)
  • long getGroupId(int groupPosition)
    **獲得某個(gè)父項(xiàng)的id **
  • long getChildId(int groupPosition, int childPosition)
    **獲得某個(gè)父項(xiàng)的某個(gè)子項(xiàng)的id **
  • boolean hasStableIds()
    **表名同一ID是否總是引用同一對(duì)象,不用管 **
  • View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
    獲得父項(xiàng)顯示的view
  • View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
    獲得子項(xiàng)顯示的view
  • boolean isChildSelectable(int groupPosition, int childPosition)
    **子項(xiàng)是否可選中,點(diǎn)擊事件要返回ture **

知道了每個(gè)方法的作用之后,就很好寫了,我們要在CollectAdapter中定義兩個(gè)數(shù)據(jù)列表,父項(xiàng)和子項(xiàng)的,為了解析view 我們還要個(gè)Context對(duì)象,所以構(gòu)建函數(shù)就寫出來(lái)了。

public CollectAdapter(ArrayList<String> gData,ArrayList<ArrayList<HistoryCollect>> iData, Context mContext) {
        this.gData = gData;
        this.iData = iData;
        this.mContext = mContext;
    }

后面的方法詳情見(jiàn)代碼:
adapter.CollectAdapter

public class CollectAdapter extends BaseExpandableListAdapter{

    private ArrayList<String> gData;
    private ArrayList<ArrayList<HistoryCollect>> iData;
    private Context mContext;

    public CollectAdapter(ArrayList<String> gData,ArrayList<ArrayList<HistoryCollect>> iData, Context mContext) {
        this.gData = gData;
        this.iData = iData;
        this.mContext = mContext;
    }

    @Override
    public int getGroupCount() {
        return gData.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return iData.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return gData.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return iData.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolderGroup groupHolder;
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_exlist_group, parent, false);
            groupHolder = new ViewHolderGroup();
            groupHolder.tv_group_name = (TextView) convertView.findViewById(R.id.tv_group_name);
            convertView.setTag(groupHolder);
        }else{
            groupHolder = (ViewHolderGroup) convertView.getTag();
        }
        groupHolder.tv_group_name.setText(gData.get(groupPosition).toString());
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (groupPosition == 0) {
            ViewHolderItem itemHolder;
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_exlist_item, parent, false);
            itemHolder = new ViewHolderItem();
            itemHolder.tv_title = (TextView) convertView.findViewById(R.id.collect_title);
            itemHolder.tv_time = (TextView) convertView.findViewById(R.id.collect_time);
            convertView.setTag(itemHolder);
            itemHolder.tv_title.setText(iData.get(groupPosition).get(childPosition).getTitle());
            itemHolder.tv_time.setText(iData.get(groupPosition).get(childPosition).getTime());
        } else {
            ViewHolderUnsplash itemHolder;
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_exlist_imgitem, parent, false);
            itemHolder = new ViewHolderUnsplash();
            itemHolder.iv_image = (ImageView) convertView.findViewById(R.id.iv_unsplash);
            itemHolder.tv_time = (TextView) convertView.findViewById(R.id.tv_collect_time);
            convertView.setTag(itemHolder);
            //這里應(yīng)該是從緩存里讀取 而不是在線顯示
            Glide.with(mContext)
                    .load(iData.get(groupPosition).get(childPosition).getUrl())
                    .into(itemHolder.iv_image);
            itemHolder.tv_time.setText(iData.get(groupPosition).get(childPosition).getTime());
        }
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private static class ViewHolderGroup{
        private TextView tv_group_name;
    }

    private static class ViewHolderItem{
        private TextView tv_title;
        private TextView tv_time;
    }

    private static class ViewHolderUnsplash{
        private ImageView iv_image;
        private TextView tv_time;
    }
}

adapter完成后,開始寫activity:

CollectActivity中我們對(duì)子項(xiàng)的點(diǎn)擊事件設(shè)置為打開相應(yīng)的activity,
當(dāng)p層從m層獲取到數(shù)據(jù)并返回給v層時(shí),我們把數(shù)據(jù)更新到mCollectAdapter,然后刷新視圖,呈現(xiàn)給用戶。

ui.activity.CollectActivity

public class CollectActivity extends MVPBaseActivity<HistoryCollectView, HistoryCollectPresenterImp1> implements HistoryCollectView {

    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.prograss)
    ProgressBar mPrograss;
    @BindView(R.id.content_listview)
    ExpandableListView mContentListview;

    private ArrayList<String> gData = null;
    private ArrayList<ArrayList<HistoryCollect>> iData = null;
    private ArrayList<HistoryCollect> lData = null;

    CollectAdapter mCollectAdapter;

    @Override
    protected HistoryCollectPresenterImp1 createPresenter() {
        return new HistoryCollectPresenterImp1(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        initData();
        initView();
    }

    private void initData() {
        gData = new ArrayList<String>();
        iData = new ArrayList<ArrayList<HistoryCollect>>();
        gData.add("知乎日?qǐng)?bào)");
        gData.add("圖片精選");
        //知乎日?qǐng)?bào)
        lData = new ArrayList<HistoryCollect>();
        iData.add(lData);
        //圖片精選
        lData = new ArrayList<HistoryCollect>();
        iData.add(lData);
    }

    private void initView() {
        initToolbar();
        mCollectAdapter = new CollectAdapter(gData,iData,this);
        mContentListview.setAdapter(mCollectAdapter);
        mContentListview.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                if (groupPosition ==0) {
                    Intent intent = new Intent(CollectActivity.this, ZhihuDetailActivity.class);
                    intent.putExtra("ZHIHUID",iData.get(groupPosition).get(childPosition).getUrl());
                    intent.putExtra("ZHIHUTITLE",iData.get(groupPosition).get(childPosition).getTitle());
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(CollectActivity.this, UnsplashPhotoActivity.class);
                    intent.putExtra("PHOTOID", iData.get(groupPosition).get(childPosition).getTitle());
                    CircularAnimUtil.startActivity(CollectActivity.this, intent, v,
                            R.color.colorPrimary);
                }
                return true;
            }
        });
        mPresenter.getData(MyDataBaseHelper.COLLECT);
        mPresenter.getData(MyDataBaseHelper.UNSPLASH);

    }

    private void initToolbar() {
        mToolbar.setTitle("收藏");
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_collect;
    }

    @Override
    public void onStartGetData() {
        if (mPrograss != null) {
            mPrograss.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onGetHistorySuccess(List<HistoryCollect> historyCollects) {

    }

    @Override
    public void onGetCollectSuccess(List<HistoryCollect> historyCollects) {
        if (mPrograss != null) {
            mPrograss.setVisibility(View.GONE);
        }
        iData.get(0).clear();
        iData.get(0).addAll(historyCollects);
        mCollectAdapter.notifyDataSetChanged();
    }

    @Override
    public void onGetUnsplashSuccess(List<HistoryCollect> historyCollects) {
        Log.d("dfegeav",historyCollects.get(0).getUrl());
        if (mPrograss != null) {
            mPrograss.setVisibility(View.GONE);
        }
        iData.get(1).clear();
        iData.get(1).addAll(historyCollects);
        mCollectAdapter.notifyDataSetChanged();
    }


    @Override
    public void onGetDataFailed(String error) {
        if (mPrograss != null) {
            mPrograss.setVisibility(View.GONE);
        }
    }
}

4. P層

p層全是老套路,沒(méi)啥寫的,直接上代碼:
presenter.HistoryCollectPresenter

public interface HistoryCollectPresenter {
    void getData(String table);
    void clearHistory();
}

presenter.imp1.HistoryCollectPresenterImp1

public class HistoryCollectPresenterImp1 extends BasePresenter<HistoryCollectView> implements HistoryCollectPresenter,OnHistoryCollectListener {

    private HistoryCollectView mHistoryCollectView;
    private HistoryCollectModelImp1 mHistoryCollectModelImp1;


    public HistoryCollectPresenterImp1(HistoryCollectView historyCollectView) {
        mHistoryCollectView = historyCollectView;
        mHistoryCollectModelImp1 = new HistoryCollectModelImp1();
    }

    @Override
    public void getData(String table) {
        mHistoryCollectView.onStartGetData();
        switch (table) {
            case MyDataBaseHelper.HISTORY:
                mHistoryCollectModelImp1.loadHistory(this);
                break;
            case MyDataBaseHelper.COLLECT:
                mHistoryCollectModelImp1.loadCollect(this);
                break;
            case MyDataBaseHelper.UNSPLASH:
                mHistoryCollectModelImp1.loadUnsplash(this);
        }

    }
    @Override
    public void clearHistory() {
        mHistoryCollectModelImp1.clearHistory();
    }
    @Override
    public void onHistorySuccess(List<HistoryCollect> historyList) {
        mHistoryCollectView.onGetHistorySuccess(historyList);
    }

    @Override
    public void onCollectSuccess(List<HistoryCollect> collectList) {
        mHistoryCollectView.onGetCollectSuccess(collectList);
    }

    @Override
    public void onUnsplashSuccess(List<HistoryCollect> unsplashList) {
        mHistoryCollectView.onGetUnsplashSuccess(unsplashList);
    }

    @Override
    public void onError(String error) {
        mHistoryCollectView.onGetDataFailed(error);
    }
    
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,366評(píng)論 25 708
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語(yǔ)法,類相關(guān)的語(yǔ)法,內(nèi)部類的語(yǔ)法,繼承相關(guān)的語(yǔ)法,異常的語(yǔ)法,線程的語(yǔ)...
    子非魚_t_閱讀 34,896評(píng)論 18 399
  • 上路傳送眼: Android練手小項(xiàng)目(KTReader)基于mvp架構(gòu)(四) GIthub地址: https:/...
    yiuhet閱讀 1,271評(píng)論 3 3
  • 我是學(xué)土木工程專業(yè)的,且做了十幾年的結(jié)構(gòu)設(shè)計(jì),職業(yè)技術(shù)上只算得是馬馬虎虎,不過(guò)通過(guò)這些年的專業(yè)知識(shí)學(xué)習(xí)以及工程經(jīng)驗(yàn)...
    山賊爺閱讀 1,011評(píng)論 2 3
  • 云龍遠(yuǎn)飛駕, 天馬自行空。 焦墨無(wú)聲雨, 長(zhǎng)毫抒胸臆。
    宗林的李閱讀 475評(píng)論 4 2

友情鏈接更多精彩內(nèi)容