Viewpager cannot change imageView background programmatically - android

I am trying to initialize a ViewPager. I've failed to set image of programmatically to the viewpager. This is what I've come up with:
TutorialActivity.class
package app.trial.com;
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;
public class TutorialActivity extends FragmentActivity {
/**
* The number of pages (wizard steps) to show in this demo.
*/
private static final int NUM_PAGES = 3;
/**
* The pager widget, which handles animation and allows swiping horizontally to access previous
* and next wizard steps.
*/
private ViewPager mPager;
/**
* The pager adapter, which provides the pages to the view pager widget.
*/
private PagerAdapter mPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tutorial);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
}
#Override
public void onBackPressed() {
if (mPager.getCurrentItem() == 0) {
// If the user is currently looking at the first step, allow the system to handle the
// Back button. This calls finish() on this activity and pops the back stack.
super.onBackPressed();
} else {
// Otherwise, select the previous step.
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}
/**
* A simple pager adapter that represents 5 ScreenSlidePageFragment objects, in
* sequence.
*/
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return TutorialFragment.create(position);
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
}
TutorialFragment.class
package app.trial.com;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class TutorialFragment extends Fragment {
public static final String ARG_PAGE = "page";
private int mPageNumber;
public static TutorialFragment create(int pageNumber) {
TutorialFragment fragment = new TutorialFragment();
Bundle args = new Bundle();
args.putInt(ARG_PAGE, pageNumber);
fragment.setArguments(args);
return fragment;
}
public TutorialFragment() {}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mPageNumber = getArguments().getInt(ARG_PAGE);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_tutorial, container, false);
switch(mPageNumber) {
case 1:
((ImageView) rootView.findViewById(R.id.tutorialImage)).setImageResource(R.drawable.tutorial_03);
break;
case 2:
((ImageView) rootView.findViewById(R.id.tutorialImage)).setImageResource(R.drawable.tutorial_02);
break;
case 3:
((ImageView) rootView.findViewById(R.id.tutorialImage)).setImageResource(R.drawable.tutorial_03);
break;
default:
break;
}
return rootView;
}
/**
* Returns the page number represented by this fragment object.
*/
public int getPageNumber() {
return mPageNumber;
}
}
When I try to change R.id.tutorialImage using rootView.findViewById(R.id.tutorialImage).setImageResource() it doesn't change the background image. Also, it doesn't return null.
By the way my fragmentTutorial layout is:
<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" tools:context="app.trial.com.TutorialFragment">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tutorialImage" />
</ScrollView>
</FrameLayout>

Related

Can't set adapter of ListView because the ListView has a null object reference

