how to add multiple ViewPagers into a Vertical ScrollView of an Activity? - android

I am trying to implement an Activity, which contains 2 ViewPagers. Each ViewPager should have swipe independently. So i am taking ScrollView as my patent layout which have a LinearLayout as Chaild. I have added ViewPagers to that Linear layout. But only 1st ViewPager is getting Displayed and second one is not getting displayed. i have goggled alot for this and tryed with different solutions Here. but nothing worked for me.
here is my Xml.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffffff"
android:orientation="vertical"
android:weightSum="4">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="30dip"
android:text="trying to add First Fragment list"
android:layout_weight="1" />
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="fill_parent"
android:layout_height="200dip"
android:layout_weight="1" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dip"
android:text="trying to add second Fragment list"
android:layout_weight="1" />
<android.support.v4.view.ViewPager
android:id="#+id/viewPager1"
android:layout_width="fill_parent"
android:layout_height="200dip"
android:layout_marginTop="30dip"
android:layout_weight="1" />
</LinearLayout>
</ScrollView>
and here is my activity.java
public class MainActivity extends FragmentActivity {
private ViewPager _mViewPager, _mViewPager1;
private ViewPagerAdapter _adapter, _adapter1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpView();
// setTab();
}
private void setUpView() {
_mViewPager = (ViewPager) findViewById(R.id.viewPager);
_adapter = new ViewPagerAdapter(getApplicationContext(),
getSupportFragmentManager());
_mViewPager.setAdapter(_adapter);
_mViewPager.setCurrentItem(0);
_mViewPager1 = (ViewPager) findViewById(R.id.viewPager1);
_adapter1 = new ViewPagerAdapter(getApplicationContext(),
getSupportFragmentManager());
_mViewPager1.setAdapter(_adapter1);
_mViewPager1.setCurrentItem(0);
}
private void setTab() {
_mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageScrollStateChanged(int position) {
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
switch (position) {
case 0:
findViewById(R.id.first_tab).setVisibility(View.VISIBLE);
findViewById(R.id.second_tab).setVisibility(View.INVISIBLE);
findViewById(R.id.second_tab1).setVisibility(View.INVISIBLE);
findViewById(R.id.second_tab2).setVisibility(View.INVISIBLE);
findViewById(R.id.second_tab3).setVisibility(View.INVISIBLE);
break;
case 1:
findViewById(R.id.first_tab).setVisibility(View.INVISIBLE);
findViewById(R.id.second_tab).setVisibility(View.VISIBLE);
findViewById(R.id.second_tab1).setVisibility(View.INVISIBLE);
findViewById(R.id.second_tab2).setVisibility(View.INVISIBLE);
findViewById(R.id.second_tab3).setVisibility(View.INVISIBLE);
break;
case 2:
findViewById(R.id.first_tab).setVisibility(View.INVISIBLE);
findViewById(R.id.second_tab).setVisibility(View.INVISIBLE);
findViewById(R.id.second_tab1).setVisibility(View.VISIBLE);
findViewById(R.id.second_tab2).setVisibility(View.INVISIBLE);
findViewById(R.id.second_tab3).setVisibility(View.INVISIBLE);
break;
case 3:
findViewById(R.id.first_tab).setVisibility(View.INVISIBLE);
findViewById(R.id.second_tab).setVisibility(View.INVISIBLE);
findViewById(R.id.second_tab1).setVisibility(View.INVISIBLE);
findViewById(R.id.second_tab2).setVisibility(View.VISIBLE);
findViewById(R.id.second_tab3).setVisibility(View.INVISIBLE);
break;
case 4:
findViewById(R.id.first_tab).setVisibility(View.INVISIBLE);
findViewById(R.id.second_tab).setVisibility(View.INVISIBLE);
findViewById(R.id.second_tab1).setVisibility(View.INVISIBLE);
findViewById(R.id.second_tab2).setVisibility(View.INVISIBLE);
findViewById(R.id.second_tab3).setVisibility(View.VISIBLE);
break;
}
}
});
}
}
with this out put is coming like the below screenshot.
second View Pager is not displayed.
please help me if you have any solutions.

