actionbar does not refresh tab selection - android

I use a TabSwipeFragment from HoloEverywhere.
In this, I use 3 tabs.
The third tab should not be accessible every time. So I use a TabSelectedListener to check this:
this.setOnTabSelectedListener(new OnTabSelectedListener(){
#Override
public void onTabSelected(int position) {
switch(position){
case 0:
default:
break;
case 1:
break;
case 2:
if(PlayerPage.accessable!=1) {
getSupportActionBar().setSelectedNavigationItem(1);
}
break;
}
}
});
The Fragment Tab is changed properly, but the Navigation not. The third "PlayerPage" is marked blue, as this is selected.

Runnable tryThis = new Runnable(){
#Override
public void run() {
getSupportActionBar().setSelectedNavigationItem(1);
}
};
Handler handler=new Handler();
handler.post(tryThis);
Using this method worked.

Related

Adding child fragments in a separate thread

I'm having an issue with adding a child fragment to another fragment in my android app.
In the parent fragment I have something like this:
#Override
public View onCreateView(blah) {
Thread initializeStateThread = new Thead() {
public void run() {
// Make some requests to get some data that determines
// which child fragment to display
state = // Some state that corresponds to a child frag
// Method that set's the child fragment based on |state|
setChildFragment(state);
}
}
initializeStateThread.start()
// Do a bunch of other stuff
return view;
}
setChildFragment(State state) {
Fragment fragment = null;
switch (mState) {
case CASE_1:
fragment = new DriverFindSpotFragment();
break;
case CASE_2:
fragment = new DriverNavigationFragment();
break;
case CASE_3:
fragment = new DriverArriveSpotFragment();
break;
}
getActivity().runOnUiThread(new Runnable() {
#Ovverride
public void run() {
getChildFragmentManager().beginTransaction().
replace(R.id.driver_fragment_container, fragment).commit();
};
}
However, this only works about 50% of the time. The other 50% of the time the child fragment just doesn't show up (there isn't an error or anything). I think the problem is related to the fact that I'm attempting to add a child fragment after the parent's view is returned form onCreateView.
I've noticed that if I force the main thread to sleep for a second or two, the issue goes away (I assume because the child fragment gets set before the view is returned in that case).
Has anyone ever run into a similar issue? Or can anyone confirm my theory as to why this issue is happening?
Try by adding your Fragment transaction in Main thread.
Like this :
runOnUiThread(new Runnable() {
#Override
public void run() {
getChildFragmentManager().beginTransaction().replace(R.id.driver_fragment_container, fragment).commit();
}
});
You can try as following method define it in fragment and replace with position/state
public void displayView(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new FormFragment();
break;
case 1:
fragment = new HomeFragment();
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getChildFragmentManager();
FragmentTransaction transaction = fragmentManager
.beginTransaction();
if (position != 0)
// transaction.addToBackStack("back");
transaction.replace(R.id.main_ll_container, fragment).commit();
} else {
Log.e("MainActivity", "Error in creating fragment");
}
}
After reading the comments and other answers, I am not sure what and where is the actual problem.
Instead of:
getActivity().runOnUiThread(new Runnable() {
#Ovverride
public void run() {
}
});
Use:
new Handler(Looper.getMainLooper()).post(new Runnable() {
#Ovverride
public void run() {
}
});
This is just a wild guess, most probably it will work.

How to change the image every time when I launch a single fragment?

I have one activity. In that when I launch a single fragment, i should get different image for each launch of fragment.
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.container, PlaceholderFragment.newInstance(position + 1)).commit();
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
// mTitle = getString(R.string.title_section1);
break;
case 2:
// mTitle = getString(R.string.title_section2);
break;
case 3:
//`enter code here` mTitle = getString(R.string.title_section3);
break;
}
}
I see you use Android Studio but you have to be more specific with how you want to determine the image if you want to determine it based on integer sent to fragment try this
public static class CustomFragment extends Fragment{
.....
#Override
public void onCreateView()
{
position = getArguments().getInt("ARG_SECTION_NUMBER")
View view = getView(getActivity());
return view;
}
public View getView(context)
{
View view = context.getLayoutInflater().inflate(R.id.view_layout,null);
ImageView image= (ImageView)view.findViewById(R.id.image_view);
switch(position)
{
case(1):
image.setImageResource(your_image_res_for_1);
break;
.....
default:
break
}
}
}
or if you want to determine based on something else use an if statement or even switch depending on what it is you are using to determine.....hope i helped