I am trying to make a TabLayout with a ViewPager, but whenever I leave the app (Don't close it, just switch to another app and back) the fragments kind of get killed. I don't know that for sure, but the fragment's need to reCreate, and then fail.
The code for creating the TabLayout, TabLayoutAdapter and the ViewPager:
TabLayout tabLayout = (TabLayout) findViewById(R.id.tablayout);
MyTabLayoutAdapter tabLayoutAdapter;
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
tabLayout.addTab(tabLayout.newTab().setText("-2"));
tabLayout.addTab(tabLayout.newTab().setText("-1"));
tabLayout.addTab(tabLayout.newTab().setText("0"));
tabLayout.addTab(tabLayout.newTab().setText("1"));
tabLayout.addTab(tabLayout.newTab().setText("2"));
tabLayoutAdapter = new MyTabLayoutAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(tabLayoutAdapter);
viewPager.setOffscreenPageLimit(3);
tabLayout.setupWithViewPager(viewPager);
viewPager.setCurrentItem(2);
The code of my TabLayoutAdapter:
public class MyTabLayoutAdapter extends FragmentStatePagerAdapter {
private MyFragment fragmentOne;
private MyFragment fragmentTwo;
private MyFragment fragmentDefault;
private MyFragment fragmentFour;
private MyFragment fragmentFive;
private int numberOfTabs;
public MyTabLayoutAdapter(FragmentManager fragmentManager, int numberOfTabs) {
super(fragmentManager);
fragmentOne = new MyFragment();
fragmentTwo = new MyFragment();
fragmentDefault = new MyFragment();
fragmentFour = new MyFragment();
fragmentFive = new MyFragment();
this.numberOfTabs = numberOfTabs;
}
#Override
public MyFragment getItem(int position) {
switch (position) {
case 0:
return fragmentOne;
case 1:
return fragmentTwo;
case 2:
return fragmentDefault;
case 3:
return fragmentFour;
case 4:
return fragmentFive;
default:
return fragmentDefault;
}
}
#Override
public int getCount() {
return numberOfTabs;
}
}
The MyFragment fragment:
public class MyFragment extends Fragment {
private View currentView;
private ListView listView;
public MyFragment() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
currentView = LayoutInflater.from(getContext()).inflate(R.layout.my_fragment_layout, container, false);
listView = (ListView) currentView.findViewById(R.id.listview);
return currentView;
}
//This line throws a NullPointer when I resume the app
public void populateListView(MyListAdapter myListAdapter) {
listView.setAdapter(myListAdapter);
}
}
Whenever I call
MyListAdapter myListAdapter = new MyListAdapter(this, R.layout.my_item, justSomeStringArray);
tabLayoutAdapter.getItem(0).populateListView(myListAdapter);
I get a NullPointerException saying that it can't set a adapter on a null object reference.
Anyone any idea how I can fix this?
Here's an approach which lets the Fragment instance drive the data retrieval by using an AsyncTask implementation (GetDataForListViewTask).
But, to follow Commonsware's advice, GetDataForListViewTask merely obtains the data. It does not set up the adapter for use on the ListViews. Instead, setting up the ListAdapter is done in the Fragment itself.
GetDataForListViewTask passes the data back to the Fragment instance using EventBus. This keeps GetDataForListViewTask from having to maintain a reference to the ListView, Activity, Context or any of that risky stuff.
And, by not "reaching in" to a Fragment from the outside to change its widgets or even hand it data, this code also tries (very broadly) to follow another piece of CommonsWare's guidance:
In general, having code outside of a fragment attempt to manipulate
the widgets inside that fragment — as you are doing here — is a bad
idea.
In any case, if you use this approach, which is a bit different from your original approach, you'll want to modify the GetDataForListViewTask so that it obtains the data from elsewhere in your app or the network, etc.
GetDataForListViewTask.java
import android.os.AsyncTask;
import org.greenrobot.eventbus.EventBus;
import java.util.ArrayList;
class GetDataForListViewTask extends AsyncTask<Void, Void, ArrayList<String>> {
#Override
protected ArrayList<String> doInBackground(Void... ignored) {
// since this method runs on a worker thread, you may
// replace this code with a network call or a background computation
// from another part of the app is ready
ArrayList<String> listItems = new ArrayList<>();
for (int i=0; i<10; i++) {
listItems.add("item " + i);
}
return listItems;
}
#Override
protected void onPostExecute(ArrayList<String> result) {
super.onPostExecute(result);
// post event so that Fragment can use the data to populate its ListView
EventBus.getDefault().post(new MyFragment.ListViewDataReadyEvent(result));
}
}
MainActivity.java
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tablayout);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
MyAdapter tabLayoutAdapter = new MyAdapter(getSupportFragmentManager(), 5);
viewPager.setAdapter(tabLayoutAdapter);
viewPager.setOffscreenPageLimit(3);
tabLayout.setupWithViewPager(viewPager);
viewPager.setCurrentItem(2);
}
class MyAdapter extends FragmentStatePagerAdapter {
private final int count;
public MyAdapter(FragmentManager fm, int count) {
super(fm);
this.count = count;
}
#Override
public MyFragment getItem(int position) {
return MyFragment.newInstance();
}
#Override
public int getCount() {
return this.count;
}
#Override
public CharSequence getPageTitle(int position) {
return "Tab " + position;
}
}
}
MyFragment.java
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.ArrayAdapter;
import android.widget.ListView;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.util.ArrayList;
public class MyFragment extends Fragment {
private ListView listView;
private GetDataForListViewTask getDataForListViewTask;
public static MyFragment newInstance() {
return new MyFragment();
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true); // to support use of GetDataForListViewTask
EventBus.getDefault().register(this);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
listView = (ListView) LayoutInflater.from(getContext()).inflate(
R.layout.my_fragment_layout, container, false);
getDataForListViewTask = new GetDataForListViewTask();
getDataForListViewTask.execute();
return listView;
}
#Override
public void onDestroy() {
if (getDataForListViewTask != null) {
getDataForListViewTask.cancel(false);
}
EventBus.getDefault().unregister(this);
super.onDestroy();
}
static class ListViewDataReadyEvent {
ArrayList<String> data;
ListViewDataReadyEvent(ArrayList<String> data) {
this.data = data;
}
}
#SuppressWarnings("unused")
#Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(ListViewDataReadyEvent event) {
if (listView != null) {
ArrayAdapter<String> listAdapter = new ArrayAdapter<>(listView.getContext(),
android.R.layout.simple_list_item_1, event.data.toArray(new String[]{}));
listView.setAdapter(listAdapter);
}
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:padding="4dip"
android:gravity="center_horizontal"
android:layout_width="match_parent" android:layout_height="match_parent">
<android.support.design.widget.TabLayout
android:id="#+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill" />
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</LinearLayout>
my_fragment_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView android:id="#+id/listview"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
</ListView>
Selected compile statements for build.gradle
compile 'org.greenrobot:eventbus:3.0.0'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:appcompat-v7:25.3.1'
onCreateView() is not called on a fragment until the fragment is being added to a FragmentManager. Your final code snippet will only work, at best, if it is called sometime after fragmentOne is added to a FragmentManager. Otherwise, the fragment's ListView will not exist yet.
In general, having code outside of a fragment attempt to manipulate the widgets inside that fragment — as you are doing here — is a bad idea.

