《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
完整开源地址:https://docs.qq.com/doc/DSkNLaERkbnFoS0ZF
public myPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
fragment1 = new Fragment1();
return fragment1;
case 1:
fragment2 = new Fragment2();
return fragment2;
case 2:
fragment3 = new Fragment3();
return fragment3;
default:
return null;
}
}
@Override
public int getCount() {
return title.length;
}
@Override
public CharSequence getPageTitle(int position) {
return title[position];
}
}
}
接下来是补充内容:
1.MainActivity继承的是FragmentActivity,因为想要用到getSupportFragmentManager()来构造适配器
public Fragment getItem(int position) {
switch (position) {
case 0:
fragment1 = new Fragment1();
return fragment1;
case 1:
fragment2 = new Fragment2();
return fragment2;
case 2:
fragment3 = new Fragment3();
return fragment3;
default:
return null;
}
}
在做这个方法的时候,出现了cannot convert from Fragment1 to Fragment这个错误,关于这个错误,你可以点击
http://blog.csdn.net/jason0539/article/details/9712273进行查看。
3.如果是重写的getPageTitle(int position)这个方法,则tabs是文字标题;如果是重写的getPageIconResId(int position),则tabs是图标显示。
如图所示:
上面四个标签就是重写的getPageIconResId(int position)
private final int[] ICONS = { R.drawable.ic_launcher_gplus, R.drawable.ic_launcher_gmail,
R.drawable.ic_launcher_gmaps, R.drawable.ic_launcher_chrome };
@Override
public int getPageIconResId(int position) {
return ICONS[position];
}
4.关于官网上的demo上fragment是这样给出的
/*
-
Copyright © 2013 Andreas Stuetz andreas.stuetz@gmail.com
-
Licensed under the Apache License, Version 2.0 (the “License”);
-
you may not use this file except in compliance with the License.
-
You may obtain a copy of the License at
-
http://www.apache.org/licenses/LICENSE-2.0
-
Unless required by applicable law or agreed to in writing, software
-
distributed under the License is distributed on an “AS IS” BASIS,
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-
See the License for the specific language governing permissions and
-
limitations under the License.
*/
package com.astuetz.viewpager.extensions.sample;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.TextView;
public class SuperAwesomeCardFragment extends Fragment {
private static final String ARG_POSITION = “position”;
private int position;
public static SuperAwesomeCardFragment newInstance(int position) {
SuperAwesomeCardFragment f = new SuperAwesomeCardFragment();
Bundle b = new Bundle();
b.putInt(ARG_POSITION, position);
f.setArguments(b);
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
position = getArguments().getInt(ARG_POSITION);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
FrameLayout fl = new FrameLayout(getActivity());
fl.setLayoutParams(params);
final int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources()
.getDisplayMetrics());
TextView v = new TextView(getActivity());
params.setMargins(margin, margin, margin, margin);
v.setLayoutParams(params);
v.setLayoutParams(params);
v.setGravity(Gravity.CENTER);
v.setBackgroundResource(R.drawable.background_card);
v.setText("CARD " + (position + 1));
fl.addView(v);
return fl;
}
}
关于TypedValue.applyDimension()这个方法,你可以查看 http://blog.csdn.net/harryweasley/article/details/42172321
关于params.setMargins(margin, margin, margin, margin);你可以查看http://www.itnose.net/detail/6038494.html
我这里就不在解释了
好了,关于PagerSlidingTabStrip我已经基本写完了,收获还是很大的。
接下来总结一下,我这个文章的其他链接地址:
1.关于cannot convert from Fragment1 to Fragment这个错误,你可以查看http://blog.csdn.net/jason0539/article/details/9712273
2.关于TypedValue.applyDimension()这个方法,你可以查看http://blog.csdn.net/harryweasley/article/details/42172321
3.关于params.setMargins(margin, margin, margin, margin);你可以查看http://www.itnose.net/detail/6038494.html
4.本篇博客地址:。。。。。。。。。。
5.PagerSlidingTabStrip在github上的地址 ,https://github.com/astuetz/PagerSlidingTabStrip
6.自己应用的下载地址http://download.csdn.net/detail/harryweasley/8314811
7.官网的PagerSlidingTabStrip下载地址 http://download.csdn.net/detail/harryweasley/8314807
8.关于怎么导入包,怎么导入程序,你可以查看 http://blog.csdn.net/harryweasley/article/details/41413547
1、选中标题后,高亮标题
@Override
public void onPageSelected(int position) {
setSelectTextColor(position);
if (delegatePageListener != null) {
delegatePageListener.onPageSelected(position);
}
}
private void setSelectTextColor(int position) {
for (int i = 0; i < tabCount; i++) {
View view = tabsContainer.getChildAt(i);
if (view instanceof ImageButton) {
} else if (view instanceof RelativeLayout) {
View viewText = ((RelativeLayout) view).getChildAt(0);
TextView tabTextView = (TextView) viewText;
if (viewText instanceof TextView) {
if (position == i) {
tabTextView.setTextColor(自定义颜色);
} else {
tabTextView.setTextColor(tabTextColor);
}
}
}
}
}
2、首次进入默认第一个标题高亮,关键标红代码
private void updateTabStyles() {
for (int i = 0; i < tabCount; i++) {
View v = tabsContainer.getChildAt(i);
v.setBackgroundResource(tabBackgroundResId);
tabTextView.setTextColor(tabTextColor);
}
}
}
}
}
[外链图片转存中…(img-TiIJ2Cwl-1717496035636)]
2、首次进入默认第一个标题高亮,关键标红代码
[外链图片转存中…(img-QJG1EJzj-1717496035636)]
private void updateTabStyles() {
for (int i = 0; i < tabCount; i++) {
View v = tabsContainer.getChildAt(i);
v.setBackgroundResource(tabBackgroundResId);