Android XRecyclerView实现多条目加载

发布时间:2020-09-07 02:50:41 作者:飞鸟96
来源:脚本之家 阅读:158

本文实例为大家分享了Android实现多条目加载展示的具体代码,供大家参考,具体内容如下

展示效果

这里写图片描述

依赖

testCompile 'junit:junit:4.12'
 compile 'com.hjm:BottomTabBar:1.1.1'
 compile 'com.android.support:design:23.4.0'
 compile 'com.android.support:mediarouter-v7:25.0.0'
 compile 'com.android.support:appcompat-v7:25.0.0'
 compile 'com.android.support:recyclerview-v7:25.0.0'
 compile 'com.jcodecraeer:xrecyclerview:1.3.2'
 compile 'com.squareup.okio:okio:1.5.0'
 compile 'com.squareup.okhttp3:okhttp:3.2.0'
 compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
 compile files('libs/gson-2.3.1.jar')
 compile files('libs/universal-image-loader-1.9.3.jar')
 compile 'com.youth.banner:banner:1.4.9'
 compile 'com.github.bumptech.glide:glide:3.7.0'

添加配置

<uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
 <uses-permission android:name="android.permission.READ_PHONE_STATE" />
 <uses-permission android:name="android.permission.CAMERA" />
 <uses-permission android:name="android.permission.VIBRATE" />

代码区

ZhuyeActivity(首页布局含TabLayout)

public class ZhuyeActivity extends AppCompatActivity {
 private ViewPager viewpager;
 private TabLayout tablayout;
 private FragmentAdapter adapter;
 private List<Fragment> fragments;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_zhuye);
  viewpager=(ViewPager)findViewById(R.id.viewpager);
  tablayout=(TabLayout)findViewById(R.id.tablayout);
  fragments=new ArrayList<Fragment>();
  OneFragment oneFragment = new OneFragment();
  TwoFragment twoFragment = new TwoFragment();
  ThreeFragment threeFragment = new ThreeFragment();
  FourFragment fourFragment = new FourFragment();
  fragments.add(oneFragment);
  fragments.add(twoFragment);
  fragments.add(threeFragment);
  fragments.add(fourFragment);
  adapter = new FragmentAdapter(getSupportFragmentManager(),fragments);
  viewpager.setAdapter(adapter);
  tablayout.setupWithViewPager(viewpager);
 }
}

activity_zhuye(首页布局xml文件)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/activity_zhuye"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="test.bwie.com.ykmn.ZhuyeActivity">
 <RelativeLayout
  android:id="@+id/app_top"
  android:layout_width="match_parent"
  android:layout_height="50dp"
  android:background="@color/colorPrimaryDark"
  >
  <TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="首页"
   android:textSize="25dp"
   android:textColor="@color/colorBai"
   android:layout_centerInParent="true"
   />
 </RelativeLayout>
 <android.support.design.widget.TabLayout
  android:layout_width="match_parent"
  android:layout_height="40dp"
  android:id="@+id/tablayout"
  android:layout_below="@id/app_top"
  >
 </android.support.design.widget.TabLayout>

 <android.support.v4.view.ViewPager
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/viewpager"
  android:layout_below="@id/tablayout"
  />
</RelativeLayout>

OneFragment(首页以第一个fragment 里面的API是自行供应的接口)

public class OneFragment extends Fragment {
 private XRecyclerView xr;
 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.y1, null, false);
  //初始化xr控件
  xr=(XRecyclerView)view.findViewById(R.id.xre_xrv);

  //加布局管理器
  LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
  layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
  xr.setLayoutManager(layoutManager);

  getData(API.TYPE_HOME,1);
  xr.setLoadingListener(new XRecyclerView.LoadingListener() {

    /*下拉刷新*/

   @Override
   public void onRefresh() {
    curr=0;
    list.clear();
    getData(API.TYPE_HOME,curr);
    xr.refreshComplete();
   }

    /*上拉加载*/

   @Override
   public void onLoadMore() {
    /*curr++;
    getData(API.TYPE_HOME,curr);
    xr.refreshComplete();*/
//    xr.loadMoreComplete();
   }
  });
  return view;
 }
 private void getData(String url,int curr){
  OkHttp3Utils.getInstance().doGet(url, new GsonObjectCallback<MyGsonFr>() {
   @Override
   public void onUi(MyGsonFr myGsonFr) {
    XRAdapter mxradapter=new XRAdapter(getActivity(),myGsonFr.getTop_stories());
    xr.setAdapter(mxradapter);
   }

   @Override
   public void onFailed(Call call, IOException e) {

   }
  });
 }
}

y1(OneFragment的布局代码xml)

<?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"
 >
 <com.jcodecraeer.xrecyclerview.XRecyclerView
  android:id="@+id/xre_xrv"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  >
 </com.jcodecraeer.xrecyclerview.XRecyclerView>
</RelativeLayout>

XRAdapter(适配器对条目)

public class XRAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
 List<MyGsonFr.TopStoriesBean> lists;
 Context mcontext;
 ArrayList mlist;
 private enum Item_Type{
  Typeone,Typetwo,Typethree,Typefour,Typefive,Typesix,Typeseven;
 }
 //创建适配器有参构造


 public XRAdapter(Context mcontext, List<MyGsonFr.TopStoriesBean> data) {
  this.lists = data;
  this.mcontext = mcontext;
 }

 @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 mView = LayoutInflater.from(mcontext).inflate(R.layout.recycle_item_b, null);
   ViewHolderB viewHolder = new ViewHolderB(mView);
   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<lists.size();i++){
    mlist.add(lists.get(i).getImage());
   }
   //设置图片加载器
   ((ViewHolderA)holder).mbanner.setImageLoader(new GlideImageLoader());
   ((ViewHolderA)holder).mbanner.setImages(mlist);
   ((ViewHolderA)holder).mbanner.start();
  }else if(holder instanceof ViewHolderB){
   mlist=new ArrayList();
   for (int i=0;i<lists.size();i++){
    mlist.add(lists.get(i).getImage());
   }
   ((ViewHolderB)holder).re.setLayoutManager(new LinearLayoutManager(mcontext));
   ((ViewHolderB)holder).re.setAdapter(new ViewHolderBa(lists,mcontext));
  }
 }

 @Override
 public int getItemCount() {
  return 2;
 }

 @Override
 public int getItemViewType(int position) {
  if(position==0){
   return Item_Type.Typeone.ordinal();
  }else if(position==1){
   return Item_Type.Typetwo.ordinal();
  }
  return -1;
 }
 class ViewHolderA extends RecyclerView.ViewHolder{
  private Banner mbanner;
  public ViewHolderA(View itemView) {
   super(itemView);
   mbanner=(Banner)itemView.findViewById(R.id.mybanner);
  }
 }
 class ViewHolderB extends RecyclerView.ViewHolder{
  private RecyclerView re;
  public ViewHolderB(View itemView) {
   super(itemView);
   re = (RecyclerView) itemView.findViewById(R.id.id_recyclerview);
  }
 }
}

recycle_item_a(banner轮播xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="match_parent"
 android:layout_height="150dp">
 <com.youth.banner.Banner
  android:id="@+id/mybanner"
  android:layout_width="match_parent"
  android:layout_height="150dp">
 </com.youth.banner.Banner>
</LinearLayout>

recycle_item_a(Recyclerview布局xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:orientation="vertical"
 android:layout_height="match_parent">
 <android.support.v7.widget.RecyclerView
  android:id="@+id/id_recyclerview"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  />
</LinearLayout>

ViewHolderBa(Recyclerview小适配器)

public class ViewHolderBa extends RecyclerView.Adapter<ViewHolderBa.MyViewHolder>{

 List<MyGsonFr.TopStoriesBean> lists;
 Context mc;

 public ViewHolderBa(List<MyGsonFr.TopStoriesBean> lists, Context mc) {
 this.lists = lists;
 this.mc = mc;
 }

 @Override
 public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
 MyViewHolder holder = new MyViewHolder(LayoutInflater.from(mc).inflate(R.layout.item_home, parent, false));
 return holder;
 }

 @Override
 public void onBindViewHolder(MyViewHolder holder, int position) {
 holder.tv.setText(lists.get(position).getTitle());
 ImageLoader instance = ImageLoader.getInstance();
 instance.displayImage(lists.get(position).getImage(),holder.img);
 }

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

 class MyViewHolder extends RecyclerView.ViewHolder {

 TextView tv;
 ImageView img;

 public MyViewHolder(View view) {
  super(view);
  tv = (TextView) view.findViewById(R.id.text_a1);
  img = (ImageView) view.findViewById(R.id.img_a1);
 }
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

推荐阅读:
  1. android怎么实现条目倒计时功能
  2. Android如何实现下拉展示条目效果

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android xrecyclerview 加载

上一篇:pl/sql连接远程oracle服务器的配置教程

下一篇:详解nodejs微信公众号开发——3.封装消息响应模块

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》