声明:项目参考Kay Wu博客(一枚萌妹)
开源库的使用##
- ViewPager
- PagerSlidingTabStrip
- SwipeRefreshLayout
开源库的导入##
仅远程版本库:
修改app目录build.gradle->dependencies->compile '包名'
详细参考:https://drakeet.me/android-studio
ViewPager##
v4包提供,可让用户左右切换当前view,使用PagerAdapter提供内容,继承FragmentPagerAdapter
必须实现:
- getCount()——返回item总数
- getItem()——根据position返回对应fragment
PagerSlidingTabStrip##
利用github开源项目:
https://github.com/astuetz/PagerSlidingTabStrip
根据ViewPager生成对应tab(横线)在页面切换时产生相应动画效果
SwipeRefreshLayout##
v4包提供,包含下拉刷新功能以及对应动画效果
注意:只能包含一个view,且该view必须是可滑动(如ListView)
项目UI文件##
MainActivity、PagerFragment、ListRefreshFragment
PagerFragment###
布局文件:仅含一个TextView
PagerFragment.java
public class PagerFragment extends android.support.v4.app.Fragment {
private int pageNum;
private TextView tv;
/**
*onCreateView()——加载布局
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/**
* LayoutInflater类似findViewById()——寻找layout下xml布局文件,并实例化
*
* inflate()参数:
* 1.目标布局文件id
* 2.ViewGroup对象
* 3.false:此处解析xml文件不作为根视图View
*/
View pager=inflater.inflate(R.layout.pager_fragment,container,false);
tv=(TextView)pager.findViewById(R.id.tv);
//最初创建会从getArguments()取出参数填充TextView
Bundle bundle=getArguments();//接收bundle类型参数
pageNum=bundle.getInt("page_num");
tv.setText(""+pageNum);
return pager;
}
}
LayoutInflater详细用法参照:
http://www.tuicool.com/articles/MJFNBfF
ListRefreshFragment###
布局文件:包含一个ListView
ListRefreshFragment.java
public class ListRefreshFragment extends android.support.v4.app.Fragment implements SwipeRefreshLayout.OnRefreshListener {
private SwipeRefreshLayout refreshLayout;
private ListView lv;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.list_refresh_fragment,container,false);
refreshLayout=(SwipeRefreshLayout)view.findViewById(R.id.refresh_layout);
//为下拉触发事件设置监听器
refreshLayout.setOnRefreshListener(this);
//通过颜色资源文件设置进度动画颜色资源
//直接使用Color类会抛异常,建议使用android.R.color
refreshLayout.setColorSchemeResources(android.R.color.holo_red_light,android.R.color.holo_green_light,android.R.color.holo_blue_bright);
lv=(ListView)view.findViewById(R.id.lv);
//ArrayAdapter()接收3个参数——Context,resources,List object
lv.setAdapter(new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1,getData()));
return view;
}
@Override
public void onRefresh() {
refreshLayout.setRefreshing(true);//显示刷新动画
//Handler对象里面开启线程,让Runnable执行耗时操作
//postDelayed(runnable,time)——显示动画3秒
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
refreshLayout.setRefreshing(false);
}
},3000);
}
public List getData(){
List data=new ArrayList();
for (int i=0;i<=20;i++){
data.add(i+"");//可用String.valuesOf(i)替换
}
return data;
}
}
i+" "和String.valueOf(i)区别:
前者产生两个String对象,后者直接使用String类静态方法,只产生一个对象
MainActivity###
布局文件:
MainActivity.java
/**
*FragmentActivity向下兼容4.0版本以下,v4包里面内容
*/
public class MainActivity extends FragmentActivity {
private PagerSlidingTabStrip pagerTab;
private ViewPager pager;
public final int NUM_PAGES=4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
pagerTab=(PagerSlidingTabStrip)findViewById(R.id.pager_tabs);
pager=(ViewPager)findViewById(R.id.pager);
pager.setOffscreenPageLimit(3);//设置预加载3个页面(默认为1)
pager.setAdapter(new PagerAdapter(getSupportFragmentManager()));
pagerTab.setViewPager(pager);//设置关联ViewPager
}
@Override
public void onBackPressed() {
//判断正在显示pager是否为0,是就退出程序,反之返回为0的pager
if(pager.getCurrentItem()==0){
super.onBackPressed();
}else {
pager.setCurrentItem(0);
}
}
private class PagerAdapter extends FragmentPagerAdapter{
private final String[] TITLES=getResources().getStringArray(R.array.pager_name);
private PagerAdapter(FragmentManager fm){
super(fm);
}
/**
*CharSequence类型:定义字符串为可读可写序列
*String类型->只读序列
*/
@Override
public CharSequence getPageTitle(int position) {
return TITLES[position];
}
@Override
public Fragment getItem(int position) {
Bundle bundle=new Bundle();
Fragment pagerFragment;
if(position==0){
pagerFragment=new ListRefreshFragment();
}else {
pagerFragment=new PagerFragment();
}
bundle.putInt("page_num",position);
pagerFragment.setArguments(bundle);
return pagerFragment;
}
@Override
public int getCount() {
return NUM_PAGES;//返回Item总数
}
}
}
resources文件:
Douban
- 豆瓣
- 发现
- 讨论
- 我的