Android:TabView Fragment中的Spinner在实现时替换了Tabs

时间:2018-02-15 06:26:18

标签: android android-spinner

我正在尝试将一个Spinner放在TabView中的三个选项卡中。 Spinner替换了所有选项卡,我只在屏幕上看到Spinner。

如果我在 FacultyTab.java 中删除setSpinnerContent(view)中的方法onCreateView,则会显示标签。

以下是我用来实现Spinner和TabView的代码(建议适用于TabView):

MainPage.java (所有标签都在这里实现)

public class MainPage extends AppCompatActivity {

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

        ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        Adapter adapter = new Adapter(getSupportFragmentManager());

        adapter.addFragment(new NoticeTab(), "Notices");
        adapter.addFragment(new FacultyTab(), "Faculties");
        adapter.addFragment(new StudentCornerTab(), "Student Corner");
        viewPager.setAdapter(adapter);

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

main_page.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.collegecommune.MainPage">



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


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

</LinearLayout>

FacultyTab.java

public class FacultyTab extends Fragment implements AdapterView.OnItemSelectedListener {

    public FacultyTab() {}

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

    public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {}

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.faculty_tab, container, false);
        setSpinnerContent(view);

        return view;
    }

    public void setSpinnerContent(View view) {

        getActivity().setContentView(R.layout.faculty_tab);

        Spinner spinner = (Spinner) view.findViewById(R.id.spinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
                R.array.faculty_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);
    }
}

faculty_tab.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Spinner
        android:id="@+id/spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:entries="@array/faculty_array"/>

</RelativeLayout>

Adapter.java

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

    public Adapter(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);
    }
}

首先我在实现Spinner时遇到了一些运行时错误,但在Stackoverflow中搜索后,我删除了错误,但现在发生了这个问题。

注意:我是Android的初学者。我找不到实现TabView的方法,所以我复制了它并编辑了它的一部分。更好地实现TabView的建议也会有很大帮助。

1 个答案:

答案 0 :(得分:1)

只需删除此行

即可
getActivity().setContentView(R.layout.faculty_tab);

setContentView会将R.layout.faculty_tab设置为主页面视图。不是标签的视图