MainActivity
public class MainActivity extends AppCompatActivity {
private XRecyclerView xrecyclerview;
private Unbinder mBind;
Myadapter myadapter;
int type = 0;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
initView();
initData();
xrecyclerview.setLayoutManager( new LinearLayoutManager( MainActivity.this,LinearLayout.VERTICAL,false ) );
xrecyclerview.setPullRefreshEnabled( true );
xrecyclerview.setLoadingMoreEnabled( true );
xrecyclerview.setLoadingListener( new XRecyclerView.LoadingListener() {
@Override
public void onRefresh() {
type=0;
initData();
xrecyclerview.refreshComplete();
}
@Override
public void onLoadMore() {
type++;
initData();
xrecyclerview.loadMoreComplete();
}
} );
}
public void initView() {
xrecyclerview = (XRecyclerView) findViewById( R.id.xrecyclerview );
mBind = ButterKnife.bind( this );
myadapter = new Myadapter(getLayoutInflater());
xrecyclerview.setAdapter( myadapter );
}
protected void initData() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl( Connector.Url )
.addConverterFactory( GsonConverterFactory.create() )
.build();
Realize realize = retrofit.create( Realize.class );
Call<User> infoList = realize.getInfoList(type);
infoList.enqueue( new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
User.DataBean data = response.body().getData();
List<User.DataBean.DatasBean> datas = data.getDatas();
myadapter.setList( datas );
}
@Override
public void onFailure(Call<User> call, Throwable t) {
Toast.makeText( MainActivity.this, "请求失败", Toast.LENGTH_SHORT ).show();
}
} );
}
@Override
protected void onDestroy() {
super.onDestroy();
mBind.unbind();
}
}
myadapter
public class Myadapter extends RecyclerView.Adapter<Myadapter.Viewholder> {
private final LayoutInflater layoutInflater;
private List<User.DataBean.DatasBean> mList;
public Myadapter(LayoutInflater layoutInflater) {
this.layoutInflater=layoutInflater;
mList = new ArrayList<>();
}
public void setList(List<User.DataBean.DatasBean> list) {
mList = list;
notifyDataSetChanged();
}
@NonNull
@Override
public Viewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = layoutInflater.inflate( R.layout.activity_user, null );
Viewholder viewholder = new Viewholder( view );
return viewholder;
}
@Override
public void onBindViewHolder(@NonNull Viewholder holder, int position) {
// holder.image.setImageURI( Uri.parse( mList.get( position ).));
holder.text.setText( mList.get( position ).getTitle() );
holder.text1.setText( mList.get( position ).getChapterName() );
}
@Override
public int getItemCount() {
return mList.size();
}
public class Viewholder extends RecyclerView.ViewHolder {
private TextView text;
private TextView text1;
private SimpleDraweeView image;
public Viewholder(View itemView) {
super( itemView );
image = itemView.findViewById( R.id.image );
text = itemView.findViewById( R.id.text );
text1 = itemView.findViewById( R.id.text1 );
}
}
}
接口
public class Connector {
public final static String Url = "http://www.wanandroid.com/article/list/";
}
注解
public interface Realize {
@GET("{type}/json")
Call<User> getInfoList(@Path( "type" )int type);
}