Android viewpager not showing? - android

I have this layout
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/header_cover_image"
android:layout_width="match_parent"
android:layout_height="150dp"
android:scaleType="centerCrop"
android:background="#color/com_facebook_button_background_color_pressed" />
<refractored.controls.CircleImageView
android:id="#+id/imageProfile"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_below="#+id/header_cover_image"
android:layout_centerHorizontal="true"
android:layout_marginTop="-60dp"
android:layout_gravity="center_horizontal"
android:elevation="5dp"
android:padding="20dp"
android:scaleType="centerCrop" />
<RelativeLayout
android:id="#+id/profile_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/header_cover_image"
android:background="#ebca0707"
android:elevation="4dp"
android:paddingBottom="24dp">
<ImageView
android:id="#+id/add_friend"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:src="#drawable/played" />
<ImageView
android:id="#+id/drop_down_option_menu"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:src="#drawable/wins"
android:layout_width="30dp"
android:layout_toLeftOf="#+id/drop_down_option_menu"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp" />
<TextView
android:id="#+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="76dp"
android:textColor="#fff"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:id="#+id/userStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/user_name"
android:text="Status"
android:textColor="#fff"
android:textSize="14sp"
android:layout_marginLeft="16dp"
android:layout_marginTop="15dp"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/profilePager"
>
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/profile_layout"
android:elevation="6dp"
android:background="?android:attr/colorPrimary"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_below="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
The last LinearLayout shows the Tabs but when i switch them there is no viewpager data whatsoever . I tried creating another layout for the tablayout but it still wont work .. Im calling this from a fragment as follows :
public class ProfileFragment : Fragment
{
private static Context _context;
private static Xamarin.Facebook.Profile _profile;
private CircleImageView imgProfile;
private TextView userName;
private TabLayout tabLayout;
private ViewPager viewpager;
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your fragment here
}
public static Fragment NewInstance(Activity context , Xamarin.Facebook.Profile profile)
{
_context = context;
_profile = profile;
Fragment fragment = new ProfileFragment();
return fragment;
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.Inflate(Resource.Layout.fragmentProfile, container, false);
imgProfile = view.FindViewById<CircleImageView>(Resource.Id.imageProfile);
tabLayout =view.FindViewById<TabLayout>(Resource.Id.tab_layout);
viewpager = view.FindViewById<ViewPager>(Resource.Id.pager);
userName = view.FindViewById<TextView>(Resource.Id.user_name);
userName.Text = _profile.Name;
Picasso.With(_context).Load(_profile.GetProfilePictureUri(180, 180)).Into(imgProfile);
FnInitTabLayout();
return view;
}
void FnInitTabLayout()
{
tabLayout.SetTabTextColors(Android.Graphics.Color.Aqua, Android.Graphics.Color.AntiqueWhite);
//Fragment array
var fragments = new Fragment[]
{
new ActivityFragment(),
new SportsFragment(),
new AboutFragment()
};
//Tab title array
var titles = CharSequence.ArrayFromStringArray(new[] {
"Feed",
"Sports",
"About",
});
//viewpager holding fragment array and tab title text
viewpager.Adapter = new ProileTabsAdapter(FragmentManager, fragments, titles);
tabLayout.SetupWithViewPager(viewpager);
}
}
When i put it inside an activity and without the above layout only the tab and the viewpager they work perfectly but in this scenario not . Can anyone think of what am i doing wrong ?
EDIT :
Here is the pager adapter
public class ProileTabsAdapter : FragmentPagerAdapter
{
private readonly Fragment[] fragments;
private readonly ICharSequence[] titles;
public ProileTabsAdapter(FragmentManager fm, Fragment[] fragments, ICharSequence[] titles) : base(fm)
{
this.fragments = fragments;
this.titles = titles;
}
public override int Count
{
get
{
return fragments.Length;
}
}
public override Fragment GetItem(int position)
{
return fragments[position];
}
public override ICharSequence GetPageTitleFormatted(int position)
{
return titles[position];
}
}

Use android:fillViewport="true" into your ScrollView.
wrap_content of view pager won't work inside Scrollview.

ViewPager doesn't support width="wrap_content" or height="wrap_content" attrs.
Solutions here : I am unable to have ViewPager WRAP_CONTENT
You can also use this library : WCViewPager (Wrap Content ViewPager)

Related

Viewpager2 fragment freezes custom spinner widget when returned after swiping

I am using a custom material spinner from this library.
So here is the problem when using a Viewpager2(implementation "androidx.viewpager2:viewpager2:1.0.0") in fragments.
Each fragment consists of an TextInputEditText inside a TextInputLayout.
I have 3 fragments and all the 3 have this custom spinner inside, when navigating to third fragment(by swiping) and returning back to the first fragment the Material Spinner freezes that is on clicking it, does not show the items, the onClick freezes. It enables only when the text field in that layout is focussed and the keyboard pop's up.
Here is the code for the ViewPager2 and those 3 fragments,
public class MainActivity extends AppCompatActivity {
ActivityMainBinding activityMainBinding;
ViewPager2Adapter mViewPager2Adapter;
public static String TAG = MainActivity.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
mViewPager2Adapter=new ViewPager2Adapter(this);
mViewPager2Adapter.addFragment(FragmentOne.newInstance(),"One");
mViewPager2Adapter.addFragment(FragmentTwo.newInstance(),"Two");
mViewPager2Adapter.addFragment(FragmentThree.newInstance(),"Other ");
activityMainBinding.viewPager2.setAdapter(mViewPager2Adapter);
}
}
Inside FragmentOne,the same is for rest of the fragments
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
viewBinding= DataBindingUtil.inflate(inflater,
R.layout.fragment_one, container, false);
viewBinding.matSpinOne.setItems(getItems());
viewBinding.matSpinTwo.setAdapter(getItems());
viewBinding.matSpinThree.setItems(getItems());
return viewBinding.getRoot();
}
public ArrayList<String> getItems()
{
ArrayList<String> itemList=new ArrayList<>();
itemList.add("Android OS");
itemList.add("Windows");
itemList.add("Linux ");
itemList.add("ios");
itemList.add("Mac OS");
itemList.add("Debian");
return itemList;
}
FragmentOne layout XML
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:clickable="true"
android:focusable="true"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_margin="10dp"
android:text="One Spinner"
/>
<com.jaredrummler.materialspinner.MaterialSpinner
android:id="#+id/matSpinOne"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_margin="10dp"
android:text="Two Spinner"
/>
<com.jaredrummler.materialspinner.MaterialSpinner
android:id="#+id/matSpinTwo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_margin="10dp"
android:text="Three Spinner"
/>
<com.jaredrummler.materialspinner.MaterialSpinner
android:id="#+id/matSpinThree"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Text Layout 1"
>
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
</ScrollView>
</FrameLayout>
ViewPager Adapter code,
public class ViewPager2Adapter extends FragmentStateAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPager2Adapter(#NonNull FragmentActivity fragmentActivity) {
super(fragmentActivity);
}
#NonNull
#Override
public Fragment createFragment(int position) {
return mFragmentList.get(position);
}
#Override
public int getItemCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
}
Here are my observations, when the viewpager is first displayed this line of code inside MaterialSpinner.java,
Line no 524: isLaidOut() returns true for the first time, when you swipe back to the third fragment and then come to first the isLaidOut() returns false which freezes the widget why does it return false if it has been laid already ???
The whole problem is with these lines,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
isLaidOut = isLaidOut();
} else {
isLaidOut = getWidth() > 0 && getHeight() > 0;
}
My Questions
If the View has already laid out once in the fragment why does isLaidOut() return false when the fragment's view is re-created in the ViewPager2 ?
Can I go ahead and just use isLaidOut = getWidth() > 0 && getHeight() > 0; this condition for devices above Lollipop ? (Not sure why that function was for)

Android TabLayout not showing

I'm trying to create an app which uses three different tabs. I followed this video tutorial to get started: Youtube - Android Tab Tutorial [Android Studio Tab Fragments]
I built the app and checked it out on my phone. The three tabs show up and are working properly (I can switch between them).
I figured all I needed to do now was to add widgets to the different layouts, so I did that and re-built the app and tried it again. But it looks just the same. I don't see any of the controls I just added.
Here's the code:
SectionsPageAdapter:
public class SectionsPageAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public void addFragment(Fragment fragment, String title){
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
public SectionsPageAdapter(FragmentManager fm) {
super(fm);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
}
Tab_Alarm:
public class Tab_Alarm extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab_alarm,container,false);
return view;
}
}
MainActivity:
public class MainActivity extends AppCompatActivity {
private SectionsPageAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionsPagerAdapter = new SectionsPageAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
setupViewPager(mViewPager);
TabLayout tabLayout = (TabLayout)findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
private void setupViewPager(ViewPager viewPager){
SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
adapter.addFragment(new Tab_Alarm(), "Alarm");
adapter.addFragment(new Tab_Countdown(), "Countdown");
adapter.addFragment(new Tab_Timer(), "Timer");
viewPager.setAdapter(adapter);
}
}
I have one class for each Tab, but the all look the same (except the point at different xml files.)
Here's my xml file for the TabLayout which isn't showing up:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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:id="#+id/Layout_Main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/Layout_Container_List"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:orientation="vertical"
android:paddingBottom="25dp"
android:paddingTop="25dp"
app:layout_constraintBottom_toTopOf="#+id/Layout_Container_Buttons"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1">
<ListView
android:id="#+id/ListView_AlarmList"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:id="#+id/Layout_Container_Buttons"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1">
<Button
android:id="#+id/btn_AddAlarm"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/add_alarm" />
<Button
android:id="#+id/btn_DeleteAlarm"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/delete_alarm" />
</LinearLayout>
All the tabs remain empty while running the app, despite one of them having stuff in it.
I found out what the problem was, I'm posting it here in case someone else has the same problem:
I had changed the "default layout" of the fragment from Linear (vertical) to Constraint. Apparently whatever it is that receives the return data from onCreateView can't handle Constraint Layouts.
I changed it back to a Linear Layout and now it works.

Android ViewPager with FragmentPageAdapter - only last fragment page is visible

