Banner
依赖
implementation 'com.youth.banner:banner:1.4.10'
代码
public class MainActivity extends AppCompatActivity {
Banner ban;
ArrayList< String> title= new ArrayList < > ( ) ;
ArrayList< String> img= new ArrayList < > ( ) ;
@Override
protected void onCreate ( Bundle savedInstanceState) {
super . onCreate ( savedInstanceState) ;
setContentView ( R . layout. activity_main) ;
ban= findViewById ( R . id. ban) ;
OkGo. < String> get ( "http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=200&page=1" ) . execute ( new StringCallback ( ) {
@Override
public void onSuccess ( Response< String> response) {
String body = response. body ( ) ;
try {
JSONObject jsonObject = new JSONObject ( body) ;
JSONArray data = jsonObject. getJSONArray ( "data" ) ;
for ( int i= 0 ; i< data. length ( ) ; i++ ) {
JSONObject jsonObject1 = data. getJSONObject ( i) ;
String pic = jsonObject1. getString ( "pic" ) ;
String tit = jsonObject1. getString ( "title" ) ;
title. add ( tit) ;
img. add ( pic) ;
ban. setBannerTitles ( title) ;
ban. setImages ( img) ;
ban. start ( ) ;
}
} catch ( JSONException e) {
e. printStackTrace ( ) ;
}
}
} ) ;
ban. setImageLoader ( new ImageLoaderInterface ( ) {
@Override
public void displayImage ( Context context, Object path, View imageView) {
String path1 = ( String) path;
Glide. with ( MainActivity. this ) . load ( path1) . into ( ( ImageView) imageView) ;
}
@Override
public View createImageView ( Context context) {
return new ImageView ( context) ;
}
} ) ;
ban. setBannerStyle ( BannerConfig. NUM_INDICATOR_TITLE ) ;
ban. setDelayTime ( 1000 ) ;
}
}
Pulltorefresh
代码
要记得先导入library的Module
public class Main2Activity extends AppCompatActivity {
PullToRefreshListView pullToRefreshListView;
MyAdapter myAdapter;
Banner ban;
ArrayList< String> list= new ArrayList < > ( ) ;
ArrayList< String> title= new ArrayList < > ( ) ;
ArrayList< String> img= new ArrayList < > ( ) ;
int page= 1 ;
@Override
protected void onCreate ( Bundle savedInstanceState) {
super . onCreate ( savedInstanceState) ;
setContentView ( R . layout. activity_main2) ;
pullToRefreshListView= findViewById ( R . id. pu) ;
myAdapter= new MyAdapter ( list, this ) ;
pullToRefreshListView. setAdapter ( myAdapter) ;
pullToRefreshListView. setMode ( PullToRefreshBase. Mode. BOTH ) ;
init ( ) ;
pullToRefreshListView. setOnRefreshListener ( new PullToRefreshBase. OnRefreshListener2 < ListView> ( ) {
@Override
public void onPullDownToRefresh ( PullToRefreshBase< ListView> refreshView) {
list. clear ( ) ;
page= 1 ;
init ( ) ;
}
@Override
public void onPullUpToRefresh ( PullToRefreshBase< ListView> refreshView) {
page++ ;
init ( ) ;
}
} ) ;
View head_inflate = LayoutInflater. from ( this ) . inflate ( R . layout. layout_head, null ) ;
ban= head_inflate. findViewById ( R . id. ban) ;
bannnn ( ) ;
View food_inflate = LayoutInflater. from ( this ) . inflate ( R . layout. layout_food, null ) ;
pullToRefreshListView. getRefreshableView ( ) . addHeaderView ( head_inflate) ;
pullToRefreshListView. getRefreshableView ( ) . addFooterView ( food_inflate) ;
}
private void bannnn ( ) {
OkGo. < String> get ( "http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=200&page=1" ) . execute ( new StringCallback ( ) {
@Override
public void onSuccess ( Response< String> response) {
String body = response. body ( ) ;
try {
JSONObject jsonObject = new JSONObject ( body) ;
JSONArray data = jsonObject. getJSONArray ( "data" ) ;
for ( int i= 0 ; i< data. length ( ) ; i++ ) {
JSONObject jsonObject1 = data. getJSONObject ( i) ;
String pic = jsonObject1. getString ( "pic" ) ;
String tit = jsonObject1. getString ( "title" ) ;
title. add ( tit) ;
img. add ( pic) ;
ban. setBannerTitles ( title) ;
ban. setImages ( img) ;
ban. start ( ) ;
}
} catch ( JSONException e) {
e. printStackTrace ( ) ;
}
}
} ) ;
ban. setImageLoader ( new ImageLoaderInterface ( ) {
@Override
public void displayImage ( Context context, Object path, View imageView) {
String path1 = ( String) path;
Glide. with ( Main2Activity. this ) . load ( path1) . into ( ( ImageView) imageView) ;
}
@Override
public View createImageView ( Context context) {
return new ImageView ( context) ;
}
} ) ;
ban. setBannerStyle ( BannerConfig. NUM_INDICATOR_TITLE ) ;
ban. setDelayTime ( 1000 ) ;
}
private void init ( ) {
OkGo. < String> get ( "http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=200&page=" + page) . execute ( new StringCallback ( ) {
@Override
public void onSuccess ( Response< String> response) {
String body = response. body ( ) ;
JSONObject jsonObject = null ;
try {
jsonObject = new JSONObject ( body) ;
JSONArray data = jsonObject. getJSONArray ( "data" ) ;
for ( int i= 0 ; i< data. length ( ) ; i++ ) {
JSONObject jsonObject1 = data. getJSONObject ( i) ;
String pic = jsonObject1. getString ( "pic" ) ;
list. add ( pic) ;
myAdapter. notifyDataSetChanged ( ) ;
}
} catch ( JSONException e) {
e. printStackTrace ( ) ;
}
pullToRefreshListView. onRefreshComplete ( ) ;
}
} ) ;
}
}
Flyco.tablayout----CommonTabLayout
依赖
implementation 'com.flyco.tablayout:FlycoTabLayout_Lib:2.0.2@aar'
代码
主函数
public class Main3Activity extends AppCompatActivity {
CommonTabLayout ctb;
FrameLayout fram;
ArrayList< CustomTabEntity> list= new ArrayList < > ( ) ;
Fragment1 fragment1;
Fragment2 fragment2;
Fragment3 fragment3;
@Override
protected void onCreate ( Bundle savedInstanceState) {
super . onCreate ( savedInstanceState) ;
setContentView ( R . layout. activity_main3) ;
ctb= findViewById ( R . id. ctb) ;
fram= findViewById ( R . id. fram) ;
fragment1= new Fragment1 ( ) ;
fragment2= new Fragment2 ( ) ;
fragment3= new Fragment3 ( ) ;
list. add ( new Bean ( "fragmen1" , R . drawable. an_true, R . drawable. an_false) ) ;
list. add ( new Bean ( "fragmen2" , R . drawable. an_true, R . drawable. an_false) ) ;
list. add ( new Bean ( "fragmen3" , R . drawable. an_true, R . drawable. an_false) ) ;
getSupportFragmentManager ( ) . beginTransaction ( ) . replace ( R . id. fram, fragment1) . commit ( ) ;
ctb. setTabData ( list) ;
ctb. setOnTabSelectListener ( new OnTabSelectListener ( ) {
@Override
public void onTabSelect ( int position) {
switch ( position) {
case 0 :
getSupportFragmentManager ( ) . beginTransaction ( ) . replace ( R . id. fram, fragment1) . commit ( ) ;
break ;
case 1 :
getSupportFragmentManager ( ) . beginTransaction ( ) . replace ( R . id. fram, fragment2) . commit ( ) ;
break ;
case 2 :
getSupportFragmentManager ( ) . beginTransaction ( ) . replace ( R . id. fram, fragment3) . commit ( ) ;
break ;
}
}
@Override
public void onTabReselect ( int position) {
}
} ) ;
}
}
Bean
一定要实现CustomTabEntity
public class Bean implements CustomTabEntity {
String title;
int sele;
int unsele;
public Bean ( String title, int sele, int unsele) {
this . title = title;
this . sele = sele;
this . unsele = unsele;
}
@Override
public String getTabTitle ( ) {
return title;
}
@Override
public int getTabSelectedIcon ( ) {
return sele;
}
@Override
public int getTabUnselectedIcon ( ) {
return unsele;
}
}