java - 无法为可滑动的标签滑动

标签 java android android-fragments android-studio

我正在关注 http://www.android4devs.com/2015/01/how-to-make-material-design-sliding-tabs.html我有相同的代码。我无法让选项卡相互滑动,也无法查看选项卡中的任何内容。我的代码如下。感谢您的帮助!

activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="activity.MainActivity">

    <include
        android:id="@+id/toolbar"
        layout="@layout/tool_bar" />

    <slidingModel.SlidingTabLayout
        android:layout_below="@+id/toolbar"
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="4dp"
        android:background="@color/primary_color"

        />

    <android.support.v4.view.ViewPager
        android:layout_below="@id/tabs"
        android:id="@+id/pager"
        android:layout_height="0dp"
        android:layout_width="match_parent"

        />


    <com.melnykov.fab.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="16dp"
        fab:fab_colorNormal="@color/fab_normal"
        fab:fab_colorPressed="@color/fab_pressed"
        fab:fab_colorRipple="@color/fab_ripple"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true" />


</RelativeLayout>

MainActivity.java

package activity;

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.Menu;
import android.view.MenuItem;

import com.gursimran.bei.forte.R;

import adapter.ViewPagerAdapter;
import slidingModel.SlidingTabLayout;

public class MainActivity extends AppCompatActivity {

    Toolbar mToolbar;

    ViewPager pager;
    ViewPagerAdapter adapter;
    SlidingTabLayout tabs;
    CharSequence Titles[] = {"Factorial", "Permutation", "Random"};
    int Numboftabs = 3;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);

        // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
        adapter =  new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs);

        // Assigning ViewPager View and setting the adapter
        pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(adapter);

        // Assiging the Sliding Tab Layout View
        tabs = (SlidingTabLayout) findViewById(R.id.tabs);
        tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width

        // Setting Custom Color for the Scroll bar indicator of the Tab View
        tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
            @Override
            public int getIndicatorColor(int position) {
                return getResources().getColor(R.color.fab_pressed);
            }
        });

        // Setting the ViewPager For the SlidingTabsLayout
        tabs.setViewPager(pager);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

ViewPagerAdapter.java

package adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

import fragment.FactorialTab;
import fragment.PermutationTab;
import fragment.RandomTab;

public class ViewPagerAdapter extends FragmentStatePagerAdapter {

    CharSequence Titles[]; // This will Store the Titles of the Tabs which are Going to be passed when ViewPagerAdapter is created
    int NumbOfTabs; // Store the number of tabs, this will also be passed when the ViewPagerAdapter is created

    // Build a Constructor and assign the passed Values to appropriate values in the class
    public ViewPagerAdapter(FragmentManager fm,CharSequence mTitles[], int mNumbOfTabsumb) {
        super(fm);

        this.Titles = mTitles;
        this.NumbOfTabs = mNumbOfTabsumb;

    }

    //This method return the fragment for the every position in the View Pager
    @Override
    public Fragment getItem(int position) {

        if(position == 0) // if the position is 0 we are returning the First tab
        {
            FactorialTab factorialTab = new FactorialTab();
            return factorialTab;

        } else if(position == 1)             // As we are having 2 tabs if the position is now 0 it must be 1 so we are returning second tab
        {
            PermutationTab permutationTab = new PermutationTab();
            return permutationTab;
        }
        else {
            RandomTab randomTab = new RandomTab();
            return randomTab;
        }
    }

    // This method return the titles for the Tabs in the Tab Strip
    @Override
    public CharSequence getPageTitle(int position) {
        return Titles[position];
    }

    // This method return the Number of tabs for the tabs Strip
    @Override
    public int getCount() {
        return NumbOfTabs;
    }
}

最佳答案

滑动功能由ViewPager控制,这里你的Viewpager layout_height为0dp 所以有问题。

所以基本上是这样设置的

android:layout_width="match_parent"
android:layout_height="wrap_content"

关于java - 无法为可滑动的标签滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32199440/

相关文章:

java - 通过手绘撤消和移动功能

java - Docx4J(Android):如何在Word文档中读取页眉和页脚的内容

java - 无法解析 Android Studio 中的符号 'view'

Android:从 Activity 输入数据后刷新 ViewPager fragment 内容

android - 如何从通知中打开 fragment

java - 在 SQLite JDBC 查询中附加 java.sql.Date

java - 运行集成测试时,Maven 不会初始化 spring 上下文属性

java - 在 Windows 中设置 Eclipse 以进行 Cytoscape 插件开发

android - 通过命令行将 Unity3D 项目导出为 Android 项目

android - 如何使用 Android 中的辅助功能服务访问 WebView 元素的 AccessibilityNodeInfo 的 html 内容?