Can anyone help me with NestedScrollView, I'm using this component it is hiding my ActionBar as it should, however I have a GridView with a gallery of images and when I scroll to see more images it does not let us use Scroll.
I've done everything I can imagine, I've looked everywhere, I've been trying to convert it for almost 2 days: '(
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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:fillViewport="true"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/content_main">
</FrameLayout>
</android.support.v4.widget.NestedScrollView>
GridFragment
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/fragmento_grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/black" >
<ProgressBar
android:id="#+id/pbLoader"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" >
</ProgressBar>
<GridView
android:id="#+id/grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/ad_view"
android:background="#color/grid_bg"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" >
</GridView>
</RelativeLayout>
Java
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Mako on 1/13/2017.
*/
public class dataFragment extends Fragment {
View view;
ViewPager viewPager;
TabLayout tabLayout;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.sample, container, false);
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
viewPager.setAdapter(new sliderAdapter(getChildFragmentManager()));
tabLayout = (TabLayout) view.findViewById(R.id.sliding_tabs);
tabLayout.post(new Runnable() {
#Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
return view;
}
private class sliderAdapter extends FragmentPagerAdapter {
final String tabs[]={"tab1", "tab2"};
public sliderAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
android.support.v4.app.Fragment fragment = null;
fragment = GridFragment.newInstance("6360392529587060753");
return fragment;
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
return tabs[position];
}
}
}
Video demonstrating problem:
Example
Related
I got a problem. When im using a pagerView everything inside is showing up beside ImageView.
I'm using this :
<!-- activity_screen_slide.xml -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:id="#+id/relativeLayout">
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="0dp" />
</RelativeLayout>
and this :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#drawable/common_google_signin_btn_icon_dark" />
</LinearLayout>
And i use this for fragment
package com.example.gebruiker.drumio.SlideScreenTrackPreview;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.example.gebruiker.drumio.R;
public class TestFragment extends Fragment {
ImageView hihat;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup
container, #Nullable Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup)
inflater.inflate(R.layout.track_preview_test,container,false);
hihat =(ImageView)rootView.findViewById(R.id.imageView2);
hihat.setBackgroundColor(Color.BLACK);
hihat.setColorFilter(Color.BLACK);
return rootView;
}
}
Except the button, nothing is showing up.
Here is the code for the activity that i use :
package com.example.gebruiker.drumio.SlideScreenTrackPreview;
public class ScreenSliderActivity extends FragmentActivity {
private static final int NUM_PAGES = 5;
private PagerAdapter mPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_slide);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setBackgroundColor(Color.WHITE);
mPagerAdapter = new
ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
}
#Override
public void onBackPressed() {
if (mPager.getCurrentItem() == 0) {
super.onBackPressed();
} else {
// Otherwise, select the previous step.
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return new TestFragment();
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
}
Your activity works for me if you inherit from AppCompatActivity instead of from FragmentActivity. I don't know why; maybe somebody with more experience can elucidate.
IN this layout i have made tab layout with fragment activity and wanted to display few things,however nothing is being shown in fragment activity.
This is the base xml where the fragment is being made change.xml
<android.support.v4.widget.DrawerLayout 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/activity_change"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_collapseParallaxMultiplier="0.5"
tools:context="com.example.alpit.formula2.Change">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/change_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/change"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.NoActionBar.PopupOverlay"></android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="#+id/change_tab"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:tabGravity="fill">
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/change_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/change_appbar"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
</android.support.v4.view.ViewPager>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_change"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/navigation_menu"
app:theme="#style/NavigationTheme">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
This is the java code of the tab layout
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Distance extends Fragment {
protected View onCreate(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
return inflater.inflate(R.layout.activity_distance, container, false);
}
}
This is the xml code of distance
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_distance"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.alpit.formula2.Distance"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Weight in kg" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Weight in kg" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Weight in kg"
android:textSize="100sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Weight in kg" />
</LinearLayout>
This is my java code of adding tabs in the layout
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import java.util.ArrayList;
import java.util.List;
class Change extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private ActionBarDrawerToggle toggle3;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_change);
Toolbar toolbar = (Toolbar) findViewById(R.id.change);
setSupportActionBar(toolbar);
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.activity_change);
toggle3 = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.Close);
drawerLayout.addDrawerListener(toggle3);
toggle3.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_change);
navigationView.setItemIconTintList(null);
navigationView.setNavigationItemSelectedListener(this);
setTitle("Conversion");
viewPager = (ViewPager) findViewById(R.id.change_viewpager);
setUpViewpager(viewPager);
TabLayout tabs = (TabLayout) findViewById(R.id.change_tab);
tabs.setupWithViewPager(viewPager);
}
public boolean onOptionsItemSelected(MenuItem menuItem) {
if (toggle3.onOptionsItemSelected(menuItem)) {
return true;
}
return onOptionsItemSelected(menuItem);
}
private void setUpViewpager(ViewPager upViewpager) {
Adapter_tab adapter = new Adapter_tab(getSupportFragmentManager());
adapter.addFragment(new Distance(), "Distance");
adapter.addFragment(new Mass(), "Mass");
upViewpager.setAdapter(adapter);
}
#Override
public boolean onNavigationItemSelected(MenuItem item) {
return false;
}
static class Adapter_tab extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public Adapter_tab(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentTitleList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
just add onCreateView in Distance
I have an application where I have a part of the screen for displaying information about a student and the remaining part with tablayout and viewpager. My problem is tablayout shows up in the design editor but viewpager doesn't. In-spite of the code written nothing is showing up. Tell me where I am getting things wrong?
CAMarksFragment.java
package com.learn.app;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class CAMarksFragment extends Fragment {
TabLayout tabLayout;
ViewPager viewPager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_camarks, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//you can set the title for your toolbar here for different fragments different titles
getActivity().setTitle("CA Marks");
viewPager=(ViewPager)getActivity().findViewById(R.id.viewPager);
viewPager.setAdapter(new CustomAdapter(getActivity().getSupportFragmentManager(),getActivity().getApplicationContext()));
tabLayout=(TabLayout)getActivity().findViewById(R.id.tabLayout);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
});
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
SharedPreferences pref = getActivity().getApplicationContext().getSharedPreferences("AppStatus", 0); // 0 - for private mode
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("Home", false);
editor.commit();
}
private class CustomAdapter extends FragmentPagerAdapter {
private String[] fragments={"Theory","Practical"};
public CustomAdapter(FragmentManager supportFragmentManager, Context applicationContext) {
super(supportFragmentManager);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new Theory();
case 1:
return new Practical();
default:
return null;
}
}
#Override
public int getCount() {
return fragments.length;
}
#Override
public CharSequence getPageTitle(int position) {
return fragments[position];
}
}
}
fragments_camarks.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.learn.app.TestTimeTableFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/tablebackgrounds"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/ca_all"
android:background="#drawable/allbackgrounds"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageButton
android:id="#+id/user_profile_photo"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:background="#drawable/profile_circular_border_imageview"
android:padding="20dp"
android:scaleType="centerCrop"
android:src="#drawable/defaultpic" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="Hewitt"
android:textStyle="bold"
android:textColor="#ffffff"
android:id="#+id/camarks_name"
android:padding="10dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14dp"
android:id="#+id/camarks_dept"
android:text="Master of Computer Application"
android:textColor="#ffffff"
android:padding="10dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14dp"
android:id="#+id/camarks_rollno"
android:text="15mx13"
android:textColor="#ffffff"
android:padding="10dp"
/>
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="#ffffff"
android:textStyle="bold"
android:background="#80000000"
android:id="#+id/ca_title"
android:text="CA Marks"
android:gravity="center"
android:padding="10dp"
/>
</LinearLayout>
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tabLayout"
android:layout_below="#id/ca_all"
android:background="#color/colorPrimaryDark"
app:tabGravity="fill"
app:tabMode="fixed"
>
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/viewPager"
android:layout_below="#id/tabLayout"
android:layout_centerHorizontal="true"
>
</android.support.v4.view.ViewPager>
</RelativeLayout>
</ScrollView>
</FrameLayout>
Practical.java
package com.learn.app;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Practical extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_layout1, container, false);
}
}
Theory.java
package com.learn.app;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Theory extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_layout, container, false);
}
}
fragment_layout contains linearlayout with a textview in it.
In the emulator viewpager doesn't showup and tablayout remain untouchable.What I am getting wrong here. Help me to proceed further. Thanks in advance.
Emulator Output
Many things are wrong in the code.
in xml : add android:layout_height="match_parent" for viewpager
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/tabLayout"
android:layout_centerHorizontal="true">
in code:
change your code as: you need to use fragment's view to get the viewpager instance. Also your code missing some basic functionalities for viewpager with tablayout
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_camarks, container, false);
viewPager=(ViewPager)view.findViewById(R.id.viewPager);
tabLayout=(TabLayout)view.findViewById(R.id.tabLayout);
tabLayout.addTab(tabLayout.newTab().setText("Theory"));
tabLayout.addTab(tabLayout.newTab().setText("Practical"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
viewPager.setAdapter(new CustomAdapter(getActivity().getSupportFragmentManager(),getActivity().getApplicationContext()));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
tabLayout.setupWithViewPager(viewPager);
return view;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//you can set the title for your toolbar here for different fragments different titles
getActivity().setTitle("CA Marks");
}
It could be because you are using views from androidx
Well it does have ViewPager along with ViewPager2
Add the following line in build.gradle(:app) file:
implementation 'androidx.viewpager:viewpager:1.0.0'
I have fragment activity that have view pager, I want every changing page the content is change programmatically. `
package com.idroid.splashscreen;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.TextView;
import android.widget.Toast;
public class ScreenSlidePagerActivity extends FragmentActivity {
private static final int NUM_PAGES = 5;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_slide);
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
OnPageChangeListener pageChangeListener=null;
pageChangeListener = new OnPageChangeListener() {
#Override
public void onPageScrollStateChanged(int arg0) { }
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) { }
#Override
public void onPageSelected(int position) {
Toast.makeText(getApplicationContext()
.getApplicationContext(),
"page "+position,
Toast.LENGTH_LONG).show();
TextView txt=(TextView) findViewById(R.id.txtcoba);
txt.setText("page "+position);
}
};
mPager.setOnPageChangeListener(pageChangeListener);
}
#Override
public void onBackPressed() {
if (mPager.getCurrentItem() == 0) {
super.onBackPressed();
} else {
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return new ScreenSlidePageFragment();
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
}
`
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/txtcoba"
style="?android:textAppearanceMedium"
android:padding="16dp"
android:lineSpacingMultiplier="1.2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="coba" />
</ScrollView>
I change it on page selected method and change the content of text view. for the toast it's work but for the text doesn't change. but on page 4 when I back to page 3, the page 3 show the page. but the other page not.
what should I do ? thanks before. this is the ScreenSlidePageFragment class
package com.idroid.splashscreen;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ScreenSlidePageFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_screen_slide_page, container, false);
return rootView;
}
}
this is fragment_screen_slide_page.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/txtcoba"
style="?android:textAppearanceMedium"
android:padding="16dp"
android:lineSpacingMultiplier="1.2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="coba" />
</ScrollView>
this is activity_screen_slide.xml
<?xml version="1.0" encoding="utf-8"?>
<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="match_parent" />
Issue is your trying to set the TextView of Fragment layout.
As per your code your TextView is in the ScreenSlidePageFragment layout.
Remove the 'TextView' setText in onPageeSelcted
Change your ScreenSlidePageFragment like this
private class ScreenSlidePageFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View row = inflater.inflate(R.layout.activity_screen_slide, container, false);
TextView txt=(TextView) row.findViewById(R.id.txtcoba);
txt.setText("page using fragment");
return row;
}
}
I have a big request for any of You. I would like to create a third Fragment in my simple application. Everything works fine, no error. I would like to make the last Fragment it is: "TestFragment" when I click that button inside that Fragment I'd like to refresh its View (on click button) and load another view inside this fragment (the same fragment). Please help me do this, show how to get it. I have read in Google but can not find the solution appropriate for this application.
Here is simple code:
MAIN ACTIVITY:
package com.example.viewpagerexample;
import com.example.viewpagerexample.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
public class MainActivity extends FragmentActivity {
private MyAdapter mAdapter;
private ViewPager mPager;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new MyAdapter(getSupportFragmentManager());
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
}
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return 2;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new TestFragment();
// case 1:
// return new ImageFragment();
case 1:
return new TestFragment();
//
default:
return null;
}
}
}
}
TEST FRAGMENT:
package com.example.viewpagerexample;
import com.example.viewpagerexample.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class TestFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("Test", "hello");
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.test, container, false);
// TextView textView = (TextView) view.findViewById(R.id.detailsText);
// textView.setText("Testing");
return view;
}
}
TEST 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:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="QUIZ IMPORTANT TAKE A LOOK AT THIS!!!!!"
android:textSize="30dp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_marginLeft="30dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="asdjiasdjasoldjasjdasjdlasjdksahfkadshfalidshfladshfladsfhlasdfgjl;adkglkafdgnklfdsgjpfasmga.dsnvlkasdnvkldsnflkdasjflkasdnvlkadjvodsijfosdjv;lasdvlasdjvladosijao" />
DETAILS FRAGMENT:
package com.example.viewpagerexample;
import com.example.viewpagerexample.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class Details extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("Test", "hello");
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details, container, false);
TextView textView = (TextView) view.findViewById(R.id.detailsText);
textView.setText("Testing");
return view;
}
}
ACTIVITY_MAIN LAYOUT:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
DETAILS LYOUT:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
How to do this refresh inside the same fragment?