Related

ViewPager with FragmentPagerAdapter Not showing Content

I am trying to use Fragment
This is my activity class:
public class InterfaceAct extends FragmentActivity implements View.OnClickListener {
private ViewPager mViewPager;
private List<Fragment> datas;
private ViewPagerFragmentAdapter viewPagerFragmentAdapter;
private LinearLayout mLLHome,mLLTimer,mLLEdit,mLLAbout;
private ImageView mImageViewHome,mImageViewTimer,mImageViewEdit,mImageViewAbout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activiy_interface);
initDatas();// Init Data For Fragments
initView();// Init Component
initEvent();// Sign for Click Listener
viewPagerFragmentAdapter=new ViewPagerFragmentAdapter(getSupportFragmentManager(),datas);// Init Adpater Class
mViewPager.setAdapter(viewPagerFragmentAdapter);
}
private void initDatas() {
datas=new ArrayList<Fragment>();
datas.add(new MyFragment1());
datas.add(new MyFragment2());
datas.add(new MyFragment3());
datas.add(new MyFragment4());
}
private void initEvent() {
mLLHome.setOnClickListener(this);
mLLTimer.setOnClickListener(this);
mLLEdit.setOnClickListener(this);
mLLAbout.setOnClickListener(this);
mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {//ViewPager Scrolling Switch Listner
#Override
public void onPageSelected(int arg0) {
int currentItem=mViewPager.getCurrentItem();
resetImag();
switch (currentItem) {
case 0:
mImageViewHome.setImageResource(R.drawable.home_yes);
break;
case 1:
mImageViewTimer.setImageResource(R.drawable.timer_yes);
break;
case 2:
mImageViewEdit.setImageResource(R.drawable.edit_yes);
break;
case 3:
mImageViewAbout.setImageResource(R.drawable.about_yes);
break;
default:
break;
}
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
private void initView() {
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mLLHome = (LinearLayout) findViewById(R.id.ll_home);
mLLTimer = (LinearLayout) findViewById(R.id.ll_timer);
mLLEdit = (LinearLayout) findViewById(R.id.ll_edit);
mLLAbout = (LinearLayout) findViewById(R.id.ll_about);
mImageViewHome = (ImageView) findViewById(R.id.img_home);
mImageViewTimer = (ImageView) findViewById(R.id.img_timer);
mImageViewEdit = (ImageView) findViewById(R.id.img_edit);
mImageViewAbout = (ImageView) findViewById(R.id.img_about);
}
#Override
public void onClick(View v) {
resetImag();
switch (v.getId()) {
case R.id.ll_home:
mViewPager.setCurrentItem(0);
mImageViewHome.setImageResource(R.drawable.home_yes);
break;
case R.id.ll_timer:
mViewPager.setCurrentItem(1);
mImageViewTimer.setImageResource(R.drawable.timer_yes);
break;
case R.id.ll_edit:
mViewPager.setCurrentItem(2);
mImageViewEdit.setImageResource(R.drawable.edit_yes);
break;
case R.id.ll_about:
mViewPager.setCurrentItem(3);
mImageViewAbout.setImageResource(R.drawable.about_yes);
break;
default:
break;
}
}
private void resetImag() {// Reset Picture
mImageViewHome.setImageResource(R.drawable.home_no);
mImageViewTimer.setImageResource(R.drawable.timer_no);
mImageViewEdit.setImageResource(R.drawable.edit_no);
mImageViewAbout.setImageResource(R.drawable.about_no);
}
}
The ViewPagerFragmentAdapter Looks like this:
public class ViewPagerFragmentAdapter extends FragmentStatePagerAdapter {
private List<Fragment> datas;
public ViewPagerFragmentAdapter(FragmentManager fm,List<Fragment> datas) {
super(fm);
this.datas=datas;
}
#Override
public Fragment getItem(int position) {// Return View Object
return datas.get(position);
}
#Override
public int getCount() {// Return View Num
return datas.size();
}
#Override
public Object instantiateItem(ViewGroup container, int position) {// Init View
return super.instantiateItem(container, position);
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {// Destroy View
super.destroyItem(container, position, object);
}
}
The layout for the page, it includes a viewpager and also a menu bar in linear layout:
?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="449dp"
android:layout_weight="1"></android.support.v4.view.ViewPager>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#ffffffff"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/ll_home"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/img_home"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#0000"
android:src="#drawable/home_yes" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home"
android:textColor="#b6b3b3" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll_timer"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/img_timer"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#0000"
android:src="#drawable/timer_no" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Timer"
android:textColor="#b6b3b3" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll_edit"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/img_edit"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#0000"
android:src="#drawable/edit_no" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edit"
android:textColor="#b6b3b3" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll_about"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/img_about"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#0000"
android:src="#drawable/about_no" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="About"
android:textColor="#b6b3b3" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
And This is one of the fragment:
public class MyFragment1 extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.tab1,null);
}
}
I have four fragment prepared for viewpager(they are basiclly the same but with differnt layout), here is what the tab1 layout might look like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Chat"
android:textSize="30sp" >
</TextView>
</LinearLayout>
And I only got the menu bar when I run it, the viewpager is not set and I don't know why
Remove android:layout_weight="1" <Viewpager> and add android:orientation="vertical" to Linear layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="449dp"
></android.support.v4.view.ViewPager>
//-----------

