本文實例為大家分享了Android實現(xiàn)多條目加載展示的具體代碼,供大家參考,具體內(nèi)容如下
10年積累的成都網(wǎng)站設(shè)計、網(wǎng)站建設(shè)經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先網(wǎng)站設(shè)計后付款的網(wǎng)站建設(shè)流程,更有桃源免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
展示效果
這里寫圖片描述
依賴
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'
添加配置
代碼區(qū)
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是自行供應(yīng)的接口)
public class OneFragment extends Fragment { private XRecyclerView xr; private Listlist = new ArrayList<>(); //獲取數(shù)據(jù)的開始 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; } //創(chuàng)建適配器有參構(gòu)造 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); } } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。