I have an issue with my viewpager for the android app that I am working on. I have DrawerLayout that calls Fragments. One of these Fragments is the HomeFragment, which contains 2 clickable textviews. I want the HomeFragment to switch to another Fragment(ParkingTutorialFragment) which has a swipeable view. The code I am using switches to the ParkingTutorialFragment but the swipeable view does not seem to be working since nothing happens when I swipe. Below is the code for the Fragments and the adapter:
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ParkingTutorialFragment extends Fragment {
private ViewPager viewPager;
private int page;
private String title;
public static ParkingTutorialFragment newInstance(int page, String title) {
ParkingTutorialFragment parkingFragment = new ParkingTutorialFragment();
Bundle args = new Bundle();
args.putInt("someInt", page);
args.putString("someTitle", title);
parkingFragment.setArguments(args);
return parkingFragment;
}
// Store instance variables based on arguments passed
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// page = getArguments().getInt("someInt", 0);
// title = getArguments().getString("someTitle");
}
// Inflate the view for the fragment based on layout XML
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.parking_tutorial_page, container, false);
return rootView;
}
}
Please Note:
page = getArguments().getInt("someInt", 0);
The above line returns null, I am not sure why if anyone can also tell me why it is not able to retrieve the key for the value "someInt" if anyone can also point that out I would greatly appreciate it
import java.util.LinkedHashMap;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
//Fragment for the home page
public class HomeFragment extends Fragment {
private TypedArray tutorial_icons;
private TextView tutorial_1;
private ViewPager viewpager;
FragmentPagerAdapter adapterViewPager;
public HomeFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.home, container, false);
viewpager = (ViewPager) rootView.findViewById(R.id.tabs_pager);
adapterViewPager = new TabsPagerAdapter(getFragmentManager());
viewpager.setAdapter(adapterViewPager);
tutorial_1 = (TextView) rootView.findViewById(R.id.tutorial_item_1);
tutorial_1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
FragmentManager fragmentManager2 = getFragmentManager();
FragmentTransaction fragmentTransaction2 = fragmentManager2.beginTransaction();
ParkingTutorialFragment parkingFrag = new ParkingTutorialFragment();
fragmentTransaction2.addToBackStack("Back");
fragmentTransaction2.hide(HomeFragment.this);
fragmentTransaction2.add(android.R.id.content, parkingFrag);
fragmentTransaction2.commit();
}
});
return rootView;
}
}
Below is the code for the Adapter:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
public class TabsPagerAdapter extends FragmentPagerAdapter {
private static int NUM_ITEMS = 3;
public TabsPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
// Returns total number of pages
@Override
public int getCount() {
return NUM_ITEMS;
}
// Returns the fragment to display for that page
@Override
public Fragment getItem(int position) {
switch (position) {
case 0: // Fragment # 0 - This will show FirstFragment
return ParkingTutorialFragment.newInstance(0, "Page # 1");
case 1: // Fragment # 0 - This will show FirstFragment different title
return new OffersFragment();
default:
return ParkingTutorialFragment.newInstance(0, "Page # 1");
}
}
// Returns the page title for the top indicator
@Override
public CharSequence getPageTitle(int position) {
return "Page " + position;
}
}
The flow is HomeFragment --> Press Button --> Go to ParkingTutorialFragment (supposed to contain a swipeview) If anyone can help me achieve the desired functionality and clarify the null pointer from the .getInt method, it would be very much appreciated. Thank you.
if i have understood you correctly this will do the trick.update your code inside button click.
FragmentManager fragmentManager2 = getFragmentManager();
FragmentTransaction fragmentTransaction2 = fragmentManager2.beginTransaction();
fragmentTransaction2.addToBackStack("Back");
fragmentTransaction2.hide(HomeFragment.this);
fragmentTransaction2.add(android.R.id.content, ParkingTutorialFragment.newInstance(0, "Page # 1"));
fragmentTransaction2.commit();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.