Radiobuttons becomes unclickable when used with viewpager in android

I am using viewpager and segmented group to create swipeable fragments. These segmented group is actually based on RadioGroup.
What I want is that on clicking these segments(actually radiobuttons) the respective pages should be loaded. The problem is these radiobuttons are not clickable. But as soon as I comment out this line
viewPager.setAdapter(adapter)
These radiobuttons becomes clickable. I tried searching for the solution but could not resolve it. Also I do not want to use TabLayout because I want this UI somewhere in the middle of screen.
Here's the code -
public class MainActivity extends AppCompatActivity implements
ViewPager.OnPageChangeListener {
SegmentedGroup segmentedGroup;
ViewPager viewPager;
MyViewAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
viewPager = (ViewPager) findViewById(R.id.pager);
adapter = new MyViewAdapter(getSupportFragmentManager());
segmentedGroup = (SegmentedGroup) findViewById(R.id.segmentedgroup);
viewPager.setAdapter(adapter);
segmentedGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i) {
case R.id.rb1:
Toast.makeText(getApplicationContext(), "1", Toast.LENGTH_SHORT).show();
viewPager.setCurrentItem(0);
break;
case R.id.rb2:
Toast.makeText(getApplicationContext(), "2", Toast.LENGTH_SHORT).show();
viewPager.setCurrentItem(1);
break;
case R.id.rb3:
Toast.makeText(getApplicationContext(), "3", Toast.LENGTH_SHORT).show();
viewPager.setCurrentItem(2);
break;
}
}
});
viewPager.addOnPageChangeListener(this);
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(0);
segmentedGroup.check(segmentedGroup.getChildAt(0).getId());
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
switch (position) {
case 0:
segmentedGroup.check(R.id.rb1);
break;
case 1:
segmentedGroup.check(R.id.rb2);
break;
case 2:
segmentedGroup.check(R.id.rb3);
break;
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
class MyViewAdapter extends FragmentPagerAdapter {
public MyViewAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position)
{
case 0: return new FragmentOne();
case 1: return new FragmentTwo();
case 2: return new FragmentThree();
}
return null;
}
#Override
public int getCount() {
return 3;
}
}
}
Here is the XML-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<LinearLayout
android:id="#+id/someView"
android:layout_width="wrap_content"
android:layout_height="105px"
android:orientation="horizontal"></LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/someView"
android:gravity="center_horizontal">
<info.hoang8f.android.segmented.SegmentedGroup xmlns:segmented="http://schemas.android.com/apk/res-auto"
android:id="#+id/segmentedgroup"
android:layout_width="match_parent"
android:layout_height="105px"
android:orientation="horizontal"
segmented:sc_border_width="1dp"
segmented:sc_corner_radius="10dp"
segmented:sc_tint_color="#36578B">
<RadioButton
android:id="#+id/rb1"
style="#style/RadioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ONE" />
<RadioButton
android:id="#+id/rb2"
style="#style/RadioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TWO" />
<RadioButton
android:id="#+id/rb3"
style="#style/RadioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="THREE" />
</info.hoang8f.android.segmented.SegmentedGroup>
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</RelativeLayout>
I am beginner and any help is appreciated.
I figured out the answer after playing around with android layout. In android studio layout editor, the viewpager was occupying the whole screen. After adding android:layout_below parameter in viewpager it worked.

how to show a textview on top of viewpager actionbar and under the supportactionbar

I am trying to show a TextView between the main actionbar and viewpager action bar but it is showing under the viewpager action bar. here is my code
promotions.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/promotionheader"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:gravity="center"
android:layout_alignParentTop="true"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:orientation="vertical">
<TextView
android:id="#+id/txtViewHeading"
android:text="#string/heading_promotion"
android:textColor="#color/white"
android:textSize="18sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/border_promotion"
android:padding="10dp"
android:gravity="center_vertical|center_horizontal" />
</LinearLayout>
<android.support.v4.view.ViewPager xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/promotionheader"
tools:context=".Promotions"></android.support.v4.view.ViewPager>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dip"
android:layout_weight="1"
android:gravity="center"
android:layout_alignParentBottom="true"
android:background="#color/colorPrimary"
android:orientation="horizontal">
<ImageButton
android:id="#+id/btnStores"
style="?android:attr/imageButtonStyle"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#color/colorPrimary"
android:paddingLeft="10dp"
android:paddingRight="15dp"
android:onClick="startSelectStore"
android:layout_gravity="center_horizontal|center"
android:src="#drawable/btn_home" />
<ImageButton
android:id="#+id/btnCoupons"
style="?android:attr/imageButtonStyle"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="15dp"
android:background="#color/colorPrimary"
android:onClick="startCouponActivity"
android:layout_gravity="center_horizontal|center"
android:src="#drawable/btn_coupon" />
<ImageButton
android:id="#+id/btnNotifications"
style="?android:attr/imageButtonStyle"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="15dp"
android:background="#color/colorPrimary"
android:onClick="startNotificationActivity"
android:layout_gravity="center_horizontal|center"
android:src="#drawable/btn_notification" />
<ImageButton
android:id="#+id/btnAbout"
style="?android:attr/imageButtonStyle"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:paddingLeft="10dp"
android:background="#color/colorPrimary"
android:onClick="startAboutActivity"
android:layout_gravity="center_horizontal|center"
android:src="#drawable/btn_about" />
</LinearLayout>
</RelativeLayout>
Promotions.java
public class Promotions extends AppCompatActivity implements ActionBar.TabListener {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
ArrayList<String> shopList;
// TabHost tabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.promotions);
LayoutInflater layoutInflater = LayoutInflater.from(this);
View customActionBar = layoutInflater.inflate(R.layout.actionbar_promotion, null);
//-- Set Custom icon in ActionBar
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(customActionBar);
ImageButton ibItem1 = (ImageButton) customActionBar.findViewById(R.id.imgBtnSettings);
ibItem1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(Promotions.this, SelectStore.class));
}
});
// Set up the action bar.
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//View customActionBar1 = layoutInflater.inflate(R.layout.actionbar_promotion_tab, null);
//actionBar.setCustomView(customActionBar1);
actionBar.invalidateOptionsMenu();
//-- Get value from intent
Intent intent = getIntent();
shopList = (ArrayList<String>) intent.getSerializableExtra("selectedStoreList");
// 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(shopList.get(i)).setTabListener(this));
}
}
public void startSelectStore(View view) {
Intent intent = new Intent(Promotions.this, SelectStore.class);
startActivity(intent);
}
public void startCouponActivity(View view) {
Intent intent = new Intent(Promotions.this, Coupons.class);
startActivity(intent);
}
public void startNotificationActivity(View view) {
Intent intent = new Intent(Promotions.this, Notifications.class);
startActivity(intent);
}
public void startAboutActivity(View view) {
Intent intent = new Intent(Promotions.this, AboutStore.class);
startActivity(intent);
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
Bundle args = new Bundle();
switch (position) {
case 0:
fragment = new PromotionFragment();
args.putInt("fragNum", 1);
fragment.setArguments(args);
break;
case 1:
fragment = new PromotionFragment();
args.putInt("fragNum", 2);
fragment.setArguments(args);
break;
case 2:
fragment = new PromotionFragment();
args.putInt("fragNum", 3);
fragment.setArguments(args);
break;
case 3:
fragment = new PromotionFragment();
args.putInt("fragNum", 4);
fragment.setArguments(args);
break;
case 4:
fragment = new PromotionFragment();
args.putInt("fragNum", 5);
fragment.setArguments(args);
break;
case 5:
fragment = new PromotionFragment();
args.putInt("fragNum", 6);
fragment.setArguments(args);
break;
case 6:
fragment = new PromotionFragment();
args.putInt("fragNum", 7);
fragment.setArguments(args);
break;
case 7:
fragment = new PromotionFragment();
args.putInt("fragNum", 8);
fragment.setArguments(args);
break;
case 8:
fragment = new PromotionFragment();
args.putInt("fragNum", 9);
fragment.setArguments(args);
break;
case 9:
fragment = new PromotionFragment();
args.putInt("fragNum", 10);
fragment.setArguments(args);
break;
}
return fragment;
}
#Override
public int getCount() {
if (shopList.isEmpty()) {
return 0;
} else {
return shopList.size();
}
}
}
}
My UI looks like
I want Promotions & Events to be on top of ALL Stores
Someone pls help...
you can try this :---
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fab="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Dashboard">
<!-- our toolbar -->
<LinearLayout
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:alpha="3"
android:background="#5d4292"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/menu"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="left|center_vertical"
android:paddingLeft="10dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/menu_icon" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
android:textColor="#ffffff"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/search"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="right|center_vertical"
android:paddingRight="10dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/search_icon" />
</LinearLayout>
</LinearLayout>
<!-- our tablayout to display tabs -->
<android.support.design.widget.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/toolbar"
android:alpha="3"
android:background="#5d4292"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<!-- View pager to swipe views -->
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tabLayout" />
</RelativeLayout>

Unable to load the correct tab on the corresponding index on getItem in android

This is my current scenario:
I have 4 Tabsin my acivity. I am calling a new API to load data from the server on each tab. The problem is that the correct fragment is not loading on the correct index. I have googled here and here but the solutions provided there don't help me.My codes are as follows:
public class OffersTab extends FragmentActivity implements OnTabChangeListener,
OnPageChangeListener {
private TabsPagerAdapter mAdapter;
private ViewPager mViewPager;
private TabHost mTabHost;
ImageView imv_home;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.offers);
imv_home = (ImageView) findViewById(R.id.imv_home);
imv_home.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}
});
mViewPager = (ViewPager) findViewById(R.id.viewpager);
// Tab Initialization
initialiseTabHost();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
// Fragments and ViewPager Initialization
mViewPager.setAdapter(mAdapter);
mViewPager.setOnPageChangeListener(OffersTab.this);
}
// Method to add a TabHost
private static void AddTab(OffersTab activity, TabHost tabHost,
TabHost.TabSpec tabSpec) {
tabSpec.setContent(new MyTabFactory(activity));
tabHost.addTab(tabSpec);
}
// Manages the Tab changes, synchronizing it with Pages
public void onTabChanged(String tag) {
int pos = this.mTabHost.getCurrentTab();
this.mViewPager.setCurrentItem(pos);
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
// Manages the Page changes, synchronizing it with Tabs
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
int pos = this.mViewPager.getCurrentItem();
this.mTabHost.setCurrentTab(pos);
}
#Override
public void onPageSelected(int arg0) {
}
// Tabs Creation
private void initialiseTabHost() {
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
// TODO Put here your Tabs
OffersTab.AddTab(this, this.mTabHost, this.mTabHost
.newTabSpec("Ladies").setIndicator("Ladies"));
OffersTab.AddTab(this, this.mTabHost, this.mTabHost
.newTabSpec("Babies").setIndicator("Babies"));
OffersTab.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Girls")
.setIndicator("Girls"));
OffersTab.AddTab(
this,
this.mTabHost,
this.mTabHost.newTabSpec("Accessories").setIndicator(
"Accessories"));
mTabHost.setOnTabChangedListener(this);
}
}
Adapter class
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem( int index) {
Log.i("index", "" + index);
switch (index) {
case 0:
// Top Rated fragment activity
return new LadiesFragment();
case 1:
// Games fragment activity
return new BabyFragment();
case 2:
// Movies fragment activity
return new GirlsFragment();
case 3:
// Movies fragment activity
return new AccesoryFragment();
default:
break;
}
return new LadiesFragment();
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 4;
}
}
xml layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/yyy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="horizontal" >
<RelativeLayout
android:id="#+id/rel_top_header_exp"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:background="#color/orange" >
<LinearLayout
android:id="#+id/lnr_top_home"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:gravity=""
android:weightSum="3" >
<ImageView
android:id="#+id/imv_srch_home"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentLeft="true"
android:layout_gravity="center_vertical"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:src="#drawable/search_icon" />
<ImageView
android:id="#+id/imv_wish_home"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentLeft="true"
android:layout_gravity="center_vertical"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:src="#drawable/wish_list_icon" />
<ImageView
android:id="#+id/imv_cart_home_exp"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentLeft="true"
android:layout_gravity="center_vertical"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:src="#drawable/my_bag_icon" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#+id/imv_home"
android:text="#string/app_name"
android:textSize="#dimen/text16" />
<ImageView
android:id="#+id/imv_home"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:src="#drawable/back" />
</RelativeLayout>
</LinearLayout>
<TabHost
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/yyy" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
</LinearLayout>
</TabHost>
</RelativeLayout>
On your fragment/activity implementing the TabLayout.OnTabSelectedListener, use this:
#Override
public void onTabSelected(TabLayout.Tab tab) {
pager.setCurrentItem(tab.getPosition());
}

findViewById always return null using FragmentStatePagerAdapter

I am trying to update some views that place in a ViewPager in some fragment,
and every time I am using findViewById I am getting null back.
I tried various of solutions that I find in here (stack overflow) and nothing help.
here is all the code :
activity_main.xml
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
MainActivity.java:
PageManager _pageManager;
ViewPager _viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_viewPager = (ViewPager) findViewById(R.id.viewpager);
_pageManager = new PageManager(getSupportFragmentManager());
_viewPager.setAdapter(_pageManager);
_imageView=(ImageView)findViewById(R.id.PictureBox);
PageManager.java
public class PageManager extends FragmentStatePagerAdapter {
final int NUMBER_OF_PAGES = 3;
public PageManager(android.support.v4.app.FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new CountdownPage();
case 1:
return new VideosPage();
case 2:
return new SeasonSummary();
default:
return null;
}
}
#Override
public int getCount() {
return NUMBER_OF_PAGES;
}
}
CountdownPage.java
public class CountdownPage extends Fragment {
int resId=0;
ImageView _imageView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container==null)
ShowMessage.GetInstance(null).ShowMessage("error","container");
return inflater.inflate(R.layout.countdown_page,container,false);
}
}
countdown_page.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#color/Black"
android:layout_width="match_parent"
android:id="#+id/countdownLayout"
android:layout_height="match_parent">
<TextView
style="#style/HeaderStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Countdown"
android:gravity="center"
android:id="#+id/CountdownHeader"
/>
<ImageView
android:layout_width="320dp"
android:layout_height="320dp"
android:id="#+id/PictureBox"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:onClick="testF"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:id="#+id/counterTextView"
android:gravity="center"
android:textColor="#FFFF"
/>
</LinearLayout>
I am trying to solve this for hours
Thanks a lot,
Or Yaacov
Your ImageView(id is PictureBox) is in the 'countdown_page' layout xml not 'activity_main' layout xml ,So you should not get ImageView through findViewbyId(int) in 'onCreate()' method in Activity, The ImageView is belong to CountdownPage fragment.
had the same problem with getActivity().findViewById. Try getView().findViewById instead

Categories

Resources