Switch fragment within designated space

I have a layout, MainScreen.java which has following design.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/toolbar"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/tab_and_view" />
</LinearLayout>
toolbar will be the title of the app. tab_and_view will consist of a tabview and framelayout
Here's the layout file of tab_and_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.android.luftschlafer.engdictionary.Tab.SlidingTabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorAccent"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1" />
</LinearLayout>
I have made some tabs: Home,Search, Settings and About app. All of them will display their content in the ViewPager.
In Home page, there is only a ListView item.
<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"
tools:context=".Corr_Pages.HomePage">
<!-- TODO: Update blank fragment layout -->
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/home_function_list"/>
</RelativeLayout>
And codes of HomePage.java:
public class HomePage extends BaseFragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "Home";
private static final String ind_color="IndicatorColor";
private static final String div_color="DividerColor";
// TODO: Rename and change types of parameters
private String mParam1;
private int mParam2,mParam3;
private OnFragmentInteractionListener mListener;
public HomePage() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* #param tab_name Parameter 1.
* #return A new instance of fragment HomePage.
*/
// TODO: Rename and change types and number of parameters
public static HomePage newInstance(String tab_name, int indicatorColor,int
dividerColor,int iconResId) {
HomePage fragment = new HomePage();
fragment.setTitle(tab_name);
fragment.setIndicatorColor(indicatorColor);
fragment.setDividerColor(dividerColor);
fragment.setIconResId(iconResId);
//Pass out data
Bundle args = new Bundle();
args.putString(ARG_PARAM1, tab_name);
args.putInt(ind_color,indicatorColor);
args.putInt(div_color,dividerColor);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2=getArguments().getInt(ind_color);
mParam3=getArguments().getInt(div_color);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home_page, container, false);
}
//This method will be done by
#Override
public void onViewCreated(View v, final Bundle savedInstanceState){
//Initialize all visual elements here
final LayoutInflater inflater= LayoutInflater.from(getContext());//Test
ListView lv=(ListView) v.findViewById(R.id.home_function_list);
//Initialize the list items and their corresponding adapter
String[] SampleSelections=new String[] {"New things","Most common words","Recommendation from Wilson"};
ArrayAdapter<String> adapter= new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1, SampleSelections);
lv.setAdapter(adapter);
//Declare the events
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position){
//Toasts will be replaced by appropriate methods soon
case 0:
Toast.makeText(getContext(),"New Contents are coming",Toast.LENGTH_SHORT).show();
declare_new_content_page();
break;
case 1:
Toast.makeText(getContext(),"There are some most common words",Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(getContext(),"Wilson's recommendation",Toast.LENGTH_SHORT).show();
break;
default:
//Nothing is selected
break;
}
}
});
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
#Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
//Handles the operation of new content page.
private void declare_new_content_page(){
//Declare widgets here
FragmentManager fm=getFragmentManager();
FragmentTransaction ftran=fm.beginTransaction();
ftran.replace(R.id.tab_and_view,NewContent.newInstance("NewContent"));
ftran.commit();
}
}
In the ListView of MainScreen.java, it has three choices: What's new, most common words and recommendation. Now if I press What's new, a new fragment should be displayed in the ViewPager with a name NewContent.java. I tried to do this by using following codes:
FragmentManager fm=getFragmentManager();
FragmentTransaction ftran=fm.beginTransaction();
ftran.replace(R.id.tab_and_view,NewContent.newInstance("NewContent"));
ftran.commit();
However, after the transaction is completed, the new fragment does not remain in viewPager anymore, it also used the spaces for tabview too.
Therefore I want to know how to switch fragment within this designated ViewPager space.
In the new fragment, NewContent.java, it also has a button for users to go back to home page. I would also like to ask how to finish this operation?
I'm new to stackoverflow, so I hope you know what problem i'm facing.... Thank you.
Another fragment file, TabFragment.java, i used it to build the tabs.
package com.android.luftschlafer.engdictionary;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.android.luftschlafer.engdictionary.Corr_Pages.AboutPage;
import com.android.luftschlafer.engdictionary.Corr_Pages.SearchPage;
import com.android.luftschlafer.engdictionary.Corr_Pages.SettingPage;
import com.android.luftschlafer.engdictionary.Tab.BaseFragment;
import com.android.luftschlafer.engdictionary.Tab.SlidingTabLayout;
import com.android.luftschlafer.engdictionary.Corr_Pages.HomePage;
import java.util.LinkedList;
/**
* Created by LuftSchlafer on 6/17/2016.
*/
//Configurations of all fragments
public class TabFragment extends Fragment {
private SlidingTabLayout tabs;
private ViewPager pager;
private FragmentPagerAdapter adapter;
public static Fragment newInstance(){
TabFragment f = new TabFragment();
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.tab_and_view, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
//adapter
final LinkedList<BaseFragment> fragments = getFragments();
adapter = new TabFragmentPagerAdapter(getFragmentManager(), fragments);
//pager
pager = (ViewPager) view.findViewById(R.id.pager);
pager.setAdapter(adapter);
//tabs
tabs = (SlidingTabLayout) view.findViewById(R.id.tabs);
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
#Override
public int getIndicatorColor(int position) {
return fragments.get(position).getIndicatorColor();
}
#Override
public int getDividerColor(int position) {
return fragments.get(position).getDividerColor();
}
});
tabs.setBackgroundResource(R.color.colorAccent);
tabs.setCustomTabView(R.layout.tab_title, R.id.txtTabTitle,
R.id.imgTabIcon);//REMEMBER THE SEQUENCE!
tabs.setViewPager(pager);
}
//Produce the fragments and corresponding tabs.
private LinkedList<BaseFragment> getFragments(){
int indicatorColor = Color.parseColor("RED");
int dividerColor = Color.TRANSPARENT;
LinkedList<BaseFragment> fragments = new LinkedList<>();
//Add fragments here
fragments.add(HomePage.newInstance("Home", indicatorColor,
dividerColor,R.drawable.ic_home_black_24dp));
fragments.add(SearchPage.newInstance("Search", indicatorColor,
dividerColor,R.drawable.ic_book_black_24dp));
fragments.add(SettingPage.newInstance("Settings", indicatorColor,
dividerColor,R.drawable.ic_build_black_24dp));
fragments.add(AboutPage.newInstance("About
app",indicatorColor,dividerColor,R.drawable.ic_info_outline_black_24dp));
return fragments;
}
}

Android Replace Fragment in View Pager

I'm new to Android development and Java outside of simple JavaScripts for web pages. I've created a view pager that displays three different fragments. For the second fragment, BooksFragment, I use a grid view to display a grid of buttons that when the user presses should replace BooksFragment with another view.
The problem I run into is that although I'm using .replace() to replace the fragment, the second fragment just shows up on top of the original. Now, I've searched my problem extensively and I believe the reason that usually happens is when the original fragment is not created dynamically. Being the noob that I am, it does seem like I AM creating the fragments dynamically but I could be mistaken (I'm not using any tags in any of my XML files). Can anyone please help me out? My code is below:
MainActivity.java
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.app.FragmentManager;
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
// strings for tabs
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// tab titles
private String[] tabs = {"Status",
"Schedule",
"Books"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
//actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.pager, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
TabsPagerAdapter.java
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class TabsPagerAdapter extends FragmentPagerAdapter{
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
// First fragment activity
return new StatusFragment();
case 1:
// Second fragment activity
return new ScheduleFragment();
case 2:
// Third fragment activity
return new BooksFragment();
}
return null;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 3;
}
}
BooksFragment.java
import java.util.ArrayList;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class BooksFragment extends Fragment implements OnClickListener {
// get database data
private static final String DB_NAME = "bookReadingTracker";
private static final String TABLE_NAME = "book_names";
private static final String BOOK_NAME_ID = "_id";
private static final String BOOKNAME = "bookname";
private static final String BOOKABBR = "bookabbr";
private SQLiteDatabase database;
private ArrayList<String> allbooksabbr;
private OnItemSelectedListener listener;
GridView gridView;
public static BooksFragment newInstance() {
BooksFragment f = new BooksFragment();
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ExternalDbOpenHelper dbOpenHelper = new ExternalDbOpenHelper(getActivity(), DB_NAME);
database = dbOpenHelper.openDataBase();
fillAllbooksabbr();
View view = inflater.inflate(R.layout.books_fragment, container, false);
gridView = (GridView) view.findViewById(R.id.gridviewBooks);
gridView.setAdapter(new MyAdapter(getActivity(), allbooksabbr));
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
///////////////////
Fragment chapterDetailFragment = ChaptersFragment.newInstance();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.addToBackStack(null);
transaction.replace(R.id.booksFragmentHolder, chapterDetailFragment).commit();
/////////////////////
}
});
return view;
}
private void fillAllbooksabbr() {
allbooksabbr = new ArrayList<String>();
Cursor allbooksabbrCursor = database.query(TABLE_NAME, new String[] {BOOK_NAME_ID, BOOKABBR},
null, null, null, null, "CAST (" + BOOK_NAME_ID + " AS INTEGER)");
allbooksabbrCursor.moveToFirst();
if(!allbooksabbrCursor.isAfterLast()) {
do {
String bookabbr_array = allbooksabbrCursor.getString(1);
allbooksabbr.add(bookabbr_array);
} while (allbooksabbrCursor.moveToNext());
}
allbooksabbrCursor.close();
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
MyAdapter.java
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
private Context context;
private ArrayList<String> allbooksabbr;
public MyAdapter(Context context, ArrayList<String> allbooksabbr) {
this.context = context;
this.allbooksabbr = allbooksabbr;
}
public View getView(int position, View convertView, ViewGroup parent) {
View gridView;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
gridView = inflater.inflate(R.layout.books_button, null);
} else {
gridView = (View) convertView;
}
//gridView = new View(context);
gridView.setBackgroundColor(0xff645C69);
TextView bookButton = (TextView) gridView.findViewById(R.id.bookbutton);
bookButton.setText(allbooksabbr.get(position));
return gridView;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return allbooksabbr.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}
Here are my XML files:
fragment_main.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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.gclapps.bookreadingscheduletracker.MainActivity$PlaceholderFragment" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
books_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/booksFragmentHolder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="#+id/textViewBooks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Books"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#666666"
android:paddingTop="10dp"
android:paddingBottom="5dp" />
<GridView
android:id="#+id/gridviewBooks"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="60dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="2dp"
android:horizontalSpacing="2dp" >
</GridView>
</FrameLayout>
Please help me find the reason why my BooksFragment view doesn't get replaced with my ChaptersFragment view. If the BooksFragment view is indeed not created dynamically, please help me do so. I've read the documentation on how to do it, but am not able to figure out how to do it in my case with using the viewpager. Thanks!
Ok so after a quick look through your code, I noticed you used getChildFragmentManager() here:
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
getChildFragmentManager() manages fragments that are children of the current fragment. So when you call replace() , it doesn't actually remove the displayed BookFragment since it is not a child of the current fragment (the current fragment IS the BookFragment).
So normally to replace the fragment, I'd call getSupportFragmentManager() to get the right manager, but since you're using a ViewPager it makes things more difficult. You'd probably need to somehow change the ViewPager adapter on gridview item click with a new adapter that returns ChapterDetailFragment in its getItem() method.
You might be able to do something like this:
public class TabsPagerAdapter extends FragmentPagerAdapter{
private boolean isChapterDetailShowing;
private int chapter;
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
// First fragment activity
return new StatusFragment();
case 1:
// Second fragment activity
return new ScheduleFragment();
case 2:
// Third fragment activity
if (isChapterDetailShowing)
return new ChapterDetailFragment();
else
return new BooksFragment();
}
return null;
}
public void showChapter(int chapter) {
this.chapter = chapter;
isChapterDetailShowing = true;
notifyDataSetChanged();
}
Then create a method in the Activity, which calls the showChapter() on the adapter. This method would be called in BookFragment's GridView onItemClickListener (use getActivity() and cast it to MainActivity).
Disclaimer: not 100% sure this will work

