Container Activity에 있지만 탭이 표시되지 않는 탭 레이아웃의 탭을 표시해야합니다. 이 프로젝트가 도움이된다면 로그인 활동,지도 활동, 등록 활동의 세 가지 활동도 있습니다.TabLayout의 탭이 표시되지 않습니다.
참고 :지도 조각은지도 활동과 다릅니다.
Container.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_container);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(viewPager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new MapFragment(), "Map");
adapter.addFragment(new Chat(),"Chat");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter{
private List<Fragment> mFragment = new ArrayList<>();
private List<String> mFragmentTitle = new ArrayList<>();
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) { return null; }
@Override
public int getCount() {
return 0;
}
public void addFragment(Fragment fragment, String fragmentTitle){
mFragment.add(fragment);
mFragmentTitle.add(fragmentTitle);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitle.get(position);
}
}
activity_container.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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: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="wrap_content"
android:textAlignment="center"
android:background="@android:color/darker_gray"
app:layout_scrollFlags="scroll|enterAlways"
android:minHeight="22dp"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
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>
채팅 조각 Chat.java
public class Chat extends Fragment {
public Chat(){
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_chat, container,false);
}
}
activity_chat.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_chat"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.downy.gpslocation.Chat">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hey there this is chat"
android:layout_centerInParent="true"/>
</RelativeLayout>
미리 도움을 주셔서 감사합니다.
@Override
public int getCount() {
return mFragment.size();
}
당신이 어댑터가 비어 있다는 말을, 0을 반환하기 때문에 : 내가 코드 ViewPagerAdapter
에서 본
이것은 재정의 메서드이며 사용하지 않으므로 0 또는 null이 반환됩니다. – TOderMAl
그들을 사용하여 어댑터 수와 표시하려는 프래그먼트를 반환하고 'getPageTitle'을 사용하면 표시 할 제목을 반환합니다. –