How do I tap to move to the next ViewPager page fragment?

I have a ViewPager setup that's been working fine for my app but it only lets me swipe the screen to move to the next slide. What I want is to also be able to move to the next slide by tapping the screen. Is this possible?
I've been trying to figure this out for a week but I couldn't find the right code to make it work.
Here's what I've done so far:
public class JoyfulHappyfuntime extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_joyful_happyfuntime);
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
}
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int pos) {
switch(pos) {
case 0: return new BoopTheName();
case 1: return new Creed();
case 2: return new Waffles();
case 3: return new Smilea();
case 4: return new Smileb();
case 5: return new Smilec();
case 6: return new PancakesOne();
case 7: return new FirstJoyfulHappyfuntime();
case 8: return new Waffles();
case 9: return new Smilea();
case 10: return new Smileb();
case 11: return new Smilec();
//etc
default: return new Continue();
}
}
#Override
public int getCount() {
return 74;
}
}
}
You can try one of those :
Adding a transparent view ontop where you handle the onClick to change
the position to the next.
using a ViewPagerIndicator on top to let the user click on the section he wants
View Pager Tutorial

Android: change background color to PagerTabStrip selected tab [duplicate]

I have a ViewPager, and I move between fragments using a switch and case. I can change the title per position, but I would also like to change the background colour per position.
public PagerTabStrip titleStrip;
titleStrip.setBackgroundColor(Color.DKGRAY);
Using this in my onCreateView sets a permanent background colour. The idea I had was to use the titleStrip.setBackgroundColor(Color.DKGRAY); where I switch the fragments or change the title. But it doesn't work properly. Sometimes the colour changes, sometimes it doesn't, sometimes it changes in the wrong fragment.
This is the code where I switch fragments:
#Override
public Fragment getItem(int position) {
switch (position) {
case 0: titleStrip.setBackgroundColor(Color.DKGRAY); // These
titleStrip.setTextColor(Color.WHITE); // This doesn't work either
return new Fragment0();
case 1:
return new Fragment1();
case 2:
return new Fragment3();
}
return null;
}
First, make suer you have got the titleStrip when createView:
titleStrip = (PagerTabStrip) pagerView.findViewById(R.id.pager_title_strip);
then, you can add OnPageChangeListener to ViewPager, you can do anything you want in onPageSelected method:
mPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
switch (position) {
case 0:
titleStrip.setBackgroundColor(Color.BLUE);
break;
case 1:
titleStrip.setBackgroundColor(Color.GRAY);
break;
}
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});

How to change item in a spinner in ActionBar programmatically

I have a spinner in my ActionBarSherlock with 5 elements in it. By clicking one of the two buttons at the bottom of the screen (see picture) I want to change the item id of the spinner.
This is the relevant code:
#Override
public void onCreate(Bundle savedInstanceState) {
/* ... some code ...*/
Context context = getSupportActionBar().getThemedContext();
ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(context, R.array.arr_contents, R.layout.sherlock_spinner_item);
list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getSupportActionBar().setListNavigationCallbacks(list, this);
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setTitle(title);
/*... some other code ...*/
}
#Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
switch(itemPosition) {
case 0:
// Do stuff
break;
case 1:
// Do stuff
break;
case 2:
// Do stuff
break;
case 3:
// Do stuff
break;
case 4:
// Do stuff
break;
default:
// Do stuff
break;
}
return true;
}
I've tried calling onNavigationItemSelected(3, R.array.arr_contents); for example to move to the third element of the spinner but it doesn't work.
How can I do it?
Not sure but probably:
getSupportActionBar().setSelectedNavigationItem(itemPosition);
Docs

Categories

Resources