布局:
主布局
<?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"> <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swipeRefreshLayout" android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.v7.widget.RecyclerView> </android.support.v4.widget.SwipeRefreshLayout> </LinearLayout>
heder布局(带banner和gridview)
<?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:layout_height="match_parent" android:orientation="vertical"> <com.youth.banner.Banner android:id="@+id/banner" android:layout_width="match_parent" android:layout_height="wrap_content" app:indicator_drawable_selected="@color/colorAccent" app:indicator_drawable_unselected="@android:color/white" app:indicator_height="4dp" app:indicator_margin="4dp" app:indicator_width="20dp"/> <GridView android:id="@+id/home_gridview" android:layout_width="match_parent" android:layout_height="wrap_content" android:numColumns="6"></GridView> </LinearLayout>
activity里面的重要方法
private void init(){ View header = LayoutInflater.from(this).inflate(R.layout.home_head, null); banner = (Banner) header.findViewById(R.id.banner); gridView=(GridView) header.findViewById(R.id.home_gridview); homeGridviewAdapter=new HomeGridviewAdapter(this); gridView.setAdapter(homeGridviewAdapter); banner.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, BigDataExpoApplication.H / 4)); ArrayList<String> titles = new ArrayList<>(Arrays.asList(new String[]{"first title", "second title", "third title", "fourth title"})); List list =Arrays.asList(new String[]{"http://img.zcool.cn/community/01b72057a7e0790000018c1bf4fce0.png", "http://img.zcool.cn/community/01b72057a7e0790000018c1bf4fce0.png", "http://img.zcool.cn/community/01b72057a7e0790000018c1bf4fce0.png", "http://img.zcool.cn/community/01b72057a7e0790000018c1bf4fce0.png"}); List arrayList = new ArrayList(list); banner.setImages(arrayList) .setBannerAnimation(CubeOutTransformer.class) .setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE) .setBannerTitles(titles) .setImageLoader(new GlideImageLoader()) .start(); mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)); myAdapter = new MyAdapter(this, null); myAdapter.setHeaderView(header); mRecyclerView.setAdapter(myAdapter); mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { mHandler.sendEmptyMessageDelayed(REFRESH_COMPLETE, 2000); } }); banner.setOnBannerListener(new OnBannerListener() { @Override public void OnBannerClick(int position) { showToast("这是第"+position+"个"); } }); }