您的位置:首页 > 编程语言 > Java开发

MVP+RxJava+Retrofit使用GET解析拼参数

2017-12-03 19:27 337 查看
添加依赖

compile 'com.hjm:BottomTabBar:1.1.1'
compile 'com.squareup.retrofit2:retrofit:2.0.1'
compile 'com.squareup.retrofit2:converter-gson:2.0.1'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.1'
compile 'io.reactivex:rxandroid:1.1.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
//fresco
compile 'com.facebook.fresco:fresco:0.14.1'
//banner
compile 'com.youth.banner:banner:1.4.9'
//    glide
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.jakewharton:butterknife:7.0.1'
compile 'com.android.support:recyclerview-v7:26.+'
compile'org.greenrobot:greendao:3.1.0'
compile'org.greenrobot:greendao-generator:3.1.0'


public class Api {
//    http://gank.io/api/data/Android/10/1 public static final String BASE_URL="http://gank.io/api/";
}


public interface ApiService {

@GET("data/Android/{size}/{page}")
Observable<MyBean> getMovieData(@Path("size") String size, @Path("page") String page);
}


public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
//初始化fresco
Fresco.initialize(this);
}
}


public interface IView {
//获取请求好的数据
void getShowData(MyBean myBean);
}


public class MainActivity extends AppCompatActivity {

private BottomTabBar mb;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mb =(BottomTabBar)findViewById(R.id.bottom_tab_bar);

mb.init(getSupportFragmentManager())
.setImgSize(50,50)
.setFontSize(14)
.setTabPadding(4,6,10)
.setChangeColor(Color.RED,Color.DKGRAY)
.addTabItem("首页",R.mipmap.ic_launcher, Fragment01.class)
.addTabItem("想法",R.mipmap.ic_launcher, Fragment02.class)
.addTabItem("市场",R.mipmap.ic_launcher, Fragment03.class)
.addTabItem("通知",R.mipmap.ic_launcher, Fragment04.class)
.addTabItem("更多",R.mipmap.ic_launcher, Fragment05.class)
.isShowDivider(false)
.setOnTabChangeListener(new BottomTabBar.OnTabChangeListener() {
@Override
public void onTabChange(int position, String name) {

}
});
}
}


public interface IModel {
//请求网路数据
void getHttpData(String url, String catalogId, String pnum,OnFinish onFinish);

}


public interface OnFinish {
void OnFinishListener(MyBean myBean);
}


public class Model implements IModel{

@Override
public void getHttpData(String url, String catalogId, String pnum, final OnFinish onFinish) {
Retrofit retrofit = new Retrofit.Builder().baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
//动态代理得到网络接口
ApiService apiService = retrofit.create(ApiService.class);

rx.Observable<MyBean> movieData = apiService.getMovieData(catalogId,pnum);

movieData.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<MyBean>() {
@Override
public void on
e8f8
Completed() {

}

@Override
public void onError(Throwable e) {

}

@Override
public void onNext(MyBean myBean) {
onFinish.OnFinishListener(myBean);
}
});
}
}


public interface IPresenter {
void loadList(String url,String a,String b);
}


public class MyPresenter implements IPresenter {
//定义view和model接口的变量
private final IView iview;
private final IModel iModel;

public MyPresenter(IView iview) {
this.iview = iview;
this.iModel = new Model();
}

@Override
public void loadList(String url, String a, String b) {
iModel.getHttpData(url, a, b, new OnFinish() {
@Override
public void OnFinishListener(MyBean myBean) {
iview.getShowData(myBean);
}
});
}
}


public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
//    @Bind(R.id.name)
//    TextView name;
//    @Bind(R.id.des)
//    TextView des;
//    @Bind(R.id.img)
//    ImageView img;
private Context context;
private List<MyBean.ResultsBean> list;

public MyAdapter(Context context, List<MyBean.ResultsBean> list) {
this.context = context;
this.list = list;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.name.setText(list.get(position).who);
holder.des.setText(list.get(position).desc);
//        Glide.with(context).load(list.get(position).images).into(holder.img);
Uri imgUrl = Uri.parse("http://img.gank.io/fef497ed-83ba-46f6-8a94-0e7b724e1c10");
holder.img.setImageURI(imgUrl);

}

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

public class ViewHolder extends RecyclerView.ViewHolder {
public TextView name,des;
public SimpleDraweeView img;
public ViewHolder(View itemView) {
super(itemView);
name=itemView.findViewById(R.id.name);
des=itemView.findViewById(R.id.des);
img=itemView.findViewById(R.id.img);
}
}
}


public class Fragment01 extends Fragment implements IView{
@Bind(R.id.recycler)
RecyclerView recycler;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedIstanceState) {
View view = inflater.inflate(R.layout.fragment_01, null);

ButterKnife.bind(this, view);
LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getContext());
recycler.setLayoutManager(linearLayoutManager);
MyPresenter myPresenter = new MyPresenter(this);
myPresenter.loadList(Api.BASE_URL,"10","1");
return view;
}

@Override
public void onDestroyView() {
super.onDestroyView();
ButterKnife.unbind(this);
}


@Override
public void getShowData(MyBean myBean) {
MyAdapter myAdapter = new MyAdapter(getContext(), myBean.results);
recycler.setAdapter(myAdapter);
}
}


主页面布局

<com.hjm.bottomtabbar.BottomTabBar
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/bottom_tab_bar"
></com.hjm.bottomtabbar.BottomTabBar>


fragment布局

<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycler"></android.support.v7.widget.RecyclerView>
</LinearLayout>


item

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="0dp"
android:layout_height="150dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/name"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/des"/>
</LinearLayout>
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/img"
android:layout_width="0dp"
android:layout_height="150dp"
android:layout_weight="1"
android:layout_margin="8dp"
fresco:failureImage="@mipmap/ic_launcher" />
</LinearLayout>


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: