Android - Swiping without Tabs - android

NoteActivity Code:
public class NoteActivity extends FragmentActivity implements ActionBar.TabListener
{
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = {"Note", "Note Info"};
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note);
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
TabsPageAdapter.Java
public class TabsPagerAdapter extends FragmentPagerAdapter
{
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index)
{
case 0:
return new NoteFragment();
case 1:
return new NoteInfoFragment();
}
return null;
}
#Override
public int getCount()
{
// get item count - equal to number of tabs
return 2;
}
}
So basically, I used an example of Tabs View and got the Tabs working and it looks like this:
What would I have to do to make it just swipeable without the Tabs showing. An example is like Snapchat. It definitely uses the Swipe view control but the tabs are hidden. Can someone please show me how to get this done?

Since you already have a ViewPager in your code, all you have to do is remove the code that creates the ActionBar tabs (under the comment // Adding tabs), as well as the code that synchronizes the tab selection with the current page (start with the ViewPager.OnPageChangeListener and the ActionBar.TabListener callbacks and see if anything breaks).

Related

how can I change the color of the tabBar in tab fragment (Viewpager)

I tried many times but it's not OK.
I try to change actionBar in MainActivity but the TabBar doesn't change, so I don't no how to change it.
This is my code :
MainActivivty
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private ActionBar actionBar;
private TabsPagerAdapter mAdapter;
private String[] tabs = { "Bài Hát", "Yêu Thích"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar ac = getActionBar();
ac.setBackgroundDrawable(new ColorDrawable(Color.rgb(72, 209, 204)));
//ac.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#E64260")));
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
if (position == 1) {
TabYeuThich frag = (TabYeuThich) mAdapter
.getFragmentTabYeuThich(1);
frag.resetPage();
} else {
TabBaiHat frag = (TabBaiHat) mAdapter
.getFragmentTabBaiHat(0);
frag.resetPage();
}
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
This is class : TabsPagerAdapter
public class TabsPagerAdapter extends FragmentPagerAdapter {
FragmentManager fm;
TabYeuThich fragYeuThich;
TabBaiHat fragBaiHat;
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
this.fm = fm;
}
#Override
public Fragment getItem(int index) {
// TODO Auto-generated method stub
switch (index) {
case 0:
fragBaiHat = new TabBaiHat();
return fragBaiHat;
case 1:
fragYeuThich = new TabYeuThich();
return fragYeuThich;
}
return null;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 2;
}
public Fragment getFragmentTabYeuThich(int post) {
return fragYeuThich;
}
public Fragment getFragmentTabBaiHat(int post) {
return fragBaiHat;
}
Please help me ! Thanks all !
Useally I set up the action bar from themes.xml
I recommend you follow up the both tutorials
developer android
action bar explanation

How i operation on Back Button in fragment. ask for back button of hardware of phone

i want to operation on back button in main fragment class. i am use swipe tab with help of fragment. so in as par activity class i know function of back button. but this is not help of me. this is my code. i already done on action bar home function. so i want to do same as back button
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Metals","Forex" };
static int i = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
i=0;
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
// actionBar.setHomeButtonEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case android.R.id.home:
Log.d("back", "backkk");
i = 1;
Toast.makeText(getApplicationContext(), "back"+i,100).show();
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
i think you want to perform an opration when back key is pressed in Fragment
So Create a public method in fragment
and from Fragment activity class , override onBackkeypressed call that method
example
#Override
public void onBackPressed() {
DetailFragment fragment = (DetailFragment) getFragmentManager()
.findFragmentById(R.id.listfragment);
fragment.doSomthing()
}

how to change the tab colour in ViewPager from black to white

I want to change the tab colour in the ViewPager. I search but didn't find any good solution for it. I want to change the tab colour from default that is from black to white. Is their any way to do this programatically for i have to modify style.xml. I don't know how to do as i am new to android. plz somebody help
public class HomeActivity extends FragmentActivity implements ActionBar.TabListener
{
public ViewPager viewPager;
private PageAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Calculate EMI", "EMI Schedule"};
String TabFragmentSchedule;
public void setTabFragmentSchedule(String t){
TabFragmentSchedule = t;
}
public String getTabFragmentSchedule(){
return TabFragmentSchedule;
}
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#0F7BFF"));
actionBar.setBackgroundDrawable(colorDrawable);
mAdapter = new PageAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs)
{
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
//method that is called to swipe viewpage on button click from calculation fragment
public void switchToFragmentSchedule(){
// viewPager.setCurrentItem(1);
viewPager.setCurrentItem(1, false);
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
You should style your theme in your xml files. You can find all the information for this task here.
If you need to change it programmatically, you can use
mActionBar.setBackgroundDrawable(new ColorDrawable(0xffffffff));
mActionBar.setDisplayShowTitleEnabled(false);
mActionBar.setDisplayShowTitleEnabled(true);
As a beginner, you might also find this styling tool very helpful.

Tab+swipe Gesture, Custom grid view, Fragments

Well I have this app with swipe gesture functionality between fragments which they contains images which are located in the resources folders .. but when i swipe left and right I noticed that the performance is bad and slow .. Any suggestions to improve it?
And here is the header of the main activity code :
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = {"Roman Curtain", "Roller Blinds", "Wooden Blinds" ,"Kitchen Blinds"};
//
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
and here is tab pager adapter :
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new RomanCurtains();
case 1:
return new RollerBlinds();
case 2:
return new WoodenBlinds();
case 3:
return new KitchenBlinds();
// case 4:
// return new KitchenBlinds();
}
return null;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 4;
}
}
There may be a two reason for the bad performance
Your images are too big and takes too much space in the memory
You can put the images in assets folder and then use Picasso library to use them. It will improve the performance.
https://github.com/square/picasso
All the pages in the ViewPager is in the memory if you use PagerAdapter or FragmentPagerAdapter.
If you use FragmentStatePagerAdapter only the visible page and the
pages adjacent to that page are kept in the memory. This will
improve the performance too.
Use ViewPager.setOffscreenPageLimit(int limit)

Android sherlock FragmentTab clear data

public class PropertyAddActivity extends SherlockFragmentActivity implements OnPageChangeListener, TabListener {
private String TAG="AddActivity: ";
private FileCache fileCache;
private ViewPager mPager;
private ActionBar ab;
private static final int COUNT = 3;
static ArrayList<Integer> mSelectedPropertyType = new ArrayList<Integer>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.property_add);
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
mPager.setOnPageChangeListener(this);
ab = getSupportActionBar();
ab.setDisplayShowTitleEnabled(false);
ab.setDisplayShowHomeEnabled(false);
ab.setNavigationMode(NAVIGATION_MODE_TABS);
GlobalProperty.getInstance().product=new PropertyAdd();
ab.addTab(ab.newTab().setText("Property").setTabListener(this));
ab.addTab(ab.newTab().setText("Property Detail 1").setTabListener(this));
ab.addTab(ab.newTab().setText("Property Detail 2").setTabListener(this));
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
//listAttachImage.clear();
mSelectedPropertyType.clear();
super.onDestroy();
}
#Override
public void onPageScrolled(int position, float positionOffset,int positionOffsetPixels) {
//AppLog.logString(TAG+"onPageScrolled");
}
#Override
public void onPageSelected(int position) {
//AppLog.logString(TAG+"onPageSelected position"+position);
getSupportActionBar().setSelectedNavigationItem(position);
}
#Override
public void onPageScrollStateChanged(int state) {
//AppLog.logString(TAG+"onPageScrollStateChanged ");
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
//AppLog.logString(TAG+"onTabSelected position: "+tab.getPosition());
mPager.setCurrentItem(tab.getPosition());
//ft.replace(R.id.fragment_container, fragment);
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
//AppLog.logString(TAG+"onTabUnselected");
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
//AppLog.logString(TAG+"onTabReselected");
}
public class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
//AppLog.logString(TAG+"COUNT: "+COUNT);
return COUNT;
}
#Override
public Fragment getItem(int position) {
Fragment f = new Fragment();
AppLog.logString(TAG+"position: "+position);
switch (position) {
case 0:
AppLog.logString(TAG+"FRAGGGG1111111");
f = Property1AddFragmentActivity.newInstance(position);
break;
case 1:
AppLog.logString(TAG+"FRAGGGG2222222");
f = Property2AddFragmentActivity.newInstance(position);
break;
case 2:
AppLog.logString(TAG+"FRAGGGGG333333");
f = Property3AddFragmentActivity.newInstance(position);
break;
default:
AppLog.logString(TAG+"Default");
break;
}
return f;
}
}}
i had add add 3 tab Now the problem is that when i set some data in tab1 view and move to tab2 the reselect tab1 the data on tab1 is as it is but when i move to tab3 to tab1 then tab1 data is clear. so cany anyof you have idea to solve this?
At the moment your code appears to be partly action bar tabs and partly ordinary fragments.
If you are trying to use action bar tabs then you need a tab listener. In onTabSelected you should add/replace and in onTabUnselected you should remove.
Please see http://developer.android.com/guide/topics/ui/actionbar.html
section "adding navigation tabs" for a good example.

Categories

Resources