Proper Way to Refresh Android ListVew while in another Tab - android

Basically I have two tabs. A Fragment and a ListFragment.
When in the Fragment, you can insert data into a Database that will populate a ListView in the ListFragment Tab directly next to it. I want to be able to swipe to that ListFragment (after inserting new data) and while using the ViewPager function for swiping, I want the ListView to refresh.
Is there a proper way to do this? I can show code if requested; but not sure which code to show. To start, here is my View pager setup:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_layout);
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayHomeAsUpEnabled(true);
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
Please note: This is NOT a dual pane setup.

The proper way to do it is to have your ListFragment implement the LoaderManager.LoaderCallbacks<D> interface and initialize a Loader to load the data being bound to the ListView.
You can read this blog post to learn more about Loaders.

Related

How to style Android default ViewPager's bar, created from Android Studio template

I am trying to create a tabbed activity, and I'm using ViewPager for that (actually, Android Studio created it from template, I didn't even know what ViewPager was). I want to style the tab-bar below (indicated with red "circle"):
I've tried setting styles.xml tried almost everything, but still, nothing applies to that. Here is the code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up the action bar.
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
actionBar.addTab(
actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
Ironically, the generated code (by Android Studio's latest version) has many deprecated method calls, but I don't want to touch them unless I know what exactly I'm doing. Here is the layout file (activity_main.xml), again, generated by Android Studio:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="#+id/pager"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context="MY_APP_IDENTIFIER"/>
How can I change the colors of the tab bar?
Try using onPageSelected it's probably the simplest way I think. Here is a link.

How to add a tab to SlidingTabLayout?

I am using google's SlidingTabLayout in my view, but i want to add dynamically new tabs to it.
I'm using this http://developer.android.com/samples/SlidingTabsBasic/src/com.example.android.common/view/SlidingTabLayout.html
This is my code which initialize it:
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
//this ^ is a FragmentStatePagerAdapter
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// Assiging the Sliding Tab Layout View
tabs = (SlidingTabLayout) findViewById(R.id.tabs);
// Setting the ViewPager For the SlidingTabsLayout
tabs.setViewPager(mViewPager);
I have absolutely no idea how to add a Tab.
I thought about changing the ViewPager but in the SlidingTabLayout there is a comment:
/**
* Sets the associated view pager. Note that the assumption here is that the pager content
* (number of tabs and tab titles) does not change after this call has been made.
*/
public void setViewPager(ViewPager viewPager) {
There's probably a nicer, more efficient way, but simply calling setViewPager() every time you add a page to your ViewPager will work.
Here is a nice tutorial. http://www.android4devs.com/2015/01/how-to-make-material-design-sliding-tabs.html
In short, you can supply them with your Pager Adapter. Override the getPageTitle method in order to supply something custom. E.g.
#Override
public CharSequence getPageTitle(int position) {
return _items[position].getDisplayName();
}

fragment in view pager not working properly

I implemented view pager in my activity like :
class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "FIRST", "SECOND", "THIRD" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
getActionBar().setHomeButtonEnabled(false);
getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
getActionBar().addTab(getActionBar().newTab().setText(tab_name)
.setTabListener(this));
}
but when I swipe between tabs it never call the oncreateview of THIRD fragment but it actually call the oncreateview of THIRD fragment when I swipe to the SECOND fragment (From first to Second) and the oncreate view of second fragment get call along with the first fragment.
I don't understand what's wrong with the flow.
There is nothing wrong with this. when you swipe to new page in viewpager, it automatically loads the two neighbor fragments of current visible fragment. why? because of having better UX.
Can I prevent it from doing that?
No.
Can I extend it to load more neighbor fragments?
yes, setOffscreenPageLimit

How to fix scrollable tabs always below the action bar in android

In tab it looks like below.
But in device it looks fine
..
I am looking like below what the google play contained in phones and tabs as same
this is my oncreate method. I had done scroll tabs with view pager .In devices it fits good but in tabs ?.I know this is action bar design pattern .
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_directory_activity);
final ActionBar actionBar = getSupportActionBar();
actionBar.setIcon(R.drawable.logo);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(true);
myDirectoryPagerAdapter = new MyDirectoryAdapter(
getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(myDirectoryPagerAdapter);
mViewPager.setOnPageChangeListener(new
ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < myDirectoryPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab()
.setText(myDirectoryPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}
It will look below the actionbar in the device because the device does not have much space to show all the tabs so the actionbar will manage on its own to show the tabs and adjust it according to the device size. As you have also displayed a logo in actionbar so it will always switch to below.
That is by default for Tabs from Action bar. It is good thing bcoz it saves some screen space for you.
If you dont want it to be present in the action bar. You need to implement it separately. Use PagerTitleStrip.
Two implementations here:
http://developer.android.com/training/implementing-navigation/lateral.html#PagerTitleStrip
https://github.com/astuetz/PagerSlidingTabStrip

changing action bar title for every swipe api level < 11

I couldn't manage to change the action bar title for every swipe action, there's always a problem. Either they get mixed or just some of them does not show up. Here are my tab codes:
actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
SectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
ViewPager = (ViewPager) findViewById(R.id.viewpager);
ViewPager.setAdapter(mSectionsPagerAdapter);
ViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
Tab tab = actionBar.newTab()
.setIcon(R.drawable.home)
.setTabListener(this);
.
.
.
see answer below before going further into the question.
You can try using this in your Fragment's onResume method :
if(getUserVisibleHint()){
getActionBar().setTitle(title);
}
defining String title as a variable inside your fragment.
You should read the documentation on it: http://developer.android.com/training/implementing-navigation/lateral.html#horizontal-paging
and
http://developer.android.com/training/implementing-navigation/lateral.html#swipe-tabs
Download the source code and use it. It worked for me. I had the same problem.

Categories

Resources