我已经设计了选项卡布局,并且可以通过滑动选项卡进行滚动,但是我的目的是让左右箭头在选项卡之间导航,其中已经看到了多个教程和博客,但是它们都没有帮助我< / p>
图像中显示的选项卡布局我想以相同的方式进行设计。
活动
package com.example.panel_monitoring.activities;
import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.Toast;
import com.example.panel_monitoring.R;
public class feeder_1 extends AppCompatActivity {
private Toolbar toolbar2;
private TabLayout tabLayout;
private Button up,down;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_feeder_1 );
toolbar2 = (Toolbar)findViewById( R.id.toolbar2 );
toolbar2.setTitle( R.string.app_name );
tabLayout = (TabLayout) findViewById( R.id.tablayout_feeder_1);
tabLayout.addTab(tabLayout.newTab().setText("<<"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 1 Item"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2 Item"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 3 Item"));
tabLayout.addTab(tabLayout.newTab().setText(">>"));
final ViewPager viewPager =
(ViewPager) findViewById(R.id.viewpager2);
final PagerAdapter adapter2 = new tabfragmentadapter(getSupportFragmentManager(),tabLayout.getTabCount());
viewPager.setAdapter(adapter2);
tabLayout.setupWithViewPager( viewPager );
}
}
标签版式适配器
package com.example.panel_monitoring.activities;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class tabfragmentadapter extends FragmentPagerAdapter {
private String[] tabs = {"<<","Panel 1","Panel 2","Panel 3",">>"};
int tabCount;
public tabfragmentadapter(FragmentManager fm,int numberofTabs) {
super( fm );
this.tabCount = numberofTabs;
}
public CharSequence getPageTitle(int position)
{
return tabs[ position ];
}
@Override
public Fragment getItem(int position)
{
switch (position) {
case 0:
feeder_tab1 tab0 = new feeder_tab1();
return tab0;
case 1:
feeder_tab2 tab1 = new feeder_tab2();
return tab1;
case 2:
act_panel_2 tab2 = new act_panel_2();
return tab2;
case 3:
act_panel_3 tab3 = new act_panel_3();
return tab3;
case 4:
act_panel_4 tab4 = new act_panel_4();
return tab4;
default:
return null ;
}
}
@Override
public int getCount() {
return tabCount;
}
}
这是我的xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.feeder_1"
android:scrollbarAlwaysDrawHorizontalTrack="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
tools:layout_editor_absoluteX="0dp">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:layout_marginBottom="8dp"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
tools:layout_editor_absoluteX="0dp" />
<android.support.design.widget.TabLayout
android:id="@+id/tablayout_feeder_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="64dp"
android:overScrollMode="always"
app:tabMode="scrollable"
app:tabGravity="center">
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/appBarLayout2"
android:layout_alignParentBottom="true"
android:layout_marginTop="0dp"
android:layout_marginBottom="3dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/appBarLayout2"
tools:layout_editor_absoluteX="8dp" />
</RelativeLayout>