Help JazzyViewPager and FragmentStatePagerAdapter - Java for Android App Development

The problem is that the transitions wont work but all over the app is working fine. It's just the transition effects that aren't working. I've already tried all of them but no luck, the slides have their default transition. There are no errors in the codes too so I don't know what I've done wrong so please help. This is also my first app so please be kind. I didn't include the instantiateitem on pageradapter because if I do there will be a nullpointerexception. :/ I'm using JazzyViewPager for the transitions and Fragment State Pager Adapter. I've also searched for possible topics that might help me but none of them worked.
Main Activity:
Code:
public class MainActivity extends FragmentActivity {
private JazzyViewPager mJazzy;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupJazziness(TransitionEffect.CubeIn);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Toggle Fade");
String[] effects = this.getResources().getStringArray(R.array.jazzy_effects);
for (String effect : effects)
menu.add(effect);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getTitle().toString().equals("Toggle Fade")) {
mJazzy.setFadeEnabled(!mJazzy.getFadeEnabled());
} else {
TransitionEffect effect = TransitionEffect.valueOf(item.getTitle().toString());
setupJazziness(effect);
}
return true;
}
private void setupJazziness(TransitionEffect effect) {
mJazzy = (JazzyViewPager) findViewById(R.id.jazzy_pager);
mJazzy.setTransitionEffect(effect);
mJazzy.setAdapter(new FragmentAdapter(getSupportFragmentManager()));
mJazzy.setPageMargin(30);
}
FragmentStatePagerAdapter:
Code:
public class FragmentAdapter extends FragmentStatePagerAdapter{
public FragmentAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int position)
{
// TODO Auto-generated method stub
Fragment fragment = new Fragment1();
switch(position){
case 0:
fragment = new Fragment1();
break;
case 1:
fragment = new Fragment2();
break;
case 2:
fragment = new Fragment3();
break;
}
return fragment;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}
@Override
public boolean isViewFromObject(View view, Object object) {
if(object != null){
return ((Fragment)object).getView() == view;
}else{
return false;
}
}
}
Fragment1:
Code:
public class Fragment1 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment1_layout, null);
return v;
}
activity_main xml:
Code:
<com.jfeinstein.jazzyviewpager.JazzyViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:app1="http://schemas.android.com/apk/res/com.eight.yamjay"
android:id="@+id/jazzy_pager"
app1:style="cubeout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
This is the guide that I've been following.

Related

Help With Grid Views

Hi everyone I'am using to grid view when i click an item on grid view 1 it should be added to grid view 2
It is added but the grid view one is scrambled up
Here is the code
Code:
mImageViews = new ArrayList<WeakReference<ImageView>>();
private ArrayList<String> mImageRepositoryTh = new ArrayList<String>(); //Thumbnails size image paths
private ArrayList<String> mImageRepositoryFu = new ArrayList<String>();//Full size image paths
private ArrayList<WeakReference<ImageView>> mWeakReferences = new ArrayList<WeakReference<ImageView>>();
private GridView mListView;
private GridView mSelected;
mListView = (GridView)findViewById(R.id.images_view);
mListView.setAdapter(new ImageAdapter(this));
mSelected = (GridView)findViewById(R.id.images_view_sel);
mChecked = new boolean[mListView.getCount()];
Log.e("ERR", "Array Length "+String.valueOf(mChecked.length));
mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
mChecked[arg2] = !mChecked[arg2];
mWeakReferences.add(new WeakReference<ImageView>((ImageView)arg1));
mSelected.setAdapter(new SelectedImageAdapter());
}
});
Code:
Adapters
public class ImageAdapter extends BaseAdapter
{
public ImageAdapter(@SuppressWarnings("unused")Context c)
{
}
public int getCount() {
return mImageRepositoryTh.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return position;
}
private boolean loadingFailed = false;
public View getView(int position, View convertView, ViewGroup parent) {
final ImageView imageView;
if(convertView != null)
{
try
{
if (mImageViews.get(position).get() != null)
{
return mImageViews.get(position).get();
}
}
catch(IndexOutOfBoundsException E)
{
Log.e("Not Yet Inflate", "Array Index Out \n", E);
}
}
imageView = (ImageView) getLayoutInflater().inflate(R.layout.item_grid_image, parent, false);
mImageLoader.displayImage("file://"+mImageRepositoryTh.get(position), imageView, mOptions,new
ImageLoadingListener() {
public void onLoadingStarted(String imageUri, View view) {
// TODO Auto-generated method stub
}
public void onLoadingFailed(String imageUri, View view,
FailReason failReason) {
// TODO Auto-generated method stub
loadingFailed = true;
}
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
// TODO Auto-generated method stub
}
public void onLoadingCancelled(String imageUri, View view) {
// TODO Auto-generated method stub
}
});
if (loadingFailed) {
mImageLoader.displayImage("file://"+mImageRepositoryFu.get(position),imageView ,mOptions);
loadingFailed = false;
}
mImageViews.add(new WeakReference<ImageView>(imageView));
return imageView;
}
}
private class SelectedImageAdapter extends BaseAdapter
{
public SelectedImageAdapter()
{
}
[user=439709]@override[/user]
public int getCount() {
return mWeakReferences.size();
}
[user=439709]@override[/user]
public Object getItem(int i) {
return null;
}
[user=439709]@override[/user]
public long getItemId(int i) {
return i;
}
[user=439709]@override[/user]
public View getView(int position, View convertView, ViewGroup parent)
{
WeakReference<ImageView> iViewRef = mWeakReferences.get(position);
return iViewRef.get();
}
}
My apologies for ads
Download the apk from attachments

[q][solved]View Pager doesnt adds fragments to backstack

i have made a fragment which contains view pager.it uses list of fragments so that fragments can be added or removed dynamically.
each child fragment of viewpager is a listfragment and loads list using asynctask
now my problem is when i have 3-4 child fragments in view pager,after switching to other pages(tabs),the earlier ones in which list is already loaded are reset automatically when i swipe back to them.so i want that they must be added to backstact so that they can be resumed.
Code:
public class TabFragment extends android.support.v4.app.Fragment {
List fragments = new ArrayList();
public PagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
String home;
ActionBar actionBar;
[user=439709]@override[/user]
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.tabfragment,
container, false);
mSectionsPagerAdapter = new ScreenSlidePagerAdapter(getChildFragmentManager());
mViewPager = (ViewPager) rootView.findViewById(R.id.pager);
actionBar = getActivity().getActionBar();
mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
public void onPageScrolled(int p1, float p2, int p3) {
// TODO: Implement this method
}
public void onPageSelected(int p1) { // TODO: Implement this// method
Main ma = ((Main) fragments.get(p1));
if (ma.current != null) {
ma.bbar(ma.current);
}
}
public void onPageScrollStateChanged(int p1) {
// TODO: Implement this method
}
});
mViewPager.setAdapter(mSectionsPagerAdapter);
return rootView;
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
[user=439709]@override[/user]
public CharSequence getPageTitle(int position) {
//Toast.makeText(getActivity(),""+position,Toast.LENGTH_SHORT).show();
Main ma = ((Main) fragments.get(position));
return new File(ma.current).getName();
}
public int getCount() {
// TODO: Implement this method
return fragments.size();
}
public ScreenSlidePagerAdapter(android.support.v4.app.FragmentManager fm) {
super(fm);
}
[user=439709]@override[/user]
public android.support.v4.app.Fragment getItem(int position) {
android.support.v4.app.Fragment f;
f = fragments.get(position);
return f;
}
}
public void removePage(int position) {
if(position==0){Toast.makeText(getActivity(),"Main Screen Cant be removed",Toast.LENGTH_LONG).show();}else{
fragments.remove(position);
mSectionsPagerAdapter.notifyDataSetChanged();
mViewPager.setCurrentItem(position-1);
}
}
public void add(String text) {
android.support.v4.app.Fragment main = new Main();
Bundle b = new Bundle();
int p = fragments.size();
b.putString("path", text);
b.putInt("pos", p);
main.setArguments(b);
fragments.add(main);
mSectionsPagerAdapter.notifyDataSetChanged();
mViewPager.setCurrentItem(p);
}

[Q] GPS with Fragment

Good day. I start learn Fragment and I want use GPS in my Application. and I use Fragment. in fragment who view coordinate i writed next code
Code:
public class MyFragment2 extends Fragment {
public static final String BUTTON_INDEX = "button_index";
private static final int BUTTON_INDEX_DEFAULT = -1;
static TextView txt;
static TextView txt2;
String[] mCatDescription;
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState){
View viewHier = inflater.inflate(R.layout.fragment2, container, false);
txt = (TextView)viewHier.findViewById(R.id.textView1);
txt2 = (TextView)viewHier.findViewById(R.id.textView2);
mCatDescription =getResources().getStringArray(R.array.cats);
Bundle args = getArguments();
int buttonIndex = args != null ? args.getInt(BUTTON_INDEX, BUTTON_INDEX_DEFAULT):BUTTON_INDEX_DEFAULT;
if(buttonIndex != BUTTON_INDEX_DEFAULT){
setDiscription(buttonIndex);
}
return viewHier;
}
public void setDiscription(int buttonIndex){
GPSWork gpsWork = new GPSWork();
switch(buttonIndex){
case 1:
txt2.setText("one");
break;
case 2:
gpsWork.GetCoordinates();
break;
case 3:
txt2.setText("therd");
break;
}
}
}
Ok. when we click on button 2 we call method GetCoordinates(). this class have next code
Code:
public class GPSWork{
LocationManager locationManager;
public GPSWork(){
locationManager = (LocationManager)MainActivity.mContext.getSystemService(Context.LOCATION_SERVICE);
}
public void GetCoordinates(){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
5000, 5, locationListener);
}
private LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
MyFragment2.txt2.setText("OK");
}
@Override
public void onProviderDisabled(String provider) {
MyFragment2.txt2.setText("OK");
}
@Override
public void onProviderEnabled(String provider) {
MyFragment2.txt2.setText("OK");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
MyFragment2.txt2.setText("OK");
}
};
}
how we see, we should get text OK on TextView on MainActivity. but we don't have text, whats wrong? and if I change code in method GetCoordinates() on
Code:
public void GetCoordinates(){
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
1000, 10, locationListener);
}
all work fine, i see text in TextView (OK )

Passing data between two fragments

Hello guys,
I have a list of fragments created and I want to delete these fragment by long pressing on them. However, before doing so I want dialog fragment to pop up and offer the user the choice to delete or not. I have created the two fragments but I can't get the data across each other. I can't give functionality to the 'OK' button. Here is my code:
Code:
public class CourseListFragment extends Fragment implements
OnItemClickListener, OnItemLongClickListener {
public static final String ARG_ITEM_ID = "course_list";
public static final String YES_NO = "modify";
Activity activity;
ListView courseListView;
ArrayList<Course> courses;
CourseListAdapter courseListAdapter;
CourseDAO courseDAO;
private GetEmpTask task;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = getActivity();
courseDAO = new CourseDAO(activity);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.schedule_fragment_course_list,
container, false);
findViewsById(view);
task = new GetEmpTask(activity);
task.execute((Void) null);
courseListView.setOnItemClickListener(this);
courseListView.setOnItemLongClickListener(this);
return view;
}
private void findViewsById(View view) {
courseListView = (ListView) view.findViewById(R.id.list_course);
}
@Override
public void onItemClick(AdapterView<?> list, View view, int position,
long id) {
Course course = (Course) list.getItemAtPosition(position);
if (course != null) {
Bundle arguments = new Bundle();
arguments.putParcelable("selectedCourse", course);
CustomCourseDialogFragment customEmpDialogFragment = new CustomCourseDialogFragment();
customEmpDialogFragment.setArguments(arguments);
customEmpDialogFragment.show(getFragmentManager(),
CustomCourseDialogFragment.ARG_ITEM_ID);
}
}
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
// Show dialogFragment
FragmentManager fm = getActivity().getSupportFragmentManager();
CheckDialogFragment dialog = new CheckDialogFragment();
dialog.show(fm, YES_NO);
Course employee = (Course) parent.getItemAtPosition(position);
// Use AsyncTask to delete from database
courseDAO.deleteEmployee(employee);
courseListAdapter.remove(employee);
return true;
}
public class GetEmpTask extends AsyncTask<Void, Void, ArrayList<Course>> {
private final WeakReference<Activity> activityWeakRef;
public GetEmpTask(Activity context) {
this.activityWeakRef = new WeakReference<Activity>(context);
}
@Override
protected ArrayList<Course> doInBackground(Void... arg0) {
ArrayList<Course> courseList = courseDAO.getCourses();
return courseList;
}
@Override
protected void onPostExecute(ArrayList<Course> empList) {
if (activityWeakRef.get() != null
&& !activityWeakRef.get().isFinishing()) {
courses = empList;
if (empList != null) {
if (empList.size() != 0) {
courseListAdapter = new CourseListAdapter(activity,
empList);
courseListView.setAdapter(courseListAdapter);
} else {
Toast.makeText(activity, "No Course Records",
Toast.LENGTH_LONG).show();
}
}
}
}
}
/*
* This method is invoked from MainActivity onFinishDialog() method. It is
* called from CustomEmpDialogFragment when an employee record is updated.
* This is used for communicating between fragments.
*/
public void updateView() {
task = new GetEmpTask(activity);
task.execute((Void) null);
}
@Override
public void onResume() {
getActivity().setTitle("Course Schedule");
getActivity().getActionBar().setTitle("Course Schedule");
super.onResume();
}
}
//Dialog Fragment
Code:
public class CheckDialogFragment extends DialogFragment {
CourseListAdapter courseListAdapter;
CourseDAO courseDAO;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// TODO Auto-generated method stub
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
.setTitle("Do you want to delete?")
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
}
})
.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
return builder.create();
}
}
I have problems trying to get the data from onItemLongClick() of CourseListFragment to CourseListFragment. Any help would be greatly appreciated.

[SOLVED] Android getParentFragment() returns null in ViewPager within a Fragment

I have an Activity that contains a Fragment with a ViewPager. Then I call a method of a Fragment within the ViewPager. But if that Fragment then calls getParentFragment(), it returns null.
Why is getParentFragment() null?
The main Fragment that contains a ViewPager:
Code:
public class MyFragment extends Fragment {
private TabLayout mTabLayout;
private ViewPager mViewPager;
private ViewPagerAdapter mAdapter;
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
View view = getView();
// Setup the ViewPager
mViewPager = (ViewPager) view.findViewById(R.id.container);
setupViewPager(mViewPager);
// Setup the TabLayout
mTabLayout = (TabLayout) view.findViewById(R.id.tabs);
mTabLayout.setupWithViewPager(mViewPager);
}
// This method is called from Activity
public void callNestedFragment() {
if (isDetached()) {
return;
}
((Fragment0) mViewPager.getItem(0)).testMethod();
}
}
The nested Fragment (inside mViewPager):
Code:
public class Fragment0 extends Fragment {
...
public void testMethod() {
if (isDetached()) {
return;
}
// Why is getParentFragment() null?
Log.i(TAG, "Parent fragment:" + getParentFragment());
return;
}
}
The ViewPagerAdapter:
Code:
public class ViewPagerAdapter extends FragmentPagerAdapter {
private Context mContext;
public ViewPagerAdapter(FragmentManager manager, Context context) {
super(manager);
mContext = context;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new Fragment0();
case 1:
return new Fragment1();
case 2:
return new Fragment2();
case 3:
return new Fragment3();
case 4:
return new Fragment4();
}
return null;
}
@Override
public int getCount() {
return 5;
}
}
If more code is needed, please let me know. Thanks for your help.
Info
SuperThomasLab said:
I have an Activity that contains a Fragment with a ViewPager. Then I call a method of a Fragment within the ViewPager. But if that Fragment then calls getParentFragment(), it returns null.
Why is getParentFragment() null?
The main Fragment that contains a ViewPager:
Code:
public class MyFragment extends Fragment {
private TabLayout mTabLayout;
private ViewPager mViewPager;
private ViewPagerAdapter mAdapter;
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
View view = getView();
// Setup the ViewPager
mViewPager = (ViewPager) view.findViewById(R.id.container);
setupViewPager(mViewPager);
// Setup the TabLayout
mTabLayout = (TabLayout) view.findViewById(R.id.tabs);
mTabLayout.setupWithViewPager(mViewPager);
}
// This method is called from Activity
public void callNestedFragment() {
if (isDetached()) {
return;
}
((Fragment0) mViewPager.getItem(0)).testMethod();
}
}
The nested Fragment (inside mViewPager):
Code:
public class Fragment0 extends Fragment {
...
public void testMethod() {
if (isDetached()) {
return;
}
// Why is getParentFragment() null?
Log.i(TAG, "Parent fragment:" + getParentFragment());
return;
}
}
The ViewPagerAdapter:
Code:
public class ViewPagerAdapter extends FragmentPagerAdapter {
private Context mContext;
public ViewPagerAdapter(FragmentManager manager, Context context) {
super(manager);
mContext = context;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new Fragment0();
case 1:
return new Fragment1();
case 2:
return new Fragment2();
case 3:
return new Fragment3();
case 4:
return new Fragment4();
}
return null;
}
@Override
public int getCount() {
return 5;
}
}
If more code is needed, please let me know. Thanks for your help.
Click to expand...
Click to collapse
Post Full Logcat Error & Fragment Class
:good:
I found the answer. If isDetached() is true, it doesn't mean getParentFragment() is not null.
So instead of the previous check in testMethod(), this is the new code:
Code:
public void testMethod() {
if (isDetached() && getParentFragment() != null) {
return;
}
...
}
I also now call the same method again in the onStart() method within Fragment0, where getParentFragment() should not be null.

Categories

Resources