Android : Fragment code causes crash of the application

This is my Menu java code that include fragments in a swipe view interface where i had separate activity for each fragment. I encounter a application class by calling this activity and i wish that someone could help me to solve my problem as i'm a beginner in android programming. Your help is very appreciated.
package com.example.lazynumber;
import java.util.Locale;
import android.annotation.SuppressLint;
import android.content.Intent;
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.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class MainMenu extends FragmentActivity {
/**
* The {#link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {#link android.support.v4.app.FragmentPagerAdapter} derivative, which
* will keep every loaded fragment in memory. If this becomes too memory
* intensive, it may be best to switch to a
* {#link android.support.v4.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {#link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_menu);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
switch(position){
case 0:
Fragment fragment = new GetNumberFragment();
return fragment;
case 1:
Fragment fragment2 = new QRFragment();
return fragment2;
case 2:
Fragment fragment3 = new ProfileFragment();
return fragment3;
default:
return null;
}
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class GetNumberFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public GetNumberFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_get_number_intro,
container, false);
return rootView;
}
}
#SuppressLint("ValidFragment")
public class QRFragment extends Fragment{
public QRFragment(){};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView2 = inflater.inflate(R.layout.qrintro, container, false);
Button testingButton = (Button)findViewById(R.id.goinQRPay);
testingButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
goTesting();
}
});
return rootView2;
}
}
private void goTesting(){
Intent goTesting = new Intent(this, Testingbutton.class);
startActivity(goTesting);
}
public static class ProfileFragment extends Fragment{
public ProfileFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView3 = inflater.inflate(R.layout.activity_profile_setting, container, false);
return rootView3;
}
}
}
These are my xml codes for reference if needed.
first one is the mainmenu xml
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainMenu" >
<!--
This title strip will display the currently visible page title, as well as the page
titles for adjacent pages.
-->
<android.support.v4.view.PagerTitleStrip
android:id="#+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#fff" />
</android.support.v4.view.ViewPager>
And this next xml is the one that i needed to have a function which is QRFragment activity xml interface.
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".QRIntro" >
<ImageView
android:id="#+id/qrintropic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/qrlazy" />
<Button
android:id="#+id/goinQRPay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/qrintropic"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="#string/buttontoqr" />"
</RelativeLayout>
The fragments need to be inserted into some kind of container layout like a FrameLayout. if you add that then you can use this method to replace the fragment
public static void replaceFragment(Activity activity, int containerlayoutId, Fragment newFragment) {
FragmentTransaction transaction = activity.getFragmentManager().beginTransaction().replace(containerlayoutId, newFragment);
transaction.commit();
activity.getFragmentManager().executePendingTransactions();
}

How to write button click event in ViewPager

Here my activity_main.xml present in /res/layout
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<!--
This title strip will display the currently visible page title, as well as the page
titles for adjacent pages.
-->
<android.support.v4.view.PagerTitleStrip
android:id="#+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#fff" />
</android.support.v4.view.ViewPager>
Here is my MainActivity.java file
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;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
public class MainActivity extends FragmentActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {#link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount() {
// Show 3 total pages.
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "POS 0"; // getString(R.string.title_section1).toUpperCase();
case 1:
return "POS 1";// getString(R.string.title_section2).toUpperCase();
case 2:
return "POS 2";// getString(R.string.title_section3).toUpperCase();
}
return null;
}
}
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view;
String i = Integer.toString(getArguments().getInt(
ARG_SECTION_NUMBER));
if (i.toString() == Integer.toString(1)) {
view = inflater.inflate(R.layout.section2, container, false);
} else {
view = inflater.inflate(R.layout.section1, container, false);
// Button button = (Button) view.findViewById(R.id.button1);
// button.setOnClickListener(new OnClickListener() {
// if i uncomment this code, button click works but i want it to handle in section1.java file
// #Override
// public void onClick(View v) {
// Activity activity = getActivity();
//
// if (activity != null) {
// Toast.makeText(activity, "sdfsdfsdfds",
// Toast.LENGTH_LONG).show();
// }
// }
//
// });
}
return view;
}
}
}
I have an activity named "Section1" which is displayed when i swipe to right side. It has a button on it.
How do i handle the button click event in section1.java file ?
here is my section.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".Section1" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendMessage"
android:text="Button" />
</RelativeLayout>
Here is the Section1.Java file. Here button click even it not fired.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class Section1 extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.section1);
}
public void sendMessage(View view) {
Toast t = Toast.makeText(getApplicationContext(), "sdfsdfsdf",
Toast.LENGTH_LONG);
t.show();
}
}
I was having the same issue as you and I finally figured it out today so I thought I would share it with you
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
switch (i) {
case 2:
// The first section of the app is the most interesting -- it offers
// a launchpad into the other demonstrations in this example application.
return new LessonFragment();
default:
// The other sections of the app are dummy placeholders.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
fragment.setArguments(args);
return fragment;
}
}
#Override
public int getCount() {
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0: return getString(R.string.title_section1).toUpperCase();
case 1: return getString(R.string.title_section2).toUpperCase();
case 2: return getString(R.string.title_section3).toUpperCase();
case 3: return getString(R.string.title_section4).toUpperCase();
}
return null;
}
}
/**
* A fragment that launches other parts of the demo application.
*/
public static class LessonFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.lesson, container, false);
// Demonstration of a collection-browsing activity.
rootView.findViewById(R.id.button1)
.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(getActivity(), Test.class);
startActivity(intent);
}
});
return rootView;
}
}
#Override
public Fragment getItem(int position) {
switch(position){
case 0 :
case 1 :
// and so on.
}
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}

Categories

Resources