Android实现xrecyclerview+MVP+okhttp一个小案例
2017-11-12 20:27
375 查看
MainActivity代码
适配器代码
package lanbo.myweektest.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import lanbo.myweektest.R;
import lanbo.myweektest.bean.NetDataBean;
/**
* Created by asus on 2017/11/9.
*/
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> implements View.OnClickListener {
Context context;
NetDataBean bean;
public MyAdapter(Context context, NetDataBean bean) {
this.context = context;
this.bean = bean;
}
@Override
public void onClick(View v) {
if (mOnItemClickListener != null) {
//注意这里使用getTag方法获取position
mOnItemClickListener.onItemClick(v,(int)v.getTag());
}
}
//模拟接口
public static interface OnItemClickListener {
void onItemClick(View view , int position);
}
private OnItemClickListener mOnItemClickListener = null;
@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=View.inflate(context,R.layout.item_layout,null);
MyHolder holder=new MyHolder(view);
view.setOnClickListener(this);
return holder;
}
@Override
public void onBindViewHolder(MyHolder holder, int position) {
holder.singlename.setText(bean.getSong_list().get(position).getAuthor());
holder.songname.setText(bean.getSong_list().get(position).getTitle());
Glide.with(context)
.load(bean.getSong_list().get(position).getPic_small())
.into(holder.iv);
holder.itemView.setTag(position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.mOnItemClickListener = listener;
}
@Override
public int getItemCount() {
return bean.getSong_list()==null?0:bean.getSong_list().size();
}
class MyHolder extends RecyclerView.ViewHolder{
ImageView iv;
TextView singlename;
TextView songname;
public MyHolder(View itemView) {
super(itemView);
iv=itemView.findViewById(R.id.xrv_item_iv);
singlename=itemView.findViewById(R.id.xrv_item_singlename);
songname=itemView.findViewById(R.id.xrv_item_songname);
}
}
}
Model代码
package lanbo.myweektest.model;
import okhttp3.Callback;
/**
* Created by asus on 2017/11/9.
*/
public interface IXRVDataModel {
public void getnetmusicdata(String num, String offset, Callback callback);
}
presenter代码
package lanbo.myweektest.presenter;
import android.content.Context;
import android.util.Log;
import com.google.gson.Gson;
import java.io.IOException;
import lanbo.myweektest.bean.NetDataBean;
import lanbo.myweektest.model.MyXRVDataModel;
import lanbo.myweektest.utils.OnUiCallback;
import lanbo.myweektest.view.IXrecyclerview;
import okhttp3.Call;
/**
* Created by asus on 2017/11/9.
*/
public class MyXRVpresenter {
IXrecyclerview view;
Context context;
MyXRVDataModel model;
public MyXRVpresenter(IXrecyclerview view, Context context) {
this.view = view;
this.context = context;
model=new MyXRVDataModel();
}
public void StartData(String num,String offset){
model.getnetmusicdata(num, offset, new OnUiCallback() {
@Override
public void onFailed(Call call, IOException e) {
}
@Override
public void onSuccess(String result) {
Log.i("**************", "ShowXrv: "+result.toString());
Gson gson=new Gson();
NetDataBean bean = gson.fromJson(result, NetDataBean.class);
view.ShowXrv(bean);
}
});
}
}
view代码
mainactivity布局代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="lanbo.myweektest.activity.MainActivity">
<com.jcodecraeer.xrecyclerview.XRecyclerView
android:id="@+id/xrv"
android:layout_width="match_parent"
android:layout_height="match_parent"></com.jcodecraeer.xrecyclerview.XRecyclerView>
</RelativeLayout>
hand_layout布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:layout_width="300dp"
android:layout_height="100dp"
androi
4000
d:src="@mipmap/ic_launcher"
/>
</RelativeLayout>
item布局
package lanbo.myweektest.activity; import android.os.Bundle; import android.os.Handler; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.view.View; import android.widget.Toast; import com.jcodecraeer.xrecyclerview.XRecyclerView; import lanbo.myweektest.R; import lanbo.myweektest.adapter.MyAdapter; import lanbo.myweektest.bean.NetDataBean; import lanbo.myweektest.presenter.MyXRVpresenter; import lanbo.myweektest.view.IXrecyclerview; public class MainActivity extends AppCompatActivity implements IXrecyclerview { MyXRVpresenter presenter=new MyXRVpresenter(this,this); XRecyclerView xrv; int num=30; int offset=1; Handler handler=new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); xrv= (XRecyclerView) findViewById(R.id.xrv); presenter.StartData("30","1"); View view=View.inflate(MainActivity.this,R.layout.head_layout,null); xrv.addHeaderView(view); } @Override public void ShowXrv(final NetDataBean bean) { LinearLayoutManager manager=new LinearLayoutManager(MainActivity.this); xrv.setLayoutManager(manager); final MyAdapter adapter=new MyAdapter(MainActivity.this,bean); xrv.setAdapter(adapter); xrv.setLoadingListener(new XRecyclerView.LoadingListener() { @Override public void onRefresh() { handler.postDelayed(new Runnable() { @Override public void run() { offset++; presenter.StartData(num+"",offset+""); adapter.notifyDataSetChanged(); xrv.refreshComplete(); } },2000); } @Override public void onLoadMore() { handler.postDelayed(new Runnable() { @Override public void run() { num+=3; presenter.StartData(num+"",offset+""); adapter.notifyDataSetChanged(); xrv.loadMoreComplete(); } },2000); } }); adapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() { @Override public void onItemClick(View view, int position) { Toast.makeText(MainActivity.this,bean.getSong_list().get(position).getAuthor(),Toast.LENGTH_SHORT).show(); } }); } }
适配器代码
package lanbo.myweektest.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import lanbo.myweektest.R;
import lanbo.myweektest.bean.NetDataBean;
/**
* Created by asus on 2017/11/9.
*/
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> implements View.OnClickListener {
Context context;
NetDataBean bean;
public MyAdapter(Context context, NetDataBean bean) {
this.context = context;
this.bean = bean;
}
@Override
public void onClick(View v) {
if (mOnItemClickListener != null) {
//注意这里使用getTag方法获取position
mOnItemClickListener.onItemClick(v,(int)v.getTag());
}
}
//模拟接口
public static interface OnItemClickListener {
void onItemClick(View view , int position);
}
private OnItemClickListener mOnItemClickListener = null;
@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=View.inflate(context,R.layout.item_layout,null);
MyHolder holder=new MyHolder(view);
view.setOnClickListener(this);
return holder;
}
@Override
public void onBindViewHolder(MyHolder holder, int position) {
holder.singlename.setText(bean.getSong_list().get(position).getAuthor());
holder.songname.setText(bean.getSong_list().get(position).getTitle());
Glide.with(context)
.load(bean.getSong_list().get(position).getPic_small())
.into(holder.iv);
holder.itemView.setTag(position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.mOnItemClickListener = listener;
}
@Override
public int getItemCount() {
return bean.getSong_list()==null?0:bean.getSong_list().size();
}
class MyHolder extends RecyclerView.ViewHolder{
ImageView iv;
TextView singlename;
TextView songname;
public MyHolder(View itemView) {
super(itemView);
iv=itemView.findViewById(R.id.xrv_item_iv);
singlename=itemView.findViewById(R.id.xrv_item_singlename);
songname=itemView.findViewById(R.id.xrv_item_songname);
}
}
}
Model代码
package lanbo.myweektest.model; import lanbo.myweektest.utils.OkHttp3Utils; import okhttp3.Callback; /** * Created by asus on 2017/11/9. */ public class MyXRVDataModel implements IXRVDataModel{ @Override public void getnetmusicdata(String num, String offset, Callback callback) { OkHttp3Utils.doGet("http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.billboard.billList&type=1&size="+num+"&offset="+offset,callback); } }Model接口代码
package lanbo.myweektest.model;
import okhttp3.Callback;
/**
* Created by asus on 2017/11/9.
*/
public interface IXRVDataModel {
public void getnetmusicdata(String num, String offset, Callback callback);
}
presenter代码
package lanbo.myweektest.presenter;
import android.content.Context;
import android.util.Log;
import com.google.gson.Gson;
import java.io.IOException;
import lanbo.myweektest.bean.NetDataBean;
import lanbo.myweektest.model.MyXRVDataModel;
import lanbo.myweektest.utils.OnUiCallback;
import lanbo.myweektest.view.IXrecyclerview;
import okhttp3.Call;
/**
* Created by asus on 2017/11/9.
*/
public class MyXRVpresenter {
IXrecyclerview view;
Context context;
MyXRVDataModel model;
public MyXRVpresenter(IXrecyclerview view, Context context) {
this.view = view;
this.context = context;
model=new MyXRVDataModel();
}
public void StartData(String num,String offset){
model.getnetmusicdata(num, offset, new OnUiCallback() {
@Override
public void onFailed(Call call, IOException e) {
}
@Override
public void onSuccess(String result) {
Log.i("**************", "ShowXrv: "+result.toString());
Gson gson=new Gson();
NetDataBean bean = gson.fromJson(result, NetDataBean.class);
view.ShowXrv(bean);
}
});
}
}
view代码
package lanbo.myweektest.view; import lanbo.myweektest.bean.NetDataBean; /** * Created by asus on 2017/11/9. */ public interface IXrecyclerview { public void ShowXrv(NetDataBean bean); }
mainactivity布局代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="lanbo.myweektest.activity.MainActivity">
<com.jcodecraeer.xrecyclerview.XRecyclerView
android:id="@+id/xrv"
android:layout_width="match_parent"
android:layout_height="match_parent"></com.jcodecraeer.xrecyclerview.XRecyclerView>
</RelativeLayout>
hand_layout布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:layout_width="300dp"
android:layout_height="100dp"
androi
4000
d:src="@mipmap/ic_launcher"
/>
</RelativeLayout>
item布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="50dp"> <ImageView android:id="@+id/xrv_item_iv" android:layout_width="50dp" android:layout_height="50dp" android:src="@mipmap/ic_launcher_round" /> <LinearLayout android:layout_toRightOf="@+id/xrv_item_iv" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/xrv_item_songname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="adsadsadas" /> <TextView android:id="@+id/xrv_item_singlename" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="adsadsadas" /> </LinearLayout> </RelativeLayout>
相关文章推荐
- 【Android 进阶】ButterKnife+Retrofit+Picasso+RecycleView 实现一个小案例
- Android实战技巧:用TextView实现Rich Text---在同一个TextView中设置不同的字体风格
- Android UI 之居中绘制文本内容的正确方法——实现自定义一个TextView
- Android setContentView 实现同一个activity下不同view的切换
- Android如何在一个TextView中实现多种文本风格?
- Android 一个按钮两次单击触发两个不同事件并实现两个View在同一界面切换
- Android实战技巧:用TextView实现Rich Text---在同一个TextView中设置不同的字体风格
- Android实战技巧:用TextView实现Rich Text---在同一个TextView中设置不同的字体风格
- android如果重写onDraw实现一个类似TextView可以显示表情和链接的控件(二)
- 在Android中动画移动一个View的位置,采用Scroller类实现Android动画之 View移动
- 【Android UI】案例03滑动切换效果的实现(ViewPager)
- Android实战技巧:用TextView实现Rich Text---在同一个TextView中设置不同的字体风格
- Android实战技巧:用TextView实现Rich Text---在同一个TextView中设置不同的字体风格
- Android如何在一个TextView中实现多种文本风格?
- Android实战技巧:用TextView实现Rich Text---在同一个TextView中设置不同的字体风格
- 将翻页效果做成一个View之在android中的View中实现装饰模式。
- Android自定义控件继承ViewGrop实现一个GridView的显示效果
- Android实战技巧:用TextView实现Rich Text---在同一个TextView中设置不同的字体风格
- Android setContentView 实现同一个activity下不同view的切换
- Android setContentView 实现同一个activity下不同view的切换