Andriod开发中怎么样实现左右滑动切换标签

用TabLayout吧,V7包里面的,我这里有一篇博客。。。。你可以看看。。。带你实现开发者头条APP(四)---首页优化(加入design包)
■网友
如果你使用的 Android Studio 开发 App,有以下 dependencies 可以使用1. Google 的 android.support.design.widget.TabLayout :Android M新控件之FloatingActionButton,TextInputLayout,Snackbar,TabLayout的使用2. nshmura/RecyclerTabLayout · GitHub
■网友
我想题主应该是问实现原理 。之前自己写过一个类似的控件。继承的是Linearlayout,水平布局。根据数据源自动生成Textview填充 。下面的指示器可以根据需求绘制成不同的形状。接着就是要拿到viewpager的引用add一个onPageChangedListener 。在其中监听viewpager的滑动事件,移动指示器(也就是设置其偏移量)。此时有两种情况 :1.一个屏幕可以装下所有的标签item。2.装不下。 第一种情况很简单就光移动指示器就可以了。第二种情况需判断移动到了当前第几项,从而使整个Linearlayout也向左滚动,以显示出剩下的标签。最后没有用到动画。当然其他答友说的tablayout+viewpager是肯定能实现一模一样的效果。
■网友
TabLayout + ViewPager能做到和截图一模一样
■网友
可以使用TabHost + ViewPager,TabHost可以实现上面的三个Tab,ViewPager的使用可以让你滑动切换。当然也可以使用嵌套Fragment + ViewPager,可以参考嵌套Fragment的使用及遇到The specified child already has a parent. You must call removeView()问题的解决 虽然有点不一样,但是原理是差不多的。这个小Demo是在tab中,可以切换多个子tab。
■网友
public class NavigationActivity extends BaseActivity {public static final int TAB_COUNT = 4; private List\u0026lt;IndicatorFragment\u0026gt; list = new ArrayList\u0026lt;\u0026gt;(TAB_COUNT); private IndicatorView indicatorView; private ViewPager viewPager; @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); setContentView(R.layout.activity_navigation); findViews(); initDatas(); initViews(); }private void findViews() {indicatorView = (IndicatorView) findViewById(R.id.indicator_view); viewPager = (ViewPager) findViewById(R.id.view_pager); }private void initDatas() {for (int i = 0; i \u0026lt; TAB_COUNT; ++i) {list.add(IndicatorFragment.newInstance()); } }private void initViews() {viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {@Override public Fragment getItem(int position) {return list.get(position); }@Override public int getCount() {return list.size(); }@Override public CharSequence getPageTitle(int position) {return "TAB" + position; } }); indicatorView.setViewPager(viewPager); }}\u0026lt;?xml version="1.0" encoding="utf-8"?\u0026gt;\u0026lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" android:orientation="vertical" tools:context=".activity.navgation.NavigationActivity"\u0026gt; \u0026lt;com.sunny.webparser.view.IndicatorView android:id="@+id/indicator_view" android:layout_width="match_parent" android:layout_height="48dp" android:background="@color/colorAccent"\u0026gt; \u0026lt;/com.sunny.webparser.view.IndicatorView\u0026gt; \u0026lt;android.support.v4.view.ViewPager android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="match_parent"\u0026gt;\u0026lt;/android.support.v4.view.ViewPager\u0026gt;\u0026lt;/LinearLayout\u0026gt;public class IndicatorView extends LinearLayout { private Paint paint; private Path path; private ViewPager viewPager; private int tabCount; private float initTransX; private float tabWidth; private float offsetX; private float indicatorWidth = 45; private float indicatorHeight = 30; public IndicatorView(Context context) { super(context); init(); } public IndicatorView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public IndicatorView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { setLayerType(LAYER_TYPE_SOFTWARE, null); paint = new Paint(); paint.setAntiAlias(true); paint.setColor(Color.parseColor("#FFFFFF")); paint.setPathEffect(new CornerPathEffect(3)); paint.setStyle(Paint.Style.FILL); path = new Path(); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); tabWidth = (float) (getWidth() / tabCount); initTransX = (tabWidth - indicatorWidth) / 2; initIndicator(); } private void initIndicator() { path.moveTo(0, 0); path.lineTo(indicatorWidth, 0); path.lineTo(indicatorWidth / 2, -indicatorHeight); path.close(); } @Override protected void dispatchDraw(Canvas canvas) { canvas.save(); canvas.translate(initTransX + offsetX, getHeight() + 2); canvas.drawPath(path, paint); canvas.restore(); super.dispatchDraw(canvas); } public void scroll(int position, float positionOffset, int positionOffsetPixels) { offsetX = position * tabWidth + tabWidth * positionOffset; invalidate(); } public void setViewPager(ViewPager viewPager) { this.viewPager = viewPager; initTabs(); viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { scroll(position, positionOffset, positionOffsetPixels); } @Override public void onPageSelected(int position) { } @Override public void onPageScrollStateChanged(int state) { } }); } private void initTabs() { tabCount = viewPager.getAdapter().getCount(); for (int i = 0; i \u0026lt; tabCount; ++i) { addTabView(i); } } private void addTabView(final int pos) { TextView tv = new TextView(getContext()); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); params.width = 0; params.weight = 1; tv.setLayoutParams(params); tv.setText(viewPager.getAdapter().getPageTitle(pos)); tv.setTextColor(getResources().getColor(R.color.white)); tv.setGravity(Gravity.CENTER); tv.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { viewPager.setCurrentItem(pos); } }); addView(tv); }}public class IndicatorFragment extends Fragment { public IndicatorFragment() { } /** * Use this factory method to create a new instance of * this fragment using the provided parameters. * * @return A new instance of fragment IndicatorFragment. */ public static IndicatorFragment newInstance() { IndicatorFragment fragment = new IndicatorFragment(); return fragment; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_indicator, container, false); }}


推荐阅读