当前位置: 动力学知识库 > 问答 > 编程问答 >

Android ParallaxHeaderViewPager in ScrollView not working

问题描述:

I'm using Android-ParallaxHeaderViewPager in my new project. It's working perfectly using a listview inside fragments, but it's not working when I use a scrollview (the header gets fixed and doesn't make the same effect as in a listview fragment). Any help would be appreciated.

Here is my code:

fragment_lore.xml (Not working)

<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;

}

Also, I tried that article but I couldn't fix my scrollview. Let me know if you need more pieces of my code.

Thanks in advance.

网友答案:

I'm as well trying to understand how to set the scrollView height with respect to the header image! I guess you were trying to implement what is shown here - http://nerds.airbnb.com/host-experience-android/

网友答案:

You better look at this http://flavienlaurent.com/blog/2013/11/20/making-your-action-bar-not-boring/ link to understand your program, because the ParallaxHeaderViewPager was done using this article. Hope this will help you.

网友答案:

I wrote a demo with scrollview instead of listview, and use fragment as container view instead activity.

Android-ParallaxHeaderViewPager_ScrollView

Maybe late for u, but i hope it`s useful to others...

分享给朋友:
您可能感兴趣的文章:
随机阅读: