У меня есть TabLayout с 2 вкладками. Для каждой вкладки я создал соответствующие фрагменты.
Класс активности (расширяющий AppCompatActivity) таков (ToolsActivity.java):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tools);
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
mViewPager = findViewById(R.id.view_pager);
mViewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = findViewById(R.id.tabs);
tabs.setupWithViewPager(mViewPager);
tabs.getTabAt(0).setIcon(R.drawable.ic_tab_reading);
tabs.getTabAt(1).setIcon(R.drawable.ic_tab_writing);
tabs.setTabMode(TabLayout.MODE_SCROLLABLE);
mCurrentSelectedListener = new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
// ...
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
// ...
}
};
tabs.addOnTabSelectedListener(mCurrentSelectedListener);
}
Соответствующий макет это (activity_tools.xml):
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
tools:context=".ToolsActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:minHeight="?actionBarSize"
android:padding="@dimen/appbar_padding"
android:text="@string/title_activity_tools"
android:textAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:tabInlineLabel="true" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
Адаптер пейджера содержит массив объекта Fragment.
mLstFragments.add(new UHFReadFragment());
mLstFragments.add(new UHFWriteFragment());
этот адаптер также содержит этот фрагмент кода:
@Override
public Fragment getItem(int position) {
if (mLstFragments.size() > 0) {
return mLstFragments.get(position);
}
throw new IllegalStateException("No fragment at position " + position);
}
Первая вкладка - UHFReadFragment. Это часть кода:
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mContext = (ToolsActivity) getActivity();
mSound = new Sound(mContext);
mTagList = new ArrayList<HashMap<String, String>>();
mAdapter = new SimpleAdapter(mContext, mTagList, R.layout.listtag_items,
new String[]{"tagUii", "tagLen", "tagCount", "tagRssi"},
new int[]{R.id.TvTagUii, R.id.TvTagLen, R.id.TvTagCount,
R.id.TvTagRssi});
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
String result = msg.obj + "";
String[] strs = result.split("@");
addEPCToList(strs[0], strs[1]);
mSound.playSound(1);
}
};
mUHF = new UHF(mContext, mHandler);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mTvCount = (TextView) getView().findViewById(R.id.tv_count);
mLvTags = (ListView) getView().findViewById(R.id.LvTags);
mLvTags.setAdapter(mAdapter);
}
И, наконец, это макет для первого фрагмента (uhf_read_fragment.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
tools:context=".ui.main.UHFReadFragment">
<LinearLayout
android:id="@+id/layout4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:background="@color/white"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp" >
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/tvTagUii"
android:textSize="15sp" />
<TextView
android:id="@+id/tv_count"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:text="0"
android:textColor="@color/red"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/tvTagLen"
android:visibility="gone" />
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/tvTagCount"
android:textSize="15sp" />
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="RSSI"
android:textSize="15sp"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="@color/gray" />
<ListView
android:id="@+id/LvTags"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Проблема в том, что расположение фрагментов не появляется, и фактически onViewCreated никогда не вызывается.
Чего еще не хватает? Я думал, что с помощью "tools: context =". Ui.main.UHFReadFragment "" макет будет связан с классом, который контролирует фрагмент.
Отображается только макет действия (при запуске приложения отображаются только заголовок и заголовок вкладок).
С уважением
Jaime