我使用自定义ViewPager
创建了一个自动图像滑块现在我想知道如何在触摸时停止图像的自动滑动,并在触摸释放时再次从滑块开始滑动。
这是我的CustomViewPager.java: -
public class CustomViewPager extends ViewPager {
private boolean isPagingEnabled = true;
public CustomViewPager(Context context) {
super(context);
}
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return this.isPagingEnabled && super.onTouchEvent(event);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
return this.isPagingEnabled && super.onInterceptTouchEvent(event);
}
public void setPagingEnabled(boolean b) {
this.isPagingEnabled = b;
}
}
这是ImageAdapter.java: -
public class SlidingImage_Adapter extends PagerAdapter {
Context context;
int images[];
LayoutInflater inflater;
public SlidingImage_Adapter(Context context, int images[]) {
this.context = context;
this.images = images;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public int getCount() {
return images.length;
}
@Override
public Object instantiateItem(ViewGroup container, final int position) {
View itemView = inflater.inflate(R.layout.sliding_images, container, false);
final ImageView imageView = (ImageView) itemView
.findViewById(R.id.image);
imageView.setImageResource(images[position]);
container.addView(itemView, 0);
return itemView;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
}
@Override
public Parcelable saveState() {
return null;
}
}
这是我在使用这个CustomViewPager的Fragment.java: -
public class HomeFragment extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
TextView tv_slider, tv_button;
RelativeLayout dummyView;
CustomViewPager mViewPager;
Timer timer;
final long DELAY_MS = 1000;//delay in milliseconds before task is to be executed
final long PERIOD_MS = 3000;
private static int currentPage = 0;
private static int NUM_PAGES = 0;
int images[] = {R.drawable.ecommerce, R.drawable.digital_marketing, R.drawable.explainer, R.drawable.it_services,
R.drawable.mobile_app, R.drawable.seo, R.drawable.software, R.drawable.webdesign};
SlidingImage_Adapter imageAdapter;
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public HomeFragment() {
}
public static HomeFragment newInstance(String param1, String param2) {
HomeFragment fragment = new HomeFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_home, container, false);
tv_slider=(TextView)v.findViewById(R.id.tv_slider);
tv_slider.setTypeface(EasyFonts.robotoLight(this.getActivity()));
tv_button=(TextView)v.findViewById(R.id.tv_button);
tv_button.setTypeface(EasyFonts.robotoLight(this.getActivity()));
mViewPager = (CustomViewPager) v.findViewById(R.id.pager);
imageAdapter = new SlidingImage_Adapter(getActivity(),images);
mViewPager.setAdapter(imageAdapter);
final Handler handler = new Handler();
final Runnable Update = new Runnable() {
public void run() {
if (currentPage == NUM_PAGES) {
currentPage = 0;
}
mViewPager.setCurrentItem(currentPage++, true);
}
};
Timer swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
@Override
public void run() {
handler.post(Update);
}
}, DELAY_MS, PERIOD_MS);
CirclePageIndicator indicator = (CirclePageIndicator)
v.findViewById(R.id.indicator);
indicator.setViewPager(mViewPager);
final float density = getResources().getDisplayMetrics().density;
//Set circle indicator radius
indicator.setRadius(3 * density);
NUM_PAGES =images.length;
// Pager listener over indicator
indicator.setOnPageChangeListener(new CustomViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
currentPage = position;
}
@Override
public void onPageScrolled(int pos, float arg1, int arg2) {
switch (pos){
case 0:
tv_slider.setText(getString(R.string.es_content));
break;
case 1:
tv_slider.setText(getString(R.string.dm_content));
break;
case 2:
tv_slider.setText(getString(R.string.ev_content));
break;
case 3:
tv_slider.setText(getString(R.string.it_content));
break;
case 4:
tv_slider.setText(getString(R.string.md_content));
break;
case 5:
tv_slider.setText(getString(R.string.seo_content));
break;
case 6:
tv_slider.setText(getString(R.string.sd_content));
break;
case 7:
tv_slider.setText(getString(R.string.wd_content));
break;
}
}
@Override
public void onPageScrollStateChanged(int pos) {
}
});
return v;
}
答案 0 :(得分:0)
根据this文档,您已经覆盖的onTouchEvent是在哪里查看的。
现在不再像在方法中那样再次调用super.onTouchEvent(event)。实现自己的自定义操作。通过调用超级,您接受默认行为。
答案 1 :(得分:0)
这是我的示例代码,它的工作正常=>
public class MainActivity extends AppCompatActivity {
final long DELAY_MS = 1000;//delay in milliseconds before task is to be executed
final long PERIOD_MS = 3000;
int currentPage = 0;
public Timer timer;
public Timer swipeTimer;
public static boolean startSlider = true;
public TextView textView;
public LinearLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
layout = (LinearLayout) findViewById(R.id.layout);
final Handler handler = new Handler();
final Runnable Update = new Runnable() {
public void run() {
if (currentPage == 6) {
currentPage = 0;
}
currentPage++;
textView.setText(String.valueOf(currentPage));
}
};
swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
@Override
public void run() {
if (startSlider){
handler.post(Update);
}
}
}, DELAY_MS, PERIOD_MS);
layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (startSlider){
startSlider = false;
}else {
startSlider = true;
}
}
});
}
}
答案 2 :(得分:0)
我找到了第三方库"Library Link",它内置了启动和停止自动滚动的方法。