以下是我的代码,请检查告诉我是什么问题。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 :(得分:0)
制作自定义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));
}
}
现在,在xml文件中使用ViewPagerCustomDuration
而不是默认ViewPager
。
答案 1 :(得分:0)
您每次都在threadpool.join()
创建视图。请将您的视图保存在全局视图变量中并使用该视图。
onCreateView()
请检查您是否每次都在创建新的片段。请为片段添加标签,并通过标签从FragmentManager获取片段。