时间:2021-05-21
本文实例为大家分享了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() { // 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); } }}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例为大家分享了XrecyclerView实现加载数据和切换不同布局,供大家参考,具体内容如下compile'com.jcodecraeer:xrecycl
Android中menu同时显示图标和文字的实现前言:Activity中的menu条目,在设置其showAsAction=”never”时,默认只显示文字tit
本文实例为大家分享了Android实现下拉刷新和上拉加载更多的具体代码,供大家参考,具体内容如下先分享下源码:Android实现下拉刷新和上拉加载更多实现思路:
一、So的热升级尝试在Android代码中,加载so库是通过调用System.loadLibrary函数实现的。但和Android的许多特性一样,只提供了加载,
问题在开发过程难免遇到这样那样的问题。对于有很多条目的数据,我们通常采用table元素来快速实现。如果某一个td的内容太多的话就会出现下面的情况,导致样式混乱难