我在我的新项目中使用Android-ParallaxHeaderViewPager。它在片段内使用listview完美地工作,但是当我使用scrollview时它不起作用(标题得到修复,并且与listview片段的效果不同)。任何帮助,将不胜感激。 这是我的代码:
fragment_lore.xml (不工作)
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scrollViewLore">
<TextView
android:id="@+id/championLore"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</ScrollView>
LoreFragment.java
public class LoreFragment extends ScrollTabHolderFragment implements OnScrollListener {
private int mPosition;
private ScrollView mScrollView;
private int mTabBarHeight;
private int mTopImageHeight;
public static final LoreFragment newInstance(Integer position, Integer pos){
LoreFragment fragment = new LoreFragment();
Bundle args = new Bundle();
args.putInt("position",position);
args.putInt("pos",pos);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPosition = getArguments().getInt("position");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_lore,container,false);
mScrollView = (ScrollView) view.findViewById(R.id.scrollViewLore);
mTabBarHeight = getResources().getDimensionPixelSize(R.dimen.tab_bar_height);
KenBurnsView headerImg = (KenBurnsView) getActivity().findViewById(R.id.header_picture);
mTopImageHeight = headerImg.getHeight();
TextView loreView = (TextView) view.findViewById(R.id.championLore);
Integer pos = Integer.valueOf(getArguments().getInt("pos"));
String lore = Cache.champions.get(pos).getDescription();
lore = lore.replace("<br>","\n");
loreView.setText(lore);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public void adjustScroll(int scrollHeight) {
if (scrollHeight == 0 && mScrollView.getScrollY() > mTopImageHeight - mTabBarHeight) {
// ScrollView does not need to adjust scroll, as the tab bar is its sticky position
return;
}
mScrollView.scrollTo(mScrollView.getScrollX(), -scrollHeight + mTopImageHeight - mTabBarHeight);
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (mScrollTabHolder != null)
mScrollTabHolder.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount, mPosition);
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// nothing
}
}
MainActivity.java
public class ChampionDetailsActivity extends ActionBarActivity implements ScrollTabHolder, ViewPager.OnPageChangeListener{
...
@Override
public void onPageScrollStateChanged(int arg0) {
// nothing
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// nothing
}
@Override
public void onPageSelected(int position) {
SparseArrayCompat<ScrollTabHolder> scrollTabHolders = mPagerAdapter.getScrollTabHolders();
ScrollTabHolder currentHolder = scrollTabHolders.valueAt(position);
currentHolder.adjustScroll((int) (mHeader.getHeight() + mHeader.getTranslationY()));
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount, int pagePosition) {
if (mViewPager.getCurrentItem() == pagePosition) {
int scrollY = getScrollY(view);
mHeader.setTranslationY(Math.max(-scrollY, mMinHeaderTranslation));
float ratio = clamp(mHeader.getTranslationY() / mMinHeaderTranslation, 0.0f, 1.0f);
setTitleAlpha(clamp(5.0F * ratio - 4.0F, 0.0F, 1.0F));
}
}
@Override
public void adjustScroll(int scrollHeight) {
// nothing
}
public int getScrollY(AbsListView view) {
View c = view.getChildAt(0);
if (c == null) {
return 0;
}
int firstVisiblePosition = view.getFirstVisiblePosition();
int top = c.getTop();
int headerHeight = 0;
if (firstVisiblePosition >= 1) {
headerHeight = mHeaderHeight;
}
return -top + firstVisiblePosition * c.getHeight() + headerHeight;
}
public static float clamp(float value, float max, float min) {
return Math.max(Math.min(value, min), max);
}
public int getActionBarHeight() {
if (mActionBarHeight != 0) {
return mActionBarHeight;
}
getTheme().resolveAttribute(android.R.attr.actionBarSize, mTypedValue, true);
mActionBarHeight = TypedValue.complexToDimensionPixelSize(mTypedValue.data, getResources().getDisplayMetrics());
return mActionBarHeight;
}
...
public class PagerAdapter extends FragmentPagerAdapter {
private Integer pos;
private ScrollTabHolder mListener;
private SparseArrayCompat<ScrollTabHolder> mScrollTabHolders;
...
public PagerAdapter(FragmentManager fm) {
super(fm);
mScrollTabHolders = new SparseArrayCompat<ScrollTabHolder>();
}
public PagerAdapter(FragmentManager fm, Integer pos) {
super(fm);
this.pos=pos;
mScrollTabHolders = new SparseArrayCompat<ScrollTabHolder>();
}
public void setTabHolderScrollingContent(ScrollTabHolder listener) {
mListener = listener;
}
...
@Override
public Fragment getItem(int position) {
ScrollTabHolderFragment fragment = null;
switch (position) {
case 0:
fragment = (ScrollTabHolderFragment) ListViewFragment.newInstance(position,pos);
break;
case 1:
fragment = (ScrollTabHolderFragment) LoreFragment.newInstance(position,pos);
break;
}
mScrollTabHolders.put(position, fragment);
if (mListener != null) {
fragment.setScrollTabHolder(mListener);
}
return fragment;
}
public SparseArrayCompat<ScrollTabHolder> getScrollTabHolders() {
return mScrollTabHolders;
}
另外,我尝试that article,但无法修复我的滚动视图。如果您需要更多代码,请告诉我。
提前致谢。
答案 0 :(得分:0)
我也想了解如何设置相对于标题图像的scrollView高度! 我猜你试图实现这里显示的内容 - http://nerds.airbnb.com/host-experience-android/
答案 1 :(得分:0)
您最好查看此http://flavienlaurent.com/blog/2013/11/20/making-your-action-bar-not-boring/链接以了解您的程序,因为ParallaxHeaderViewPager是使用本文完成的。 希望这会帮助你。
答案 2 :(得分:0)
我用scrollview而不是listview编写了一个demo,并使用fragment作为容器视图而不是activity。
Android-ParallaxHeaderViewPager_ScrollView
也许你迟到了,但我希望它对别人有用......