So I know this has been answered multiple times, such as Fragment Inside Fragment. I am using the support library for getChildFragmentManager() as advised in the post. The issue is that my viewpager's fragments are not appearing despite being inflated. My tabs (with icons) are showing perfectly fine. Here is the relevant code:
DialogFragment:
public class DialogFragment extends DialogFragment {
private static final String ARG_PAGE = "ARG_PAGE";
private int mPage;
private HotSpot hotSpot;
private Gson gson = new Gson();
private Map nonEmptyPerks;
private TabLayout tabLayout;
private ViewPager viewPager;
public DialogFragment() {
// Empty constructor required for DialogFragment
}
public static PerkDetailDialogFragment newInstance(int page, String hotSpotJson) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
args.putString("HOTSPOTDETAILED", hotSpotJson);
PerkDetailDialogFragment fragment = new PerkDetailDialogFragment();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
String json = getArguments().getString("HOTSPOTDETAILED");
hotSpot = gson.fromJson(json, HotSpot.class);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_dialog, parent, false);
// Get the ViewPager and set it's PagerAdapter so that it can display items
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
tabLayout = (TabLayout) view.findViewById(R.id.perkSelector);
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
nonEmptyPerks = getNonEmptyPerks(hotSpot.getPerks());
PerkFragmentPagerAdapter pagerAdapter =
new PerkFragmentPagerAdapter(getChildFragmentManager(), nonEmptyPerks.size());
viewPager.setAdapter(pagerAdapter);
// Give the TabLayout the ViewPager
tabLayout.setupWithViewPager(viewPager);
// ..... More code here
}
#Override
public void onStart() {
super.onStart();
getDialog().getWindow().setLayout(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
}
public class PerkFragmentPagerAdapter extends FragmentPagerAdapter {
private int pageCount;
public PerkFragmentPagerAdapter(FragmentManager fm, int pageCount) {
super(fm);
this.pageCount = pageCount;
}
#Override
public int getCount() {
return pageCount;
}
#Override
public Fragment getItem(int position) {
String perk = "temp";
return PerkFragment.newInstance(position + 1, perk);
}
}
}
PerkFragment:
public class PerkFragment extends Fragment {
private static final String ARG_PAGE = "ARG_PAGE";
private static final String ARG_PERK_DESCRIPTION = "PERK_DESCRIPTION";
private int mPage;
private String perkDescription;
public static PerkFragment newInstance(int page, String perk) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
args.putString(ARG_PERK_DESCRIPTION, perk);
PerkFragment fragment = new PerkFragment();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
perkDescription = getArguments().getString(ARG_PERK_DESCRIPTION);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_perk, container, false);
TextView perkDescriptionTextView = (TextView) view.findViewById(R.id.perkDescription);
perkDescriptionTextView.setText(perkDescription);
return view;
}
}
fragment_dialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<ImageView
android:id="#+id/hotSpotImg"
android:layout_width="match_parent"
android:layout_height="250dp"
android:scaleType="centerCrop"
android:src="#drawable/img_venue_placeholder" />
<ImageView
android:id="#+id/iconBackArrow"
android:layout_width="50dp"
android:layout_height="60dp"
android:layout_alignStart="#+id/hotSpotImg"
android:layout_alignTop="#+id/hotSpotImg"
android:src="#drawable/img_back_arrow_shadow" />
<ImageView
android:id="#+id/iconGPS"
android:layout_width="50dp"
android:layout_height="60dp"
android:layout_alignBottom="#+id/hotSpotImg"
android:layout_alignStart="#+id/hotSpotImg"
android:src="#drawable/img_gps" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/hotSpotImg">
<TextView
android:id="#+id/memberSpace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:text="#string/member_space"
android:textAllCaps="true"
android:textColor="#color/grey_2"
android:textSize="16sp" />
<com.test.test.helpers.LetterSpacingTextView
android:id="#+id/hotSpotName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/memberSpace"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:text="That filler info"
android:textAlignment="center"
android:textAllCaps="true"
android:textColor="#color/black"
android:textSize="22sp" />
<android.support.design.widget.TabLayout
android:id="#+id/perkSelector"
style="#style/MyCustomTabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/hotSpotName"
android:layout_marginBottom="10dp"
app:tabGravity="center" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp" />
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/checkIn"
android:layout_below="#id/perkSelector"
android:layout_centerHorizontal="true">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingEnd="20dp"
android:paddingStart="20dp">
<TextView
android:id="#+id/hotSpotHours"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="#string/hours"
android:textColor="#color/grey"
android:textSize="14sp" />
<TextView
android:id="#+id/hotSpotDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/hotSpotHours"
android:layout_marginTop="20dp"
android:lineSpacingExtra="10dp"
android:text="FILLER DESCRIPTION"
android:textAlignment="center"
android:textColor="#color/grey"
android:textSize="16sp" />
</RelativeLayout>
</ScrollView>
<TextView
android:id="#+id/checkIn"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="#drawable/text_view_button"
android:clickable="true"
android:text="#string/check_in"
android:textAlignment="center"
android:textAllCaps="true" />
</RelativeLayout>
</RelativeLayout>
fragment_perk.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/perkDescription"
style="#style/MyCustomTextAppearance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PERK FILLER"
android:textColor="#color/magnises_grey"
android:textSize="18sp" />
The issue is that the viewpager should include tablayout inside the viewpager tags. This fixes the problem.
e.g:
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<android.support.design.widget.TabLayout
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/MY.DIALOG"
card_view:tabTextColor="#666666"
card_view:tabSelectedTextColor="#666666" />
</android.support.v4.view.ViewPager>
Related
I'm trying to make an app that fetches a book list from an API and displays it on the master list and, upon clicking, it displays the details. It works fine with mobiles, but I can't get it to work with tablets and since there's no error I can't find out where I went wrong.
On tablets it renders as if on a phone instead of rendering the two pane view.
I'm using a single activity with fragments.
"Main" activity:
public class ItemListActivity extends AppCompatActivity {
public static FragmentManager fragmentManager;
private boolean isTwoPane = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_list);
determinePaneLayout();
fragmentManager = getSupportFragmentManager();
if(isTwoPane){
//fragmentManager.beginTransaction().add(R.id.master_dual, new ItemsListFragment()).commit();
fragmentManager.beginTransaction().add(R.id.flDetailContainer, new ItemDetailFragment()).commit();
fragmentManager.beginTransaction().add(R.id.fragmentContainer, new ItemsListFragment()).commit();
} else {
fragmentManager.beginTransaction().add(R.id.fragmentContainer, new ItemsListFragment()).commit();
}
}
private void determinePaneLayout() {
FrameLayout fragmentItemDetail = (FrameLayout) findViewById(R.id.flDetailContainer);
// If there is a second pane for details
if (fragmentItemDetail != null) {
isTwoPane = true;
}
}
Item List Fragment:
public class ItemsListFragment extends Fragment {
private ArrayList<Book> bookList;
private ArrayList<String> bookNames;
private ArrayAdapter<Book> bookArrayAdapter;
private ArrayAdapter<String> bookNamesAdapter;
private ApiInterface apiInterface;
private ListView lvItems;
private static final String bookKey = "newBook";
private static Book nBook;
public ItemsListFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_items_list,
container, false);
return view;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
lvItems = view.findViewById(R.id.lvItems);
bookNames = new ArrayList<>();
//bookNames.add(0, "Gabriel");
apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
Call<ArrayList<Book>> call = apiInterface.getBooks();
call.enqueue(new Callback<ArrayList<Book>>() {
#Override
public void onResponse(Call<ArrayList<Book>> call, Response<ArrayList<Book>> response) {
bookList = new ArrayList<>();
bookList = response.body();
bookArrayAdapter = new ArrayAdapter<>(getContext(),
android.R.layout.simple_list_item_activated_1, bookList);
lvItems.setAdapter(bookArrayAdapter);
}
#Override
public void onFailure(Call<ArrayList<Book>> call, Throwable t) {
}
});
lvItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
nBook = bookList.get(i);
if(view.findViewById(R.id.flDetailContainer) != null){
ItemListActivity.fragmentManager.beginTransaction().replace(R.id.flDetailContainer, ItemDetailFragment.newInstance(nBook)).addToBackStack(null).commit();
} else {
ItemListActivity.fragmentManager.beginTransaction().replace(R.id.fragmentContainer, ItemDetailFragment.newInstance(nBook)).addToBackStack(null).commit();
}
}
});
}`
Item Detail Fragment:
public class ItemDetailFragment extends Fragment {
private static final String bookKey = "newBook";
private static Book nBook;
private TextView title;
private TextView isbn;
private TextView currency;
private TextView price;
private TextView author;
public static ItemDetailFragment newInstance(Book book) {
ItemDetailFragment fragment = new ItemDetailFragment();
Bundle args = new Bundle();
args.putSerializable(bookKey, book);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
nBook = (Book)getArguments().getSerializable(bookKey);
Log.v("BundleOK", "BundleOK");
} else {
Log.v("Bundle Nulo", "Bundle nulo");
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate view
View view = inflater.inflate(R.layout.fragment_item_detail,
container, false);
// Return view
return view;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
title = view.findViewById(R.id.tvTitle);
isbn = view.findViewById(R.id.tvIsbn);
currency = view.findViewById(R.id.tvCurrency);
price = view.findViewById(R.id.tvPrice);
author = view.findViewById(R.id.tvAuthor);
title.setText(nBook.getTitle());
isbn.setText("ISBN: " + nBook.getIsbn());
currency.setText("Currency: "+ nBook.getCurrency());
price.setText("Price: "+ String.valueOf((long)nBook.getPrice()/100));
author.setText("Autor: "+nBook.getAuthor());
}
XML for dual pane:
<?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/LinearLayout1"
android:showDividers="middle"
android:baselineAligned="false"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="#+id/fragmentItemsList"
android:name="gabrielgomes.studio.com.itemretrieve.ItemsListFragment"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
tools:layout="#layout/fragment_items_list" />
<View android:background="#000000"
android:layout_width="1dp"
android:layout_height="wrap_content"
/>
<FrameLayout
android:id="#+id/flDetailContainer"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" />
XML for the Main Activity (Item List Activity)
<?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="horizontal" >
<FrameLayout
android:id="#+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
XML for the Item Detail Fragment:
<?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" >
<TextView
android:id="#+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="110dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:fontFamily="#font/baskervillebt"
android:gravity="center"
android:text="Item Title"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/tvIsbn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvTitle"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp"
android:fontFamily="#font/baskervilleitalicbt"
android:text="Item Body"
android:textSize="16sp" />
<TextView
android:id="#+id/tvPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvIsbn"
android:layout_centerHorizontal="true"
android:layout_marginTop="47dp"
android:fontFamily="#font/baskervilleitalicbt"
android:text="Price"
android:textSize="16sp" />
<TextView
android:id="#+id/tvCurrency"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvPrice"
android:layout_centerHorizontal="true"
android:fontFamily="#font/baskervilleitalicbt"
android:layout_marginTop="44dp"
android:text="Currency"
android:textSize="16sp" />
<TextView
android:id="#+id/tvAuthor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvCurrency"
android:fontFamily="#font/baskervilleitalicbt"
android:layout_centerHorizontal="true"
android:layout_marginTop="45dp"
android:text="Author"
android:textSize="16sp" />
</RelativeLayout>
I've been cracking my head for 5 days now, so any help is really appreciated! I tried a number of tutorials and searched SO thoroughly but no go!
Cheers!
I believe you have two copies activity_item_list.xml layout file and one is res/layout/ folder and another one is in res/layout-sw600dp/ folder.
sw600dp --> Screen width above 600dp use this layout else if less than 600dp use default layout file.
And also you make sure keep same attribute ids same in both the layout files.
I am working with recyclerview with viewpager in fragment.
I want to make navigation on bottom of screen.
enter image description here
and when I click each button, each fragment show in the frameLayout.
the problem is happen when I enter into the artist fragment second time.(at first time it show up find as I intended.)
as you can see in this image enter image description here
there is noting shows up in the fragment. and swiping the screen not working properly. It should moving stick to the left or right but it stop at the point where I stop moving my finger.
here is my source.
activity_main_board.xml
<RelativeLayout
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">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/navigation"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true">
</FrameLayout>
<LinearLayout
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="7dp"
android:layout_weight="1"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitXY"
app:srcCompat="#drawable/image_background7" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="53dp"
android:layout_weight="1"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/my_board_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView10"
android:layout_width="30dp"
android:layout_height="30dp"
app:srcCompat="#drawable/ic_action_my" />
<TextView
android:id="#+id/textView34"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MY"
android:textSize="10sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/artwork_board_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView13"
android:layout_width="30dp"
android:layout_height="30dp"
app:srcCompat="#drawable/ic_action_artwork" />
<TextView
android:id="#+id/textView35"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Artwork"
android:textSize="10sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/artist_board_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView14"
android:layout_width="30dp"
android:layout_height="30dp"
app:srcCompat="#drawable/ic_action_artist" />
<TextView
android:id="#+id/textView36"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Artist"
android:textSize="10sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/alarm_board_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView20"
android:layout_width="30dp"
android:layout_height="30dp"
app:srcCompat="#drawable/ic_action_alert" />
<TextView
android:id="#+id/textView40"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alarm"
android:textSize="10sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/menu_board_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView19"
android:layout_width="30dp"
android:layout_height="30dp"
app:srcCompat="#drawable/ic_action_menu" />
<TextView
android:id="#+id/textView38"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="전체"
android:textSize="10sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
MainBoradActivity.java
public class MainBoardActivity extends AppCompatActivity implements View.OnClickListener{
private static LinearLayout my_board_button;
private static LinearLayout artwork_board_button;
private static LinearLayout artist_board_button;
private static LinearLayout menu_board_button;
private static LinearLayout alarm_board_button;
private static FrameLayout container;
private static MyBoardFragment myBoardFragment;
private static ArtworkBoardFragment artworkBoardFragment;
private static ArtistBoardFragment artistBoardFragment;
private static AlarmBoardFragment alarmBoardFragment;
private static MenuBoardFragment menuBoardFragment;
private static FragmentManager fragmentManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_board);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
container = (FrameLayout) findViewById(R.id.container);
my_board_button = (LinearLayout) findViewById(R.id.my_board_button);
artwork_board_button = (LinearLayout) findViewById(R.id.artwork_board_button);
artist_board_button = (LinearLayout) findViewById(R.id.artist_board_button);
menu_board_button = (LinearLayout) findViewById(R.id.menu_board_button);
alarm_board_button = (LinearLayout) findViewById(R.id.alarm_board_button);
my_board_button.setOnClickListener(this);
artwork_board_button.setOnClickListener(this);
artist_board_button.setOnClickListener(this);
menu_board_button.setOnClickListener(this);
alarm_board_button.setOnClickListener(this);
myBoardFragment = new MyBoardFragment();
artworkBoardFragment = new ArtworkBoardFragment();
artistBoardFragment = new ArtistBoardFragment();
alarmBoardFragment = new AlarmBoardFragment();
menuBoardFragment = new MenuBoardFragment();
fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().add(R.id.container, myBoardFragment).commitAllowingStateLoss();
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.my_board_button:
FragmentTransaction myTransaction = fragmentManager.beginTransaction();
myTransaction.replace(R.id.container, myBoardFragment);
myTransaction.addToBackStack("0");
myTransaction.commitAllowingStateLoss();
break;
case R.id.artwork_board_button:
FragmentTransaction artworkTransaction = fragmentManager.beginTransaction();
artworkTransaction.replace(R.id.container, artworkBoardFragment);
artworkTransaction.addToBackStack("1");
artworkTransaction.commitAllowingStateLoss();
break;
case R.id.artist_board_button:
FragmentTransaction artistTransaction = fragmentManager.beginTransaction();
artistTransaction.replace(R.id.container, artistBoardFragment);
artistTransaction.addToBackStack("2");
artistTransaction.commitAllowingStateLoss();
break;
case R.id.alarm_board_button:
FragmentTransaction alarmTransaction = fragmentManager.beginTransaction();
alarmTransaction.replace(R.id.container, alarmBoardFragment);
alarmTransaction.addToBackStack("3");
alarmTransaction.commitAllowingStateLoss();
break;
case R.id.menu_board_button:
FragmentTransaction menuTransaction = fragmentManager.beginTransaction();
menuTransaction.replace(R.id.container, menuBoardFragment);
menuTransaction.addToBackStack("4");
menuTransaction.commitAllowingStateLoss();
break;
}
}
}
ArtistBoardFragment.java
public class ArtistBoardFragment extends Fragment {
private static TabLayout artist_tabs;
private static ViewPager artist_container;
private static FragmentPagerAdapter pageAdapter;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_artist_board, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
artist_tabs = (TabLayout) view.findViewById(R.id.artist_tabs);
artist_container = (ViewPager) view.findViewById(R.id.artist_container);
pageAdapter = new FragmentPagerAdapter(getActivity().getSupportFragmentManager()) {
ArtistNewFragment artistNewFragment = new ArtistNewFragment();
ArtistCategoryFragment artistCategoryFragment = new ArtistCategoryFragment();
private final String[] menuFragmentNames = new String[]{
"new",
"category"
};
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
Bundle recentBundle = new Bundle();
recentBundle.putInt("page", position);
artistNewFragment.setArguments(recentBundle);
return artistNewFragment;
case 1:
Bundle bestBundle = new Bundle();
bestBundle.putInt("page", position);
artistCategoryFragment.setArguments(bestBundle);
return artistCategoryFragment;
default:
return null;
}
}
#Override
public int getCount() {
return menuFragmentNames.length;
}
#Override
public CharSequence getPageTitle(int position) {
return menuFragmentNames[position];
}
};
artist_container.setAdapter(pageAdapter);
artist_container.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
artist_tabs.setupWithViewPager(artist_container);
}
#Override
public void onResume() {
super.onResume();
}
}
ArtistNewFragment.java
public class ArtistNewFragment extends Fragment {
private static RecyclerView new_content_list;
int pastVisibleItems, visibleItemCount, totalItemCount;
private static TimelineAdapter timelineAdapter;
private static RequestManager requestManager;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_artist_new_content, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
new_content_list = (RecyclerView) view.findViewById(R.id.new_content_list);
requestManager = Glide.with(getActivity());
final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL,false);
new_content_list.setLayoutManager(layoutManager);
new_content_list.setHasFixedSize(true);
new_content_list.setNestedScrollingEnabled(false);
//final NotiAdapter notiAdapter = new NotiAdapter(getApplicationContext(), FunctionBase.createFilter, false, lastCheckTime);
timelineAdapter = new TimelineAdapter(getActivity(), requestManager);
timelineAdapter.setObjectsPerPage(3);
new_content_list.setAdapter(timelineAdapter);
new_content_list.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
Log.d("dx", String.valueOf(dx));
Log.d("dy", String.valueOf(dy));
if(dy > 0) {
visibleItemCount = layoutManager.getChildCount();
totalItemCount = layoutManager.getItemCount();
pastVisibleItems = layoutManager.findFirstVisibleItemPosition();
if ( (visibleItemCount + pastVisibleItems) >= totalItemCount) {
timelineAdapter.loadNextPage();
}
}
}
});
}
#Override
public void onResume() {
super.onResume();
timelineAdapter.loadObjects(0);
}
}
It's hard to tell exactly due to the navigational structure of your app, but think the solution to your problem is to use the childFragmentManager inside of the ArtistBoardFragment. i.e, instead of
pageAdapter = new FragmentPagerAdapter(getActivity().getSupportFragmentManager()) ...
Use
pageAdapter = new FragmentPagerAdapter(getChildFragmentManager()) ...
The title of this question is vague because I don't really know what is happening. I hava list view inside a class called SummerJobFragment.java this list view has a onItemClicked() that is suppose to open and other fragment called SummerJobDetailsFragment.java. Below I've posted the code and the logcat screenshot.
SummerJobFragment.java
public class SummerJobsFragmnet extends Fragment {
public SummerJobsFragmnet() {
// Required empty public constructor
}
// TODO: Rename and change types and number of parameters
public static Fragment getInstance() {
Fragment fragment = new SummerJobsFragmnet();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void showMessage (String title, String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final MainActivity activity = (MainActivity) getActivity();
String [] places = activity.getAllPositionsNamesPhone().toArray(
new String[activity.getAllPositionsNamesPhone().size()]);
final ListView list = (ListView) getView().findViewById(R.id.joblistView);
int prgmImages=R.mipmap.ic_launcher;
list.setAdapter(new CustomListAdapter(activity,places,prgmImages));
// OnClick listner for the individual cells of the listView
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
SummerJobDetailsFragment.mMyAppsBundle.putInt("value", position);
SummerJobDetailsFragment fragment = new SummerJobDetailsFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.layout.displayjobs_detailed, fragment).commit();
}
});
}
SummerJobDetailsFragment.java
public class SummerJobDetailsFragment extends Fragment {
DataBaseHelper summerJobDB;
public static Bundle mMyAppsBundle = new Bundle();
public int position = SummerJobDetailsFragment.mMyAppsBundle.getInt("value");
public SummerJobDetailsFragment() {
// Required empty public constructor
}
public static Fragment getInstance() {
Fragment fragment = new SummerJobsFragmnet();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.displayjobs_detailed,container,false);
super.onViewCreated(view, savedInstanceState);
SummerJobDetailsFragment summer = new SummerJobDetailsFragment();
DataBaseHelper summerJobDB;
summerJobDB = new DataBaseHelper(getActivity());
Cursor res = summerJobDB.getAllData(position+1);
EditText jobPlace = (EditText)view.findViewById(R.id.jobTitle);
jobPlace.setText(res.getString(1));
/*
EditText jobPlace = (EditText)summer.getView().findViewById(R.id.jobTitle);
jobPlace.setText(res.getString(1));
EditText jobPosition = (EditText)summer.getView().findViewById(R.id.jobPlace);
jobPlace.setText(res.getString(2));
EditText starTime = (EditText)summer.getView().findViewById(R.id.jobStartingTime);
jobPlace.setText(res.getString(3));
EditText address = (EditText)summer.getView().findViewById(R.id.jobAddress);
jobPlace.setText(res.getString(5));
EditText phone = (EditText)summer.getView().findViewById(R.id.jobPhoneNum);
jobPlace.setText(res.getString(6));
EditText hours = (EditText)summer.getView().findViewById(R.id.jobHours);
jobPlace.setText(res.getString(4));
*/
return
}
}
and here is the displayjobs_detail.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/LinearLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/jobplace"
android:id="#+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/jobPlace"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Job Title"
android:id="#+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/jobTitle"
android:layout_gravity="right" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Job Address:"
android:id="#+id/jobAddress" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/jobAddress" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Phone number:"
android:id="#+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/jobPhoneNum"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Hours"
android:id="#+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/jobHours" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Starting Time"
android:id="#+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/jobStartingTime" />
</LinearLayout>
and here is the logcat:
First of all you are doing the layout operations in onCreate, you should do it in onCreateView() like this :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_songs, container, false);
EditText jobPlace = (EditText)rootView.findViewById(R.id.jobTitle);
jobPlace.setText(res.getString(1));
...
...
}
Also remove this statement
SummerJobDetailsFragment summer = new SummerJobDetailsFragment();
it doesn't make any sense, you call findViewById on the rootView.
No, this is how you do it,
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/displayjobs_detailed"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/LinearLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/jobplace"
android:id="#+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/jobPlace"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Job Title"
android:id="#+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/jobTitle"
android:layout_gravity="right" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Job Address:"
android:id="#+id/jobAddress" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/jobAddress" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Phone number:"
android:id="#+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/jobPhoneNum"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Hours"
android:id="#+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/jobHours" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Starting Time"
android:id="#+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/jobStartingTime" />
</LinearLayout>
</FrameLayout>
Then in your fragment use this to commit
fragmentManager.beginTransaction().replace(R.id.displayjobs_detailed, fragment).commit(); // Note R.id instead of R.layout
Initialize your view items in onCreateView of the Fragment not in onCreate
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.your_layout, container, false);
// Initialize Here
return view;
}
You are calling wrong. Difference between onCreateView and onViewCreated in Fragment
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
Instead of, call
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.your_layout, container, false);
// Initialize Here
return view;
}
I have custom dialog to show which has instruction about how to use the app.I am using ViewPager for this inside my custom dialog layout.
I am getting error
java.lang.IllegalArgumentException: No view found for id for fragment FragmentForInstruction1.
I have created a method which is called in onCreate(Bundle savedInstanceState) {} method .The method inflates the layout for dialog.
private void showCustomDialogForInstruction() {
final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dialogLayout = inflater.inflate(R.layout.dialog_layout_for_instruction_message, null, false);
layout.setAlpha(0.2f);
dialog.setCanceledOnTouchOutside(false);
dialog.setContentView(dialogLayout);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
ViewPager viewPager=(ViewPager) dialog.findViewById(R.id.pagerInstruction);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new FragmentForInstruction1());
adapter.addFrag(new Fragme`enter code here`ntForWelcomePage2());
adapter.addFrag(new FragmentForWelcomePage3());
adapter.addFrag(new FragmentForWelcomePage4());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new OnPageChangeListener()
{
#Override
public void onPageSelected(int pos)
{
}
#Override
public void onPageScrolled(int pos, float arg1, int arg2)
{
}
#Override
public void onPageScrollStateChanged(int arg0)
{
}
});
Button done = (Button) dialog.findViewById(R.id.done);
done.setText("Got It");
done.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
Button neverShow = (Button) dialog.findViewById(R.id.nevershow);
neverShow.setText("Never Show Again");
neverShow.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
return false;
}
});
dialog.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.show();
}
my view pager class:
public class ViewPagerAdapter extends FragmentPagerAdapter
{
private final List<Fragment> mFragmentList = new ArrayList<Fragment>();
public ViewPagerAdapter(FragmentManager manager)
{
super(manager);
}
#Override
public Fragment getItem(int position)
{
return mFragmentList.get(position);
}
#Override
public int getCount()
{
return mFragmentList.size();
}
public void addFrag(Fragment fragment)
{
mFragmentList.add(fragment);
}
}
and i call the method in MainActivity :
public class MainActivity extends AppCompatActivity {
SharedPreferences sharedPreferencesNeverShowAgain ;
boolean neverShowAgain;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedPreferencesNeverShowAgain = PreferenceManager.getDefaultSharedPreferences(this);
neverShowAgain = sharedPreferencesNeverShowAgain.getBoolean("NeverShowAgain", false);
if(!neverShowAgain){
showCustomDialogForMessage();
}
}
}
and the layout for dialog is:
<?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="wrap_content"
android:background="#drawable/dialog_shape"
android:orientation="vertical" >
<!-- layout title -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#00000000"
android:gravity="center"
android:orientation="vertical" >
<ViewFlipper
android:id="#+id/flipper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/top_edge_rounded"
android:flipInterval="2000"
android:padding="20dp" >
<TextView
android:id="#+id/dialog_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:gravity="center"
android:text="Input Height"
android:textColor="#color/textColor"
android:textStyle="normal" />
<TextView
android:id="#+id/title1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:gravity="center"
android:text="Input Height"
android:textColor="#color/accent"
android:textStyle="normal" />
</ViewFlipper>
<View
android:id="#+id/tri"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/triangle"
android:rotation="180" />
</LinearLayout>
<!-- layout dialog content -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="300dp" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<android.support.v4.view.ViewPager
android:id="#+id/pagerInstruction"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="#+id/layoutIndicater"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:orientation="horizontal" >
</LinearLayout>
</RelativeLayout>
</ScrollView>
<!-- layout dialog buttons -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_margin="10dp"
android:background="#drawable/all_rounded_edge_plum_for_dialog" >
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentTop="true"
android:background="#color/textColor" />
<View
android:id="#+id/ViewColorPickerHelper"
android:layout_width="1dp"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#color/textColor" />
<Button
android:id="#+id/nevershow"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="#id/ViewColorPickerHelper"
android:background="?android:attr/selectableItemBackground"
android:padding="5dp"
android:text="Close"
android:textColor="#color/textColor" />
<Button
android:id="#+id/done"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/ViewColorPickerHelper"
android:background="?android:attr/selectableItemBackground"
android:padding="5dp"
android:text="Done"
android:textColor="#color/textColor" />
</RelativeLayout>
</LinearLayout>
FragmentForInstruction1 code:
public class FragmentForInstruction1 extends Fragment{
#Override
#Nullable
public View onCreateView(LayoutInflater inflater,
#Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.fragmentinstructionpage1, container,false);
return view;
}
}
and its layout is:
<?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"
android:background="#color/backgroundColor"
android:gravity="center" >
<TextView
android:id="#+id/titleWelcomeFragment1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#color/colorPrimary"
android:textSize="30sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/messageWelcomeFragment1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#color/colorPrimary"
android:textSize="20sp"
android:textStyle="normal"
android:paddingLeft="20dp"
android:paddingRight="20dp"/>
</LinearLayout>
You should create an adapter that extends FragmentPagerAdapter:
private class CustomPagerAdapter extends FragmentPagerAdapter {
public CustomPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new FragmentForInstruction1();
case 1:
return new FragmentForInstruction2();
case 2:
return new FragmentForInstruction3();
case 3:
return new FragmentForInstruction4();
default:
return new FragmentForInstruction1();
}
}
#Override
public int getCount() {
return 4;
}
}
While crating an instance of it, pass the getChildFragmentManager() instead of getSupportFragmentManager():
viewPager.setAdapter(new CustomPagerAdapter(getChildFragmentManager()));
Halo guys, I followed the android developer steps:http://developer.android.com/training/animation/screen-slide.html
How can I call get the "btnRight" & "btnLeft" in MainJava to create a onClick function?
i have no idea about that, (???).findViewByID ?
btw, is there an another better coding design to create this effect?
i wanna create 3 page that can slide or maybe click to change screen, did i use this viewpager incorrectly? Is it probably add some new view instants of using the pagenumber trick?
please help me a bit, thanks!!
MainJava.java:
public class MainJava extends FragmentActivity {
private static final int NUM_PAGES = 3;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
actionBar.hide();
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());
mPager.setAdapter(mPagerAdapter);
mPager.setCurrentItem(1);
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return ScreenSlidePageFragment.create(position);
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
}
ScreenSlidePageFragment.java:
public class ScreenSlidePageFragment extends Fragment {
public static final String ARG_PAGE = "page";
private int mPageNumber;
public static ScreenSlidePageFragment create(int pageNumber) {
ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
Bundle args = new Bundle();
args.putInt(ARG_PAGE, pageNumber);
fragment.setArguments(args);
return fragment;
}
public ScreenSlidePageFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPageNumber = getArguments().getInt(ARG_PAGE);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout containing a title and body text.
ViewGroup rootView = (ViewGroup) inflater
.inflate(R.layout.scroll_view_content, container, false);
//rootView.addView(inflater.inflate(R.layout.scroll_view_content, null));
View pageLeft = (View) rootView.findViewById(R.id.pageLeft);
View pageRight = (View) rootView.findViewById(R.id.pageRight);
View pageMain = (View) rootView.findViewById(R.id.pageMain);
if (mPageNumber==0){
pageLeft.setVisibility(View.VISIBLE);
pageMain.setVisibility(View.GONE);
pageRight.setVisibility(View.GONE);
}else if (mPageNumber==1){
pageLeft.setVisibility(View.GONE);
pageMain.setVisibility(View.VISIBLE);
pageRight.setVisibility(View.GONE);
}else if (mPageNumber==2){
pageLeft.setVisibility(View.GONE);
pageMain.setVisibility(View.GONE);
pageRight.setVisibility(View.VISIBLE);
}
return rootView;
}
public int getPageNumber() {
return mPageNumber;
}
scroll_view_content.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- Dummy content. -->
<LinearLayout
android:id="#+id/pageLeft"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
style="?android:textAppearanceMedium"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:lineSpacingMultiplier="1.2"
android:text="AAA" />
</LinearLayout>
<LinearLayout
android:id="#+id/pageRight"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
style="?android:textAppearanceMedium"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:lineSpacingMultiplier="1.2"
android:text="CCC"/>
</LinearLayout>
<RelativeLayout
android:id="#+id/pageMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#123456"
android:orientation="vertical" >
<TextView
android:id="#+id/textB"
style="?android:textAppearanceMedium"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lineSpacingMultiplier="1.2"
android:background="#654321"
android:text="BBB"/>
<LinearLayout
android:layout_below="#+id/textB"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<Button
android:id="#+id/btnLeft"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#00ffff"
android:text="A"/>
<Button
android:id="#+id/btnRight"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ffff00"
android:text="B"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
Thats simple really. You have to use the setCurrentItem(int) of the view pager class. The algorithm would be:
int currPage=1 //Points to second page in the viewpager
if left button clicked //listener for the button
2a. currPage--
2b. mPager.setCurrentItem(currPage) //pager is object of your viewpager class
if right button clicked //listener for the button
3a. currPage++
3b. mPpager.setCurrentItem(currPage)
Check out this link for for info.