I am using a ViewPager with a FragmentPagerAdapter. Each page is an instance of the same Fragment class, I am always using a fixed number of 7 pages (days of the week starting with today). Debugging shows that all Fragments are getting created and have the correct data at the time of creation. One interesting thing is although the adapter is creating the fragments in the order 0, 1, 2...6, the onCreateView of the fragments is called in the order 6, 0, 1, 2...5.
When run the app and open this activity, the first fragment page is blank, and then I can slide through the tabs and see that all of the pages are blank except the last one. But, the last page is not displaying the data from the last day, it is displaying one day eariler than it should, the next-to-last set of data.
I copied another ViewPager / FragmentPagerAdapter that I have that works, although that one has only 3 pages and uses a different Fragment for each page.
I am not seeing what I am doing wrong, I have been searching all day and have not found an answer yet. A lot of similar questions on here are resolved by using FragmentStatePagerAdapter instead of FragmentPagerAdapter. I already tried that and it behaves exactly the same as currently.
This is the activity hosting the ViewPager:
public class ForecastFaveRowClickedActivity extends AppCompatActivity {
String distName = "";
public List<ForecastEntry> forecastEntries = new ArrayList<>();
ForecastPagerAdapter adapterViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forecast_detail);
// get the districts from the main activity
Bundle bundle = getIntent().getExtras();
distName = bundle.getString("districtName");
if (bundle.containsKey("forecastData")) {
forecastEntries = bundle.getParcelableArrayList("forecastData");
}
Collections.sort(forecastEntries);
// set up viewpager and display the first tab
// ViewPager for tabs
ViewPager viewPager = (ViewPager) findViewById(R.id.forecast_pager);
adapterViewPager = new ForecastPagerAdapter(getSupportFragmentManager(), ForecastFaveRowClickedActivity.this);
viewPager.setOffscreenPageLimit(7);
// set the tab titles based on the forecast list for this district
String[] days = new String[7];
int count = 0;
for (ForecastEntry forecastEntry : forecastEntries) {
days[count] = forecastEntry.forecastDay.substring(0,3);
count++;
}
adapterViewPager.setTabTitles(days);
viewPager.setAdapter(adapterViewPager);
//viewPager.setCurrentItem(0); // set to today's tab to start with
// give the tab layout to the viewpager
TabLayout tabLayout = (TabLayout) findViewById(R.id.forecast_sliding_date_tabs);
tabLayout.setupWithViewPager(viewPager);
}
// class for view pager adapter
public class ForecastPagerAdapter extends FragmentPagerAdapter {
private int NUM_ITEMS = 7;
private String tabTitles[] = new String[7];
private Context context;
public ForecastPagerAdapter(FragmentManager fm) {
super(fm);
}
public ForecastPagerAdapter(FragmentManager fragmentManager, Context context) {
super(fragmentManager);
this.context = context;
}
// returns total # of pages
#Override
public int getCount() {
return NUM_ITEMS;
}
// returns the fragment to display for that page
#Override
public Fragment getItem(int position) {
Fragment frag = new Fragment();
ForecastEntry forecastEntry;
Log.v("Fragment.getItem", Integer.toString(position));
forecastEntry = ((ForecastFaveRowClickedActivity)context).forecastEntries.get(position);
frag = FragmentForecastDay.newInstance(position, forecastEntry);
return frag;
}
// returns the page title for the top indicator
#Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
public void setTabTitles(String[] days) {
tabTitles = Arrays.copyOf(days, days.length);
}
}
}
This is the layout for that activity:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
android:fitsSystemWindows="true"
tools:context="com.districtoverview.ForecastFaveRowClickedActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:titleTextColor="#color/colorWhite"
android:elevation="4dp"
android:theme="#style/AppTheme.AppBarOverlay"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<LinearLayout
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"
android:paddingBottom="6dp"
android:paddingTop="6dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.districtoverview.ForecastFaveRowClickedActivity"
tools:showIn="#layout/activity_forecast_detail"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="#+id/forecast_sliding_date_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:paddingBottom="2dp"
android:paddingTop="2dp"
app:tabMode="scrollable"
app:tabGravity="fill"
style="#style/customForecastTabLayout" />
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/forecast_pager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="#android:color/white"
tools:context="com.districtoverview.ForecastFaveRowClickedActivity">
</android.support.v4.view.ViewPager>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
This is the code for the Fragment class.
public class FragmentForecastDay extends Fragment {
String label;
int page;
ForecastEntry forecastEntry;
public static FragmentForecastDay newInstance(int page, ForecastEntry forecastEntry) {
FragmentForecastDay fragment = new FragmentForecastDay();
Bundle args = new Bundle();
args.putParcelable("forecastEntry", forecastEntry);
args.putInt("page", page);
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_forecast_day, container, false);
page = getArguments().getInt("page", 0);
forecastEntry = getArguments().getParcelable("forecastEntry");
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
TextView headerDistNameTV = (TextView) getActivity().findViewById(R.id.dist_name_header_text);
headerDistNameTV.setText(parentActivity.distName);
TextView headerForecastDateTV = (TextView) getActivity().findViewById(R.id.forecast_date_header_text);
headerForecastDateTV.setText(forecastEntry.forecastDate);
TextView headerTotalTechsTV = (TextView) getActivity().findViewById(R.id.total_techs_header_text); headerTotalTechsTV.setText(Double.toString(forecastEntry.totalTechs));
TextView headerDayShiftTV = (TextView) getActivity().findViewById(R.id.day_shift_header_text);
headerDayShiftTV.setText("Day Shift");
TextView dayExpectedTechsTV = (TextView) getActivity().findViewById(R.id.day_shift_expected_text);
String dayExpectedTechs = "Expected " + Double.toString(forecastEntry.expectedTechs);
dayExpectedTechsTV.setText(dayExpectedTechs);
TextView headerAfterHoursTV = (TextView) getActivity().findViewById(R.id.after_hours_header_text);
headerAfterHoursTV.setText("After Hours");
TextView afterHoursExpectedTechsTV = (TextView) getActivity().findViewById(R.id.day_shift_expected_text);
String afterHoursExpectedTechs = "Expected " + Double.toString(forecastEntry.afterHoursExpected);
afterHoursExpectedTechsTV.setText(afterHoursExpectedTechs);
}
}
This is the fragment layout:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/forecast_day_table_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.90"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:weightSum="1">
<TableRow
android:id="#+id/row_dist_name_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.50">
<TextView
android:id="#+id/dist_name_header_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:textAppearance="?android:attr/textAppearanceLarge"
android:paddingBottom="5dp" />
</TableRow>
<TableRow
android:id="#+id/row_date_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.50">
<TextView
android:id="#+id/forecast_date_header_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:textAppearance="?android:attr/textAppearanceLarge"
android:paddingBottom="5dp" />
</TableRow>
<TableRow
android:id="#+id/row_total_techs_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.50">
<TextView
android:id="#+id/total_techs_header_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:textAppearance="?android:attr/textAppearanceLarge"
android:paddingBottom="5dp" />
</TableRow>
<TableRow
android:id="#+id/row_total_techs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.50">
<TextView
android:id="#+id/total_techs_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:textAppearance="?android:attr/textAppearanceLarge"
android:paddingBottom="5dp" />
</TableRow>
<TableRow
android:id="#+id/row_day_shift_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.50">
<TextView
android:id="#+id/day_shift_header_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:textAppearance="?android:attr/textAppearanceLarge"
android:paddingBottom="5dp" />
</TableRow>
<TableRow
android:id="#+id/row_day_shift_expected"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.50">
<TextView
android:id="#+id/day_shift_expected_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:textAppearance="?android:attr/textAppearanceLarge"
android:paddingBottom="5dp" />
</TableRow>
<TableRow
android:id="#+id/row_after_hours_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.50">
<TextView
android:id="#+id/after_hours_header_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:textAppearance="?android:attr/textAppearanceLarge"
android:paddingBottom="5dp" />
</TableRow>
<TableRow
android:id="#+id/row_after_hours_expected"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.50">
<TextView
android:id="#+id/after_hours_expected_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:textAppearance="?android:attr/textAppearanceLarge"
android:paddingBottom="5dp" />
</TableRow>
</TableLayout>
This was caused by my incorrect use of getActivity() rather than view. I had copied the code from another set of swipe tabs which was sitting on the main activity, but this set was inside of a fragment. I needed to refer to the view of the parent fragment.
So for example this:
(TextView) getActivity().findViewById(R.id.dist_name_header_text);
Should have been this:
(TextView) view.findViewById(R.id.dist_name_header_text);
Once I made this change all child fragments used in the view pager were displayed correctly.
one potential problem might be that all the seven fragments are not created properly at the time of viewpager creation.so your activity code would be as shown below
// set up viewpager and display the first tab
// ViewPager for tabs
ViewPager viewPager = (ViewPager) findViewById(R.id.forecast_pager);
adapterViewPager = new ForecastPagerAdapter(getSupportFragmentManager(), ForecastFaveRowClickedActivity.this);
viewPager.setOffscreenPageLimit(7);

