Android:ViewPager rootview被取代了吗?

时间:2014-10-02 11:20:01

标签: android android-fragments android-viewpager

我正在使用带有片段的ViewPager。

寻呼机适配器

class MainPagerAdapter extends FragmentPagerAdapter {

        public MainPagerAdapter( FragmentManager fm ) {
            super( fm );
        }

        @Override
        public Fragment getItem( int pos ) {
            Fragment fragment = DetailedFragment.newInstance( pos );
            return fragment;
        }

        @Override
        public int getCount() {
            return Ad.items.size();
        }

        @Override
        public CharSequence getPageTitle( int position ) {
            return Ad.items.get( position ).getTitle();
        }

}

片段

public class DetailedFragment extends Fragment implements OnClickListener {

    View rootView;
    int item_pos;
    Button btn;
    MyScrollView scrollView;

    public static DetailedFragment newInstance( int pos ) {
        DetailedFragment f = new DetailedFragment();
        Bundle b = new Bundle();
        b.putInt( "pos", pos );
        f.setArguments( b );
        return f;
    }


    @Override
    public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) {
        rootView = inflater.inflate( R.layout.detailed_fragment, container, false );
        item_pos = getArguments().getInt( "pos" );
        btn = ( Button ) rootView.findViewById( R.id.detailed_fragment_btn );
        btn.setOnClickListener( this );
        scrollView = ( MyScrollView ) rootView.findViewById( R.id.detailed_fragment_scroll );
        scrollView.setOnEndScrollListener( this );

        ....
        return rootView;
    }


    @Override
    public void onClick( View v ) {
        switch ( v.getId() ) {
            case R.id.detailed_fragment_btn:
                Log.e( "Hello", "Pos: " + item_pos );
                break;
        }
    }
}

当按下按钮时,我得到当前项目+ 2(如果我在第一个片段,则为3)。因为它直接加载3个片段。

// Does not help
pager.setOffscreenPageLimit( 0 );

// Does not help
btn = ( Button ) pager.getChildAt( pos ).getRootView().findViewById( R.id.detailed_fragment_btn );

这导致了我的主要问题,即: 我需要能够在每个片段内的ScrollView 中滚动时使用setTranslationY移动LinearLayout。滚动时它会移动(我猜测因为我无法看到它)当前片段的布局+2。

使用自定义滚动视图

    @Override
    public void onScrollChanged( MyScrollView scrollView, int x, int y, int oldx, int oldy ) {
        // Does not do any difference if i set these again
        scrollView = ( MyScrollView ) DetailedActivity.pager.getChildAt( item_pos ).getRootView().findViewById( R.id.detail_scrollview );
        scrollView.setOnEndScrollListener( this );

        // Try to move
        holder= ( LinearLayout ) DetailedActivity.pager.getChildAt( item_pos ).getRootView().findViewById( R.id.sections_detail_imgholder );
        holder.setTranslationY( y / 2.5f );
    }

自定义ScrollView

public class MyScrollView extends ScrollView {

    public interface OnEndScrollListener {
        public void onEndScroll();
        void onScrollChanged( MyScrollView scrollView, int x, int y, int oldx, int oldy );
    }

    private boolean mIsFling;
    private OnEndScrollListener mOnEndScrollListener;

    public MyScrollView( Context context ) {
        this( context, null, 0 );
    }

    public MyScrollView( Context context, AttributeSet attrs ) {
        this( context, attrs, 0 );
    }

    public MyScrollView( Context context, AttributeSet attrs, int defStyle ) {
        super( context, attrs, defStyle );
    }

    @Override
    public void fling( int velocityY ) {
        super.fling( velocityY );
        mIsFling = true;
    }

    @Override
    protected void onScrollChanged( int x, int y, int oldX, int oldY ) {
        super.onScrollChanged( x, y, oldX, oldY );

        if ( mIsFling ) {
            if ( Math.abs( y - oldY ) < 2 || y >= getMeasuredHeight() || y == 0 ) {
                if ( mOnEndScrollListener != null ) {
                    mOnEndScrollListener.onEndScroll();
                }
            mIsFling = false;
            }
        }

        if ( mOnEndScrollListener != null ) {
            mOnEndScrollListener.onScrollChanged( this, x, y, oldX, oldY );
        }
    }

    public OnEndScrollListener getOnEndScrollListener() {
        return mOnEndScrollListener;
    }

    public void setOnEndScrollListener( OnEndScrollListener mOnEndScrollListener ) {
        this.mOnEndScrollListener = mOnEndScrollListener;
    }

}

在第一个片段上移动工作,但如果我转到viewpager中的任何其他片段,则不会。

0 个答案:

没有答案