MVP+Adapter+ViewHolder抽取
简单的目录结构
简单的MVP设计
1. 首先我配置了MyApplication为了方便调用Context
记住需要在清单中配置name节点 android:name=".application.MyApplication"
public class MyApplication extends Application {
public static Context MainContext;
@Override
public void onCreate() {
super.onCreate();
MainContext = getApplicationContext();
}
}
2.创建Activity 继承FragmentActivity 并且模拟数据
package com.oblivion.mvp_fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import com.oblivion.mvp_fragment.fragment.FirstFragment;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends FragmentActivity {
private List<Integer> mList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
initFragment();
}
/**
* 模拟数据
*/
private void initData() {
mList = new ArrayList<>();
for (int i = 0; i < 100; i++) {
mList.add(i);
}
}
/**
* 初始化Fragment
*/
private void initFragment() {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.fl_main, FirstFragment.newInstance(mList));
transaction.commit();
}
}
3.Adapter 的基类
package com.oblivion.mvp_fragment.adapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import com.oblivion.mvp_fragment.holder.BasicHolder;
import com.oblivion.mvp_fragment.holder.ViewHolder;
import java.util.List;
/**
* Created by oblivion on 2016/11/18.
*/
public abstract class BasicAdapter<T> extends BaseAdapter {
private List<T> mList;
public BasicAdapter(List<T> mList) {
this.mList = mList;
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
BasicHolder holder;
if (convertView == null) {
holder = initHolder();
convertView = holder.holderView;
convertView.setTag(holder);
} else {
holder = (BasicHolder) convertView.getTag();
}
holder.bindData(position);
return convertView;
}
public abstract BasicHolder initHolder();
}
4.Adapter的基类继承
package com.oblivion.mvp_fragment.adapter;
import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.TextView;
import com.oblivion.mvp_fragment.R;
import com.oblivion.mvp_fragment.holder.BasicHolder;
import com.oblivion.mvp_fragment.holder.ViewHolder;
import java.util.List;
/**
* Created by oblivion on 2016/11/18.
*/
public class FirstFragmentAdapter extends BasicAdapter {
protected List<Integer> mLists;
public FirstFragmentAdapter(List<Integer> mLists) {
super(mLists);
this.mLists = mLists;
}
@Override
public BasicHolder initHolder() {
return new ViewHolder(mLists);
}
}
5.Holder的基类
package com.oblivion.mvp_fragment.holder;
import android.view.View;
import android.widget.TextView;
import com.oblivion.mvp_fragment.R;
import com.oblivion.mvp_fragment.application.MyApplication;
import java.util.List;
/**
* Created by oblivion on 2016/11/18.
*/
public abstract class BasicHolder<T> {
public View holderView;
public List<T> mLists;
public BasicHolder(List<T> mLists) {
this.mLists = mLists;
holderView = getInflateView();
}
public abstract View getInflateView();
public abstract void bindData(int position);
}
6.Holder基类的实现 ViewHolder
package com.oblivion.mvp_fragment.holder;
import android.view.View;
import android.widget.TextView;
import com.oblivion.mvp_fragment.R;
import com.oblivion.mvp_fragment.application.MyApplication;
import java.util.List;
/**
* Created by oblivion on 2016/11/18.
*/
public class ViewHolder extends BasicHolder<Integer> {
public TextView tv;
public ViewHolder(List<Integer> mLists) {
super(mLists);
}
@Override
public View getInflateView() {
View view = View.inflate(MyApplication.MainContext, R.layout.item_firstfragment, null);
tv = (TextView) view.findViewById(R.id.tv_item);
return view;
}
@Override
public void bindData(int position) {
tv.setText(mLists.get(position) + "");
}
}
7.FirstFragment
package com.oblivion.mvp_fragment.fragment;
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.ListView;
import com.oblivion.mvp_fragment.R;
import com.oblivion.mvp_fragment.adapter.FirstFragmentAdapter;
import com.oblivion.mvp_fragment.presenter.Presenter;
import com.oblivion.mvp_fragment.view.IShowView;
import java.util.List;
/**
* Created by oblivion on 2016/11/18.
*/
public class FirstFragment extends Fragment implements IShowView {
private static List<Integer> mLists;
private Presenter presenter;
private ListView listView;
public static Fragment newInstance(List<Integer> mList) {
mLists = mList;
return new FirstFragment();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_first, null);
listView = (ListView) view.findViewById(R.id.lv_first);
presenter = new Presenter(this);
presenter.doLogic();
return view;
}
@Override
public void showNext() {
}
@Override
public void showSuccess(int data) {
//数据加载成功的话
mLists.add(data);
listView.setAdapter(new FirstFragmentAdapter(mLists));
}
@Override
public void showError() {
}
}
8.网络连接接口Model
package com.oblivion.mvp_fragment.model;
/**
* Created by oblivion on 2016/11/18.
*/
public interface IModelGetData {
String getData();
}
9.Model的接口实现
package com.oblivion.mvp_fragment.model.impl;
import android.os.SystemClock;
import com.oblivion.mvp_fragment.model.IModelGetData;
/**
* Created by oblivion on 2016/11/18.
*/
public class IModelGetDataImpl implements IModelGetData {
@Override
public String getData() {
SystemClock.sleep(2000);
return "撩妹中,勿打扰...";
}
}
10.View的接口
package com.oblivion.mvp_fragment.view;
/**
* 控制视图
* Created by oblivion on 2016/11/18.
*/
public interface IShowView {
void showNext();
void showSuccess(int data);
void showError();
}
11.逻辑实现presenter
package com.oblivion.mvp_fragment.presenter;
import com.oblivion.mvp_fragment.model.IModelGetData;
import com.oblivion.mvp_fragment.model.impl.IModelGetDataImpl;
import com.oblivion.mvp_fragment.view.IShowView;
/**
* Created by oblivion on 2016/11/18.
*/
public class Presenter {
private IModelGetData modelGetData;
private IShowView showView;
public Presenter(IShowView showView) {
this.showView = showView;
modelGetData = new IModelGetDataImpl();
}
public void doLogic() {
String data = modelGetData.getData();
if (data != null && data.length() > 0) {
showView.showSuccess(1000);
}
}
}