banner實現無限輪播+下拉重新整理上拉載入+ 下拉時 listview 和輪播圖一起重新整理
阿新 • • 發佈:2018-12-22
//首先把所需要的依賴包匯入 gson jar包、imageLoader jar包、banner1.4.9jar包、design jar包,,,然後匯入library,新建專案,把library匯入專案中
//新增許可權
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
android:name=".MyApplication"drawable包下 rb_selector佈局
<?xml version="1.0" encoding=layout包下 activity_main佈局"utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:drawable="@android:color/holo_red_dark"></item> <item android:drawable="@android:color/holo_blue_dark"></item> </selector>
<?layout包下 discover_layout佈局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:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.samsung.sunfuliang10241.MainActivity"> <RadioGroupandroid:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:id="@+id/rel_navigate" android:layout_alignParentBottom="true"> <RadioButton android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="首頁" android:button="@null" android:gravity="center" android:id="@+id/rb_index" android:padding="3dp" android:background="@drawable/rb_selector" android:checked="true"/> <RadioButton android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="發現" android:button="@null" android:padding="3dp" android:gravity="center" android:id="@+id/rb_discovery" android:background="@drawable/rb_selector"/> <RadioButton android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="下載" android:button="@null" android:gravity="center" android:padding="3dp" android:id="@+id/rb_down" android:background="@drawable/rb_selector"/> <RadioButton android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="我的" android:padding="3dp" android:button="@null" android:gravity="center" android:id="@+id/rb_me" android:background="@drawable/rb_selector"/> </RadioGroup> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/rel_navigate" android:id="@+id/main_content"></FrameLayout> </RelativeLayout>
<?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="match_parent" android:gravity="center"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tv" android:text="這是發現頁面" android:textColor="#f00" /> </LinearLayout>layout包下 down_layout佈局
<?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="match_parent" android:gravity="center" > <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tv" android:text="這是下載頁面" android:textColor="#f00" /> </LinearLayout>layout包下 index_layout佈局
<?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:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <android.support.design.widget.TabLayout android:layout_width="match_parent" android:layout_height="40dp" app:tabGravity="center" app:tabIndicatorColor="@color/colorAccent" app:tabMode="scrollable" app:tabSelectedTextColor="@color/colorPrimaryDark" app:tabTextColor="@color/colorPrimary" android:id="@+id/myTab" ></android.support.design.widget.TabLayout> <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/vp" ></android.support.v4.view.ViewPager> </LinearLayout>layout包下 item佈局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="60dp" android:layout_height="60dp" android:id="@+id/img"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv_title"/> </LinearLayout>layout包下 me_layout佈局
<?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="match_parent" android:gravity="center" > <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tv" android:text="這是我頁面" android:textColor="#f00" /> </LinearLayout>layout包下 recommend_layout佈局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ptr="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.handmark.pulltorefresh.library.PullToRefreshScrollView android:id="@+id/refresh_scroll_view" android:layout_width="match_parent" android:layout_height="match_parent" ptr:ptrAnimationStyle="flip" ptr:ptrDrawable="@drawable/default_ptr_flip" ptr:ptrHeaderBackground="#383838" ptr:ptrHeaderTextColor="#FFFFFF"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.youth.banner.Banner android:id="@+id/myBanner" android:layout_width="match_parent" android:layout_height="200dp"></com.youth.banner.Banner> <views.MyListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/lv"></views.MyListView> </LinearLayout> </com.handmark.pulltorefresh.library.PullToRefreshScrollView> </LinearLayout>layout包下 test 佈局
<?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="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.youth.banner.Banner android:layout_width="match_parent" android:layout_height="200dp" android:id="@+id/myBanner"></com.youth.banner.Banner> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/lv"></ListView> </LinearLayout> </LinearLayout>主包下MainActivity 類
public class MainActivity extends AppCompatActivity { private RadioGroup radioGroup; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); radioGroup = (RadioGroup) findViewById(R.id.rel_navigate); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { if(checkedId==R.id.rb_index){ addFragment(new IndexFragment()); }else if(checkedId==R.id.rb_discovery){ addFragment(new DiscoveryFragment()); }else if(checkedId==R.id.rb_down){ addFragment(new DownFragment()); }else if(checkedId==R.id.rb_me){ addFragment(new MeFragment()); } } }); //預設新增"首頁" addFragment(new IndexFragment()); } /** * 新增fragment到主頁面中 * @param fragment */ public void addFragment(Fragment fragment){ getSupportFragmentManager().beginTransaction().replace(R.id.main_content,fragment).commit(); } }主包下 MyApplication 類
public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); //初使化 ImageloaderUtil.initConfig(this); } }adapter包下 MyAdapter 類
public class MyAdapter extends BaseAdapter { private List<Result.DataEntity> datas; private Context context; public MyAdapter(List<Result.DataEntity> datas, Context context) { this.datas = datas; this.context = context; } @Override public int getCount() { return datas.size(); } @Override public Object getItem(int position) { return datas.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder ; if(convertView==null){ convertView=View.inflate(context, R.layout.item,null); holder=new ViewHolder(); holder.tvTitle= convertView.findViewById(R.id.tv_title); holder.img=convertView.findViewById(R.id.img); convertView.setTag(holder); }else{ holder=(ViewHolder) convertView.getTag(); } holder.tvTitle.setText(datas.get(position).getTITLE()); if(datas.get(position).getIMAGEURL()==null || datas.get(position).getIMAGEURL().equals("")){ holder.img.setImageResource(R.mipmap.ic_launcher); }else{ //imageLoader載入圖片 ImageLoader.getInstance().displayImage(datas.get(position).getIMAGEURL(),holder.img, ImageloaderUtil.getImageOptions()); } return convertView; } class ViewHolder{ TextView tvTitle; ImageView img; } }adapter包下 MyPagerAdapter 類
public class MyPagerAdapter extends FragmentPagerAdapter { //標題 private List<String> titles; public MyPagerAdapter(FragmentManager fm, List<String> titles) { super(fm); this.titles = titles; } //返回標題 @Override public CharSequence getPageTitle(int position) { return titles.get(position); } @Override public Fragment getItem(int position) { //暫時返回“推薦頁面” return new RecommendFragment(); } @Override public int getCount() { return titles.size(); } }Bean包下 Result 類public class Result { /** * result : success * dataScroll : [{"FROMNAME":"九三學社中央宣傳部","SUBTITLE":null,"SHOWTIME":"2017-10-15","ID":"30310406","IMAGEURL":"/11002/upload/webcms/content/image/2017/10/15/14_56_29_117_12420_01.jpg","RN":1,"TITLE":"九三學社中央科學座談會聚焦文物考古與絲綢之路"},{"FROMNAME":"九三學社湖北省委","SUBTITLE":null,"SHOWTIME":"2017-10-09","ID":"30f6c95527b447528511d105333fbf56","IMAGEURL":"/11002/upload/webcms/content/image/2017/10/09/14_11_02_821_12967_01.jpg","RN":2,"TITLE":"邵鴻率隊赴十堰調研"},{"FROMNAME":"九三學社寧夏區委","SUBTITLE":null,"SHOWTIME":"2017-09-27","ID":"30081029","IMAGEURL":"/11002/upload/webcms/content/image/2017/09/27/08_57_48_840_53302_01.jpg","RN":3,"TITLE":"九三學社寧夏區委文化傳承報告會在銀召開 叢斌做報告"},{"FROMNAME":"團結報","SUBTITLE":"\u2014\u2014記九三學社中央助力\u201c海科會\u201d舉辦","SHOWTIME":"2017-09-26","ID":"29753347","IMAGEURL":"/11002/upload/webcms/content/image/2017/09/20/09_11_17_163_37240_01111.jpg","RN":4,"TITLE":"主打\u201c科技牌\u201d 彰顯\u201c科技範兒\u201d"},{"FROMNAME":"九三學社自貢市委","SUBTITLE":null,"SHOWTIME":"2017-09-21","ID":"6d356ad135454b9db3c2b333dea8c381","IMAGEURL":"/11002/upload/webcms/content/image/2017/09/21/11_12_58_489_41459_20170914093742676.jpg","RN":5,"TITLE":"韓啟德:始終保持綠色發展定力 譜寫\u201c中國夢\u201d自貢篇章"}] * data : [{"FROMNAME":"九三學社中央宣傳部","SUBTITLE":"Get the Flash Player to see this player.","SHOWTIME":"2017-10-19","ID":"30310476","IMAGEURL":null,"RN":1,"TITLE":"九三學社中央致信祝賀中共十九大勝利召開"},{"FROMNAME":"九三學社中央宣傳部","SUBTITLE":"10月18日,舉世矚目的中國共產黨第十九次全國代表大會在北京隆重開幕。 九三學社","SHOWTIME":"2017-10-18","ID":"30310443","IMAGEURL":"http://www.93.gov.cn/11002/upload/webcms/content/image/2017/10/18/20_41_29_761_28057_hanqide.jpg","RN":2,"TITLE":"九三學社中央領導列席中共十九大開幕會"},{"FROMNAME":"九三學社中央宣傳部","SUBTITLE":"中共十九大隆重開幕了。這是在全面建成小康社會關鍵階段、中國特色社會主義進入新時代","SHOWTIME":"2017-10-19","ID":"30310450","IMAGEURL":null,"RN":3,"TITLE":"韓啟德:學習中共十九大精神 建設新時代中國特色社會主義參政黨"},{"FROMNAME":"新華網","SUBTITLE":"新華社合肥10月23日電(記者 徐海濤)記者從中國科學技術大學獲悉,九三學社中央","SHOWTIME":"2017-10-24","ID":"30441488","IMAGEURL":null,"RN":4,"TITLE":"我國科學家在一維量子液體研究領域獲重要進展"},{"FROMNAME":"九三學社上海市委","SUBTITLE":"10月23日上午,九三學社中央副主席、上海市政協副主席、九三學社上海市委主委趙雯","SHOWTIME":"2017-10-23","ID":"30441487","IMAGEURL":"http://www.93.gov.cn/11002/upload/webcms/content/image/2017/10/23/16_38_56_567_80995_11.jpg"