How can i scroll my whole view pager and stick to toolBar in Fragment Android?

First of all my English is not too good. But i will try to understand my point to you people.
I am making app in which i used scroll view and inside it many other layout with API xml's and also view pager.You can see below
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.iptikarpromotion.activity.LoaderActivity" >
<ScrollView
android:id="#+id/scrollview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#color/background_color" >
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.daimajia.slider.library.SliderLayout
android:id="#+id/slider"
android:layout_width="match_parent"
android:layout_height="155dp"
custom:auto_cycle="true"
custom:indicator_visibility="visible"
custom:pager_animation="Accordion"
custom:pager_animation_span="1100" />
<com.daimajia.slider.library.Indicators.PagerIndicator
android:id="#+id/custom_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:gravity="center"
custom:selected_color="#color/primary"
custom:selected_drawable="#drawable/ic_launcher"
custom:selected_height="6dp"
custom:selected_padding_left="6dp"
custom:selected_padding_right="6dp"
custom:selected_width="6dp"
custom:shape="oval"
custom:unselected_color="#55333333"
custom:unselected_height="6dp"
custom:unselected_padding_left="2dp"
custom:unselected_padding_right="2dp"
custom:unselected_width="6dp" />
<com.daimajia.slider.library.Indicators.PagerIndicator
android:id="#+id/custom_indicator2"
style="#style/AndroidImageSlider_Corner_Oval_Orange"
android:layout_marginBottom="20dp" />
<--! Many other Linear and RelativeLayout is used here-->
<!-- For Viewpager -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="#string/quickly"
android:textColor="#color/current_deals_color"
android:textSize="15sp" >
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView3"
android:layout_marginTop="-3dp"
android:text="#string/sub_quickly" android:textColor="#color/all_categories_sub_title_color"
android:textSize="10sp"
android:textStyle="italic" />
</RelativeLayout>
<LinearLayout
android:id="#+id/rootContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:orientation="vertical" >
<com.astuetz.PagerSlidingTabStrip
android:id="#+id/sliding_tabs"
style="#style/PagerTabStripText"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="#+id/my_awesome_toolbar"
android:background="#drawable/bg_quickly_pick"
android:paddingBottom="3sp"
android:textSize="10sp"
app:pstsDividerColor="#android:color/transparent"
app:pstsIndicatorColor="#color/indicator_color"
app:pstsIndicatorHeight="3dp"
app:pstsTabPaddingLeftRight="15dip"
app:pstsUnderlineColor="#android:color/transparent" />
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</android.support.v4.view.ViewPager>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
And here id my code below
public class FragmentHome extends Fragment implements ViewPager.OnPageChangeListener {
private ViewPager viewPager;
private TabPagerAdapter PagerAdapter;
private static int pageIndexd;
private PagerSlidingTabStrip pagerSlidingTabStrip;
LinearLayout ll_jewellery = null;
private SliderLayout mDemoSlider;
ScrollView scrollView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home_layout, null);
scrollView = (ScrollView)rootView.findViewById(R.id.scrollview);
viewPager = (ViewPager) rootView.findViewById(R.id.pager);
rootView.findViewById(R.id.layout);
pagerSlidingTabStrip = (PagerSlidingTabStrip) rootView.findViewById(R.id.sliding_tabs);
mDemoSlider = (SliderLayout)rootView.findViewById(R.id.slider);
imageSlider();
ll_jewellery = (LinearLayout)rootView.findViewById(R.id.ll_jewellery);
CategoryLayoutClikListener categoryLayoutClikListener = new CategoryLayoutClikListener();
ll_jewellery.setOnClickListener(categoryLayoutClikListener);
viewPager.setOffscreenPageLimit(1);
pageIndexd = viewPager.getCurrentItem();
viewPager.setCurrentItem(pageIndexd);
FragmentManager fm = getFragmentManager();
PagerAdapter = new TabPagerAdapter(fm);
setAdapter();
return rootView;
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void onPageSelected(int position) {
}
private void imageSlider(){
HashMap<String,String> url_maps = new HashMap<String, String>();
url_maps.put("Hannibal", "http://static2.hypable.com/wp-content/uploads/2013/12/hannibal-season-2-release-date.jpg");
url_maps.put("Big Bang Theory", "http://tvfiles.alphacoders.com/100/hdclearart-10.png");
url_maps.put("House of Cards", "http://cdn3.nflximg.net/images/3093/2043093.jpg");
url_maps.put("Game of Thrones", "http://images.boomsbeat.com/data/images/full/19640/game-of-thrones-season-4-jpg.jpg");
for(String name : url_maps.keySet()){
TextSliderView textSliderView = new TextSliderView(getActivity());
// initialize a SliderLayout
textSliderView.description(name).image(url_maps.get(name)).setScaleType(BaseSliderView.ScaleType.Fit);
//.setOnSliderClickListener(this);
//add your extra information
textSliderView.getBundle().putString("extra",name);
mDemoSlider.addSlider(textSliderView);
}
mDemoSlider.setPresetTransformer(SliderLayout.Transformer.Default);
mDemoSlider.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
mDemoSlider.setCustomAnimation(new DescriptionAnimation());
mDemoSlider.setDuration(4000);
}
private void setAdapter() {
viewPager.setAdapter(PagerAdapter);
pagerSlidingTabStrip.setViewPager(viewPager);
}
private class CategoryLayoutClikListener implements OnClickListener {
#Override
public void onClick(View v) {
}
}
}
So i want to scroll my whole view, and on scroll below viewpager should be stick with toolbar. In xml you can see my viewpager stick below on end of scroll view. no movement up and down. i want to scroll up and stick with toolbar and than scroll down to its position. How can i achieve this.
I follow this below link too and make many many changes but it doesnt help me.At the end i clean my code to original position.
https://github.com/kmshack/Android-ParallaxHeaderViewPager
http://architects.dzone.com/articles/how-make-google-plus-profile
Thanks

How to combine two view in ViewPager Android?

I'm creating ViewPager with CirclePageIndicator.There are two View inside. Right now, It's showing two View in one page. But when i scroll to next the previous one is showing. It means it is showing like previous one + new.
Current Look like:
ItemA, ItemB | ItemB, ItemC | ItemC, ItemD |
Expected look like
ItemA, ItemB | ItemC, ItemD | ItemE, ItemF |
How can i fix it?
Is there any way to combine this two ItemA and ItemB?
Here is my code.
Adapter
public class ViewPagerAdapter extends PagerAdapter {
// Declare Variables
Context context;
ViewPager pager;
String[] domain;
String[] title;
int[] flag;
LayoutInflater inflater;
public ViewPagerAdapter(ViewPager pager,Context context, String[] domain,
String[] title, int[] flag) {
this.pager= pager;
this.context = context;
this.domain = domain;
this.title = title;
this.flag = flag;
}
#Override
public int getCount() {
//Show the CirclePageIndicator for only half of array.
return (int) Math.ceil((double)title.length/2);
// return title.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.related_article_item, container,
false);
// Locate the TextViews in viewpager_item.xml
final Button btnSave = (Button) itemView.findViewById(R.id.btn_save);
final Button btnLike = (Button) itemView.findViewById(R.id.btn_like);
final TextView tv_domain = (TextView) itemView.findViewById(R.id.domain_text);
TextView tv_title = (TextView) itemView.findViewById(R.id.title_text);
ResizableImageView imageView = (ResizableImageView) itemView.findViewById(R.id.imageViewDynamic);
final ProgressBar progressBar = (ProgressBar) itemView.findViewById(R.id.loading);
// Capture position and set to the TextViews
tv_domain.setText(domain[position]);
tv_title.setText(title[position]);
// Locate the ImageView in viewpager_item.xml
imageView.setImageResource(flag[position]);
// Add viewpager_item.xml to ViewPager
((ViewPager) container).addView(itemView);
return itemView;
}
//This is the code to show two view inside one page.
#Override
public float getPageWidth(int position) {
return (0.5f);
}
// return (super.getPageWidth(position) / 2);
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
((ViewPager) container).removeView((RelativeLayout) object);
}
}
MainActivity
public class TestingActivity extends ActionBarActivity {
// Declare Variables
ViewPager viewPagerRelated;
PagerAdapter adapter;
String[] title;
String[] domain;
int[] flag;
ViewPager pager1;
CirclePageIndicator mIndicator;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from viewpager_main.xml
setContentView(R.layout.viewpager_main);
title = new String[]{"Sample1", "Sample2", "Sample3",
"Sample4", "Sample5", "Sample6", "Sample7", "Sample8",
"Sample9", "Sample10"};
domain = new String[]{"text1", "text1",
"text1", "text1", "text1", "text1",
"text1", "text1", "text1", "text1"};
flag = new int[]{R.drawable.arsenal, R.drawable.aston_villa,
R.drawable.manchester_city, R.drawable.liverpool,
R.drawable.chelsea, R.drawable.manchester_united, R.drawable.swansea,
R.drawable.liverpool, R.drawable.west_brom, R.drawable.west_ham};
// Locate the ViewPager in viewpager_main.xml
viewPagerRelated = (ViewPager) findViewById(R.id.pager2);
// Pass results to ViewPagerAdapter Class
adapter = new ViewPagerAdapter(pager1, ArticleViewActivityV2.this, domain,
title, flag);
// Binds the Adapter to the ViewPager
viewPagerRelated.setAdapter(adapter);
// viewPagerRelated.setPageMargin(3);
// ViewPager Indicator
mIndicator = (CirclePageIndicator) findViewById(R.id.indicator_pager);
mIndicator.setViewPager(viewPagerRelated);
}
}
XML Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="#layout/related_article_item"
android:id="#+id/article_1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<include
layout="#layout/related_article_item"
android:id="#+id/article_2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
Item 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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="220dp"
android:background="#drawable/gridview_item_selector"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp">
<RelativeLayout
android:id="#+id/imageLayout"
android:layout_width="200dp"
android:layout_height="100dp">
<com.bindez.news.utils.ResizableImageView
android:id="#+id/imageViewDynamic"
android:layout_width="match_parent"
android:layout_height="100dp"
android:src="#drawable/app_icon" />
<ProgressBar
android:id="#+id/loading"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="10dp"
android:visibility="visible" />
</RelativeLayout>
<TextView
android:id="#+id/title_text"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="#+id/imageLayout"
android:layout_marginTop="7dp"
android:maxLines="10"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:paddingTop="7dp"
android:text="Title Text"
android:textColor="#color/tile_text"
android:textSize="14sp" />
<TextView
android:id="#+id/domain_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title_text"
android:layout_marginBottom="3dp"
android:ellipsize="end"
android:maxLines="1"
android:paddingBottom="7dp"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:text=""
android:textColor="#color/text_gray"
android:textSize="12sp" />
<TextView
android:id="#+id/seperator"
android:layout_width="fill_parent"
android:layout_height="1px"
android:layout_below="#+id/domain_text"
android:background="#color/light_gray_seperator" />
<Button
android:id="#+id/btn_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/seperator"
android:background="#android:color/transparent"
android:drawableRight="#drawable/ic_unsave"
android:minHeight="0dp"
android:minWidth="0dp"
android:paddingBottom="5dp"
android:paddingRight="7dp"
android:paddingTop="5dp" />
<Button
android:id="#+id/btn_like"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/seperator"
android:background="#android:color/transparent"
android:drawableLeft="#drawable/ic_unlike"
android:drawablePadding="5dp"
android:minHeight="0dp"
android:minWidth="0dp"
android:paddingBottom="5dp"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:paddingTop="5dp"
android:text=""
android:textColor="#color/btn_green"
android:textSize="14sp" />
</RelativeLayout>
</RelativeLayout>

Categories

Resources