Android Tablayout,为什么标签不动?

时间:2018-03-29 06:07:20

标签: java android tabs android-tablayout

我正在关注本教程https://www.youtube.com/watch?v=zcnT-3F-9JA我已经使用了来自github的代码,但输出错误。我在顶部放了3个标签,当我按下标签时,活动应该改变,但实际上没有任何反应,我仍然只有我的main_activity在屏幕上。希望有人会帮忙。这是我的代码

PS - 是的我每个片段都有3个xml文件(即使我也有main_activity,但是我必须找出错误,然后将1tab与主要活动一起使用)。我还为这3个标签提供了3个java文件。

XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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"
    android:background="#FFFFFF"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <android.support.v4.view.ViewPager
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />


        <TextView
            android:id="@+id/lvltext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/expa"
            android:layout_centerInParent="true"
            android:fontFamily="@font/futuracondensed"
            android:text="@string/leveltext"
            android:textColor="@color/black"
            android:textSize="30sp" />

        <TextView

            android:id="@+id/lvlnum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/lvltext"
            android:layout_centerInParent="true"
            android:fontFamily="@font/futuracondensed"
            android:text="@string/levelnum"
            android:textColor="@color/black"
            android:textSize="30sp" />

        <ImageView
            android:id="@+id/girl"
            android:layout_width="wrap_content"
            android:layout_height="262dp"
            android:layout_below="@id/lvlnum"
            android:src="@drawable/girl" />

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/girl"
            android:fontFamily="@font/futuracondensed"
            android:text="@string/button"
            android:textColor="@color/black" />

        <ImageView
            android:id="@+id/girl2"
            android:layout_width="match_parent"
            android:layout_height="258dp"
            android:layout_below="@id/button"
            android:src="@drawable/fitnessmodel" />

        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/girl2"
            android:fontFamily="@font/futuracondensed"
            android:text="@string/button2"
            android:textColor="@color/black" />

        <LinearLayout
            android:layout_below="@id/tabs"
            android:id="@+id/expa"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/team_a_score"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:fontFamily="@font/futuracondensed"
                    android:gravity="center"
                    android:paddingBottom="5dp"
                    android:text="0"
                    android:textColor="@color/black"
                    android:textSize="60sp" />
            </LinearLayout>

            <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:layout_marginTop="0dp"
                android:background="@color/black" />

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/team_b_score"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fontFamily="@font/futuracondensed"
                    android:gravity="center"
                    android:paddingBottom="5dp"
                    android:text="100"
                    android:textColor="@color/black"
                    android:textSize="60sp" />
            </LinearLayout>
        </LinearLayout>
    </RelativeLayout>

</android.support.v4.widget.NestedScrollView>

爪哇

public class MainActivity extends AppCompatActivity {
    private SectionsPageAdapter mSectionsPageAdapter;

    private ViewPager mViewPager;

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

        mSectionsPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = findViewById(R.id.container);
        setupViewPager(mViewPager);

        TabLayout tabLayout = findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);
    }

    private void setupViewPager(ViewPager viewPager) {
        SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
        adapter.addFragment(new Tab1Fragment(), "TAB1");
        adapter.addFragment(new Tab2Fragment(), "TAB2");
        adapter.addFragment(new Tab3Fragment(), "TAB3");
        viewPager.setAdapter(adapter);
    }


    @Override
    public void onResume() {
        super.onResume();
    }
}



public class SectionsPageAdapter extends FragmentPagerAdapter {

        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        public SectionsPageAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }
    }

3 个答案:

答案 0 :(得分:2)

使用库中给出的这个XML你已经跟随了你想要将它们添加到你已经采用的片段布局的任何设计。你可以正常工作

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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"
    android:orientation="vertical"
    tools:context="com.example.chirag.slidingtabsusingviewpager.MainActivity">

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/toolbar"
        android:background="@color/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="SlidingTabsUsingViewPager"
            android:textSize="20dp"/>


    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tablayout"
        android:background="@color/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    </android.support.design.widget.TabLayout>

    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:id="@+id/pager">

    </android.support.v4.view.ViewPager>

</LinearLayout>

答案 1 :(得分:0)

试试这段代码:

OneFragment.java

OneFragment.java
package info.androidhive.materialtabs.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import info.androidhive.materialtabs.R;


public class OneFragment extends Fragment{

public OneFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_one, container, false);
}

 }

<强> fragment_one.xml

 fragment_one.xml
<RelativeLayout 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"
 tools:context="info.androidhive.materialtabs.fragments.OneFragment">

 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/one"
    android:textSize="40dp"
    android:textStyle="bold"
    android:layout_centerInParent="true"/>

</RelativeLayout>

<强> activity_main.xml中

<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"/>
    </android.support.design.widget.AppBarLayout>

   <android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"  />
</android.support.design.widget.CoordinatorLayout>

<强> MainActivity.java

  package info.androidhive.materialtabs.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import java.util.ArrayList;
import java.util.List;

import info.androidhive.materialtabs.R;
import info.androidhive.materialtabs.fragments.OneFragment;
import info.androidhive.materialtabs.fragments.ThreeFragment;
import info.androidhive.materialtabs.fragments.TwoFragment;

[![enter image description here][1]][1]public class MainActivity extends AppCompatActivity {

private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;

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

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

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new 
ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new OneFragment(), "ONE");
    adapter.addFragment(new TwoFragment(), "TWO");
    adapter.addFragment(new ThreeFragment(), "THREE");
    viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
  }
 }

enter image description here

并添加调用3个标签的主要活动

     private class MyPagerAdapter extends FragmentPagerAdapter {

     public MyPagerAdapter(FragmentManager fm) {
        super(fm);
     }

    @Override
    public Fragment getItem(int pos) {
        switch(pos) {
            case 0: return SimpleFragment.newInstance("FirstFragment, Instance 1");
            case 1: return PieView.newInstance("SecondFragment, Instance 1");
            case 2: return PieView1.newInstance("ThirdFragment, Instance 1");
             case 3: return DataSaveDetails.newInstance("ThirdFragment, Instance 1");
              default:
        }
        return null;
    }

它有效,请尝试这个

答案 2 :(得分:0)

在你的getItem()方法上。使用Switch并返回片段,而不是在addFragment方法中添加所有片段。

    public Fragment getItem(int position){
     switch(position){
       case 0 : return new Tab1Fragment();
       case 1 : return new Tab2Fragment();
       case 2 : return new Tab3Fragment();
     }

    }