扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
本文实例为大家分享了Android实现多条目加载展示的具体代码,供大家参考,具体内容如下
10年积累的成都网站设计、网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计后付款的网站建设流程,更有桃源免费网站建设让你可以放心的选择与我们合作。
展示效果
这里写图片描述
依赖
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'
添加配置
代码区
ZhuyeActivity(首页布局含TabLayout)
public class ZhuyeActivity extends AppCompatActivity { private ViewPager viewpager; private TabLayout tablayout; private FragmentAdapter adapter; private Listfragments; @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 (); 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"?>
OneFragment(首页以第一个fragment 里面的API是自行供应的接口)
public class OneFragment extends Fragment { private XRecyclerView xr; private Listlist = 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 () { @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"?>
XRAdapter(适配器对条目)
public class XRAdapter extends RecyclerView.Adapter{ List lists; Context mcontext; ArrayList mlist; private enum Item_Type{ Typeone,Typetwo,Typethree,Typefour,Typefive,Typesix,Typeseven; } //创建适配器有参构造 public XRAdapter(Context mcontext, List 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
recycle_item_a(banner轮播xml)
<?xml version="1.0" encoding="utf-8"?>
recycle_item_a(Recyclerview布局xml)
<?xml version="1.0" encoding="utf-8"?>
ViewHolderBa(Recyclerview小适配器)
public class ViewHolderBa extends RecyclerView.Adapter{ List lists; Context mc; public ViewHolderBa(List 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); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流