依赖 compile 'com.jcodecraeer:xrecyclerview:1.3.2'
xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.jcodecraeer.xrecyclerview.XRecyclerView
android:id="@+id/xre_xrv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.jcodecraeer.xrecyclerview.XRecyclerView>
</LinearLayout>
MainActivity中
public class FragmentOne extends Fragment{
String path;
private XRecyclerView xr;
private int page=0;
private List<String> list=new ArrayList<>();
private int curr;
private XRAdapter adapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_one,null );
xr=(XRecyclerView) view.findViewById(R.id.xre_xrv);
//加布局管理器
LinearLayoutManager layoutManager=new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
xr.setLayoutManager(layoutManager);
xr.setLoadingListener(new XRecyclerView.LoadingListener() {
@Override
public void onRefresh() {//下拉刷新
curr=0;
list.clear(); getData(path);
xr.refreshComplete();
} @Override
public void onLoadMore() {
//上拉加载
//curr++;//实行分页
page=page+5;
getData(path);
xr.refreshComplete();
}
});
getData(path);
return view;
}
private void getData(String url)
{
path = "http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.billboard.billList&type=1&size=10&offset="+page;
OkHttp3Utils.getInstance().doGet(path, new GsonObjectCallback<Super>() {
@Override
public void onUi(Super aSuper) {
List<Super.SongListBean> song_list = aSuper.getSong_list();
XRAdapter adapter=new XRAdapter(aSuper.getSong_list(),getActivity());
xr.setAdapter(adapter);
}
@Override
public void onFailed(Call call, IOException e) {
}
});
}}
XRAdapter
public class XRAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
List<Super.SongListBean> song_list;
Context mcontext;
ArrayList mlist;
public XRAdapter(List<Super.SongListBean> song_list, Context mcontext) {
this.song_list = song_list;
this.mcontext = mcontext;
}
private enum Item_Type{
Typeone,Typetwo
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == Item_Type.Typeone.ordinal()) {
View mView = LayoutInflater.from(mcontext).inflate(R.layout.recycle_item_a, null);
ViewHolderA viewHolder = new ViewHolderA(mView);
return viewHolder;
} else if (viewType == Item_Type.Typetwo.ordinal()) {
View mView1 = LayoutInflater.from(mcontext).inflate(R.layout.item_home, null);
ViewHolderB viewHolder = new ViewHolderB(mView1);
return viewHolder;
}
return null;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof ViewHolderA)
{
mlist=new ArrayList();
for (int i=0;i<song_list.size();i++)
{
mlist.add(song_list.get(position).getPic_big());
}
//设置图片加载器
((ViewHolderA) holder).mbanner.setImageLoader(new GlideImageLoader());
((ViewHolderA) holder).mbanner.setImages(mlist);
((ViewHolderA) holder).mbanner.start();
}else if(holder instanceof ViewHolderB)
{
ViewHolderB holderB= (ViewHolderB) holder;
/*ListAdapter adapter=new ListAdapter(song_list,mcontext);
holderB.listView.setAdapter(adapter);*/
holderB.te.setText(song_list.get(position).getTitle());
ImageLoader.getInstance().displayImage(song_list.get(position).getPic_small(),holderB.im);
/* ViewHolderB holderB= (ViewHolderB) holder;
holderB.recyclerView.setLayoutManager(new LinearLayoutManager(mcontext));
Home_TwoAdapter adapter = new Home_TwoAdapter(mcontext, song_list);
holderB.recyclerView.setAdapter(adapter);*/
}
}
@Override
public int getItemViewType(int position) {
if (position==0)
{
return Item_Type.Typeone.ordinal();
}else{
return Item_Type.Typetwo.ordinal();
}
}
@Override
public int getItemCount() {
return song_list.size();//传对象需要得到
}
class ViewHolderA extends RecyclerView.ViewHolder {
public Banner mbanner;
public ViewHolderA(View itemView) {
super(itemView);
mbanner = (Banner) itemView.findViewById(R.id.mybanner);
/* mbanner.setOnBannerListener(new OnBannerListener() {
@Override
public void OnBannerClick(int position) {
Intent intent = new Intent(mcontext, DianjiActivity.class);
mcontext.startActivity(intent);
}
});*/
}
}
class ViewHolderB extends RecyclerView.ViewHolder {
//RecyclerView recyclerView;
//ListView listView;
TextView te;
ImageView im;
public ViewHolderB(View itemView) {
super(itemView);
//recyclerView=(RecyclerView) itemView.findViewById(R.id.id_recyclerview);
// listView=(ListView) itemView.findViewById(R.id.listview);
te=(TextView) itemView.findViewById(R.id.it_text);
im=(ImageView) itemView.findViewById(R.id.it_img);
}
}
}
item_home中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/it_img"
android:layout_width="200dp"
android:layout_height="200dp" />
<TextView
android:id="@+id/it_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>