Android|Android SlidingTabLayout用法&禁止ViewPager滑动
2019独角兽企业重金招聘Python工程师标准>>>
文章图片
【Android|Android SlidingTabLayout用法&禁止ViewPager滑动】Android 提供的API例子程序中有个自定义View,SlidingTabLayout+SlidingTabStrip,两者结合构造完美可滑动Tab
主要代码
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.android.common.logger.Log;
import com.example.android.common.view.CustomViewPager;
import com.example.android.common.view.SlidingTabLayout;
public class SlidingTabsBasicFragment extends Fragment {static final String LOG_TAG = "SlidingTabsBasicFragment";
private SlidingTabLayout mSlidingTabLayout;
private CustomViewPager mViewPager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_sample, container, false);
}@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
mViewPager = (CustomViewPager) view.findViewById(R.id.viewpager);
mViewPager.setAdapter(new SamplePagerAdapter());
mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
mSlidingTabLayout.setViewPager(mViewPager);
mViewPager.setCanPageScroll(false);
}class SamplePagerAdapter extends PagerAdapter {/**
* @return the number of pages to display
*/
@Override
public int getCount() {
return 10;
}@Override
public boolean isViewFromObject(View view, Object o) {
return o == view;
}
@Override
public CharSequence getPageTitle(int position) {
return "Item " + (position + 1);
}@Override
public Object instantiateItem(ViewGroup container, int position) {
View view = getActivity().getLayoutInflater().inflate(R.layout.pager_item,
container, false);
TextView title = (TextView) view.findViewById(R.id.item_title);
title.setText(String.valueOf(position + 1));
container.addView(view);
return view;
}@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
Log.i(LOG_TAG, "destroyItem() [position: " + position + "]");
}}
}
xml代码
CustomViewPager
package com.example.android.common.view;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
public class CustomViewPager extends ViewPager {private boolean isCanPageScroll = true;
private int scaledTouchSlop = 0;
private float startX = 0f;
private float startY = 0f;
public CustomViewPager(Context context) {
this(context,null);
}public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
scaledTouchSlop = ViewConfiguration.get(context).getScaledPagingTouchSlop();
}public void setCanPageScroll(boolean isCanScroll){
this.isCanPageScroll = isCanScroll;
}@Override
public boolean onTouchEvent(MotionEvent ev) {
if(!isCanPageScroll && interceptTouchEvent(ev))
{
return true;
}
returnsuper.onTouchEvent(ev);
}/**
* 判断是否拦截滑动
* @param ev
* @return
*/
private boolean interceptTouchEvent(MotionEvent ev)
{
float curX = ev.getX();
float curY = ev.getY();
switch (ev.getAction())
{
case MotionEvent.ACTION_DOWN:
startX = curX;
startY = curY;
break;
case MotionEvent.ACTION_MOVE:
if(Math.abs(curY-startY)=scaledTouchSlop)
{
return true;
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
startX = 0f;
startY = 0f;
break;
}
return false;
}}
转载于:https://my.oschina.net/ososchina/blog/407143
推荐阅读
- android第三方框架(五)ButterKnife
- Android中的AES加密-下
- 带有Hilt的Android上的依赖注入
- android|android studio中ndk的使用
- Android事件传递源码分析
- RxJava|RxJava 在Android项目中的使用(一)
- Android7.0|Android7.0 第三方应用无法访问私有库
- 深入理解|深入理解 Android 9.0 Crash 机制(二)
- android防止连续点击的简单实现(kotlin)
- Android|Android install 多个设备时指定设备