2016-09-15 62 views
2

這是我的代碼,請檢查一下告訴我是什麼問題。大多數情況下,第二次滑動時不會順滑地滑動fragment。我與FragmentPagerAdapter和覆蓋getItemPosition()方法檢查,但無法找到解決方案。查看傳呼機不能順利滑動

//View Pager Adapter 

public class MyBookingPagerAdapter extends FragmentStatePagerAdapter{ 
    private ArrayList<Booking> bookingList; 
    public MyBookingPagerAdapter(FragmentManager fm,ArrayList<Booking> bookingList) { 
     super(fm); 
     this.bookingList=bookingList; 
    } 

    @Override 
    public Fragment getItem(int position) { 
     return MyBookingFragment.newInstance(bookingList.get(position)); 
    } 

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

    @Override 
    public int getItemPosition(Object object) { 
     return POSITION_NONE; 
    } 
} 

//View Pager Fragment 

public class MyBookingFragment extends BaseFragment { 
    private Booking booking; 
    private RelativeLayout containerTopView; 
    private TextView txtTitle,txtAddress,txtDay,txtMonth,txtShowDayName,txtShowDate,txtLastEntyTime,txtFemalesprice,txtCouplePrice,txtMalesPrice,txtTotalAmount; 
    private ImageView imgTopView; 
    private LinearLayout dotsContainer; 

    public static MyBookingFragment newInstance(Booking booking){ 
     MyBookingFragment fragmentMyBooking= new MyBookingFragment(); 
     Bundle args=new Bundle(); 
     args.putSerializable("booking",booking); 
     fragmentMyBooking.setArguments(args); 
     return fragmentMyBooking; 
    } 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     View view=inflater.inflate(R.layout.fragment_my_booking_appbar,container,false); 
     //  init Layout view (find View By Id from the xml) 
     initViews(view); 

     //  Set booking values of views 
     if(booking!=null) 
      setValues(booking); 
     return view; 
    } 



    private void setValues(Booking booking) { 
     txtTitle.setText(booking.getEventName()); 
     txtAddress.setText(booking.getAddress()); 
     txtShowDayName.setText(booking.getDayName()); 
     txtShowDate.setText(booking.getBookingDate()); 
     txtLastEntyTime.setText(booking.getBookingTime()); 
     txtFemalesprice.setText(booking.getFemale()+" x +getString(R.string.Rs)+booking.getLaydiesPrice()); 
     txtMalesPrice.setText(booking.getMale()+" x "+getString(R.string.Rs)+booking.getStageEntryPrice()); 
     txtCouplePrice.setText(booking.getCouple()+" x "+getString(R.string.Rs)+booking.getCouplePrice()); 
     txtTotalAmount.setText(booking.getTotalPrice()); 
     try{ 
     ImageLoader.getInstance().displayImage(booking.getImgTopView(),imgTopView); 
     }catch (Exception e){ 
      Utils.print("MyBookingFragment ImageLoader",e.toString()); 
     } 
    } 

    private void initViews(View view) { 
     txtShowDayName= (TextView) view.findViewById(R.id.txtMyBookingShowDay); 
     txtShowDate= (TextView) view.findViewById(R.id.txtMyBookingShowDate); 
     txtLastEntyTime= (TextView) view.findViewById(R.id.txtMyBookingLastEntryTime); 
     txtFemalesprice= (TextView) view.findViewById(R.id.txtMyBookingFemalePrice); 
     txtMalesPrice= (TextView) view.findViewById(R.id.txtMyBookingMalesPrice); 
     txtCouplePrice= (TextView) view.findViewById(R.id.txtMyBookingCouplePrice); 
     txtTotalAmount= (TextView) view.findViewById(R.id.txtMyBookingTotal); 
     txtAddress= (TextView) view.findViewById(R.id.txtMyBookingAddress); 
     txtTitle= (TextView) view.findViewById(R.id.txtMyBookingTitle); 
     imgTopView=(ImageView) view.findViewById(R.id.imgTopView); 
     containerTopView= (RelativeLayout) view.findViewById(R.id.containerTopView); 
     booking= (Booking) getArguments().getSerializable("booking"); 
    } 

} 

//Setting View Pager Adapter 

        adapter=new MyBookingPagerAdapter(getSupportFragmentManager(),bookingArrayList); 
        pager.setAdapter(adapter); 
        pager.setOffscreenPageLimit(3); 



        //  showing no data msg view 
        if(adapter.getCount()<=0) 
         findViewById(R.id.frameNoData).setVisibility(View.VISIBLE); 
        else{ 
         setIndicators(bookingArrayList.size()); 
         pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { 
          @Override 
          public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {} 
          @Override 
          public void onPageSelected(int position) { 
           if(indicators!=null) 
            indicators[position].setChecked(true); 
          } 
          @Override 
          public void onPageScrollStateChanged(int state) { } }); 
+0

可能是任何內存問題,請在具有良好規格的其他設備上嘗試,並解決它 – Androider

回答

0

讓您的自定義ViewPager類:在XML文件中使用ViewPagerCustomDuration,而不是默認ViewPager

public class ViewPagerCustomDuration extends ViewPager { 

public ViewPagerCustomDuration(Context context) { 
    super(context); 
    scrollInitViewPager(); 
} 

public ViewPagerCustomDuration(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    scrollInitViewPager(); 
} 

private ScrollerCustomDuration mScroller = null; 

private void scrollInitViewPager() { 
    try { 
     Field scroller = ViewPager.class.getDeclaredField("mScroller"); 
     scroller.setAccessible(true); 
     Field interpolator = ViewPager.class.getDeclaredField("sInterpolator"); 
     interpolator.setAccessible(true); 
     mScroller = new ScrollerCustomDuration(getContext(), (Interpolator) interpolator.get(null)); 
     scroller.set(this, mScroller); 
    } catch (Exception e) { 
    } 
} 
} 

ScrollerCustomDuration.Java

public class ScrollerCustomDuration extends Scroller { 

private double mScrollFactor = 1; 

public ScrollerCustomDuration(Context context) { 
    super(context); 
} 

public ScrollerCustomDuration(Context context, Interpolator interpolator) { 
    super(context, interpolator); 
} 

@SuppressLint("NewApi") 
public ScrollerCustomDuration(Context context, Interpolator interpolator, boolean flywheel) { 
    super(context, interpolator, flywheel); 
} 

/** 
* Set the factor by which the duration will change 
*/ 
public void setScrollDurationFactor(double scrollFactor) { 
    mScrollFactor = scrollFactor; 
} 

@Override 
public void startScroll(int startX, int startY, int dx, int dy, int duration) { 
    super.startScroll(startX, startY, dx, dy, (int) (duration * mScrollFactor)); 
} 

} 

現在。

+0

什麼ScrollerCustomDuration,它是任何自定義類? –

+0

是的,我忘了提及。只需檢查我的編輯。爲了更平滑的滾動,你可以增加scrollfactor變量。 – Piyush

+0

沒有什麼改善(大多在第二或第三次刷卡它跳過片段刷卡,否則工作正常) –

0

您正在創建視圖,每次在onCreateView()。請將您的視圖保存在全局視圖變量中並使用該視圖。

View view; 
in onCreateView() 
// 
if(view=null){ 
view=infla ter.inflate(R.layout.fragment_my_booking_appbar,container,false); 
} 

請檢查您是否正在創建每次新片段,請給你的片段的標籤,並通過標籤從FragmentManager獲得片段。

+0

沒有什麼好轉。 –