Reputation: 25
Following a codelabs tutorial, I'm trying to implement an activity in which I'd display content from a room database. It requires an access to the textview (the content of the tab) in order to update it. But the best I can do is to update the title of the tab, even though I the textview set for content.
In a nutshell, I have a tablayout to which I add tab, I set a layout to the newly added tab. I then access the textview of the view of the tab, to update the content. The id of the textview is the right one, but it changes the title of the tab instead. Did I get the customview concept wrong?
[UPDATE] After a few modifications, it seems that the issue is that the fragments created and the ones I'm trying to update are not the same: the first are created with a view, while the others don't. Therefore I can't access their view to update their content.
A little bit of code now, to illustrate:
I have a main activity
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) {
}
});
}
}
Its layout
<?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>
A fragment for my tabs
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 ");
}
}
Its layout
<?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" />
And an adapter
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);
}
}
Upvotes: 0
Views: 1627
Reputation: 1842
tab.getCustomView()
will return you the custom view used for this tab.
You are confusing between tab's own view and fragment's (contained in viewPager) view.
You have set the same layout for both your tab and TabFragment
Here
tabLayout.addTab(tabLayout.newTab().setCustomView(R.layout.fragment_tab)
.setText("Tab1"));
And Here
@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);
}
First thing: You should rename fragment_tab
to something like custom_tab_layout
to avoid this confusion.
Now you want to update the content (fragment) text, so you need to get the fragment from viewPager which you can find here
Upvotes: 1