在a codelabs tutorial之后,我正在尝试实施一项活动,在该活动中显示会议室数据库中的内容。它需要访问textview(选项卡的内容)才能更新它。但是,即使我为内容设置了textview,我最好的办法还是更新标签的 title 。
简而言之,我有一个向其添加标签的标签布局,并为新添加的标签设置了布局。然后,我访问选项卡视图的textview来更新内容。文本视图的ID是正确的ID,但它更改了选项卡的标题。我把customview概念弄错了吗?[更新] 经过一些修改后,问题似乎在于所创建的片段与我要更新的片段不相同:第一个片段是使用视图创建的,而其他片段则不是。因此,我无法访问他们的视图来更新他们的内容。
现在有一点代码,以说明:
我主要活动
public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TabLayout tabLayout = findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
final ViewPager viewPager = findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
// Setting a listener for clicks.
viewPager.addOnPageChangeListener(new
TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(
new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
int index = viewPager.getCurrentItem();
PagerAdapter adapter = ((PagerAdapter) viewPager.getAdapter());
TabFragment fragment = adapter.getFragment(index);
fragment.setSettings();
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
}
其布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".Main2Activity"
tools:showIn="@layout/activity_main2">
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/toolbar"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="@id/tab_layout"/>
</RelativeLayout>
我的标签的片段
public class TabFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
private View fragview;
public TabFragment() {
// Required empty public constructor
}
public static TabFragment newInstance(int sectionNumber) {
TabFragment fragment = new TabFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
this.fragview = inflater.inflate(R.layout.fragment_tab, container, false);
TextView textView = (TextView) this.fragview.findViewById(R.id.text_tab);
textView.setText("TAB ");
return this.fragview;
}
public void setSettings(){
TextView textView = (TextView) this.fragview.findViewById(R.id.text_tab);
textView.setText("DONE ");
}
}
其布局
<?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=".TabFragment">
<TextView
android:id="@+id/text_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_blank_fragment" />
还有一个适配器
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
private Context context;
private Map<Integer, TabFragment> pageReferenceMap = new HashMap<Integer, TabFragment>();
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
@Override
public Fragment getItem(int position) {
TabFragment myFragment = TabFragment.newInstance(position);
pageReferenceMap.put(position, myFragment);
return new TabFragment();
}
@Override
public void destroyItem (ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
pageReferenceMap.remove(position);
}
@Override
public int getCount() {
return mNumOfTabs;
}
public TabFragment getFragment(int index) {
return pageReferenceMap.get(index);
}
}
答案 0 :(得分:0)
tab.getCustomView()
将返回用于此标签的自定义视图。
您在选项卡本身的视图和片段视图(包含在viewPager中)之间感到困惑。
您已经为Tab和TabFragment设置了相同的布局
此处
tabLayout.addTab(tabLayout.newTab().setCustomView(R.layout.fragment_tab)
.setText("Tab1"));
在这里
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_tab, container, false);
}
第一件事:您应该将fragment_tab
重命名为custom_tab_layout
之类,以避免混淆。
现在您要更新内容(片段)文本,因此您需要从viewPager中获取片段,您可以在其中找到here