ViewPager和片段没有连接
我试图实现与片段的选项卡视图,但我的片段没有出现。我已经将它与viewpager连接起来,但仍然没有工作。ViewPager和片段没有连接
这是我的MainActivity
package com.example.minkhantlu.timetableapp;
import android.support.design.widget.TabLayout;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TableLayout;
import com.example.minkhantlu.timetableapp.fragment.ViewPagerAdapter;
public class HomeActivity extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager viewPager;
private ViewPagerAdapter viewAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
viewPager = (ViewPager) findViewById(R.id.my_view_pager);
viewAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(viewAdapter);
TabLayout.Tab mon = tabLayout.newTab();
TabLayout.Tab tue = tabLayout.newTab();
TabLayout.Tab wed = tabLayout.newTab();
TabLayout.Tab thur = tabLayout.newTab();
TabLayout.Tab fri = tabLayout.newTab();
mon.setText("Mon");
tue.setText("Tue");
wed.setText("Wed");
thur.setText("Thur");
fri.setText("Fri");
tabLayout.addTab(mon, 0);
tabLayout.addTab(tue, 1);
tabLayout.addTab(wed, 2);
tabLayout.addTab(thur,3);
tabLayout.addTab(fri,4);
tabLayout.setTabTextColors(ContextCompat.getColorStateList(this, R.color.colorAccent));
tabLayout.setSelectedTabIndicatorColor(ContextCompat.getColor(this, R.color.colorAccent));
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
}
}
与此viewPagerAdapter
package com.example.minkhantlu.timetableapp.fragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.Log;
public class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return new MondayFragment();
}
@Override
public int getCount() {
return 0;
}
}
我的片段
package com.example.minkhantlu.timetableapp.fragment;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.minkhantlu.timetableapp.R;
public class MondayFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_monday, container, false);
}
尝试
} 片段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="com.example.minkhantlu.timetableapp.fragment.MondayFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_blank_fragment" />
</RelativeLayout>
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="com.example.minkhantlu.timetableapp.HomeActivity">
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimaryDark"/>
<android.support.v4.view.ViewPager
android:id="@+id/my_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tab_layout"
/>
</FrameLayout>
但我的片段并没有出现,plz帮助我与此有关。
您在getCount()
中返回0。在这里您应该返回ViewPager
要显示的页面数量。
你的方法应该返回页面的数量来显示
@Override
public int getCount() {
return 0;
}
,你可以修改它返回1,因此它会在寻呼机显示页面
@Override
public int getCount() {
return 1;
}
我知道了你的答案! –
在ViewPagerAdapter,可以尝试更改
@Override
public int getCount() {
return 0;
}
到
@Override
public int getCount() {
return 1;
}
哦,我知道了,如果我想创建5个片段,它应该是5? –
在ViewPagerAdapter
@Override
public int getCount() {
return 1;
}
和inOncreate
//set Adapter to view pager
viewPager.setAdapter(viewAdapter);
//set tablayout with viewpager
tabLayout.setupWithViewPager(viewPager);
// adding functionality to tab and viewpager to manage each other when a page is changed or when a tab is selected
pager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
//Setting tabs from adpater
tabLayout.setTabsFromPagerAdapter(viewAdapter);
或审查教程在
http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/
我知道了THX你的答案! –
没问题。请记住将此问题标记为已回答。 – Emmanuel