ViewPager that listens to clicks - android

I need a ViewPager that enables to user to swipe images. But on a click on the images, a new activity has to start with another, bigger viewpager that contains the same images.
How can i implement a click listener while still keeping the swipe on the viewpager?

It should be pretty straight-forward, and has nothing to do with your ViewPager. Just add onClickListener to your ImageView:
ImageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// start new activity here
}
}
});
or add the onClick in XML:
<ImageView
...
android:onClick="startTheOtherActivity" />

Related

How to switch between fragments using buttons?

I have an activity with a Viewpager and two buttons inside.
The two buttons are "next" and "back".
I made some Fragment layouts and now want to change the shown Fragment by clicking one of those buttons.
I just know how to use the FragmentStatePagerAdapter to slide between fragments, but how can I do this via onClick?
YourBtn.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
//what you want to do
Yourpager.setCurrentItem( page )
}
});

Fragment's button listener on activity - Android Studio

I have tabbed activity using fragments and in one of them i have a button, but i can only use in the fragment itself but now, I want to use it in my MainActivity?
is there a way to have a Fragment's button listener on activity?
there are two methods
You can write a Button click event callback in the fragment
You can write in activity
((Button)getFragmentManager().findFragmentById(/*frgment id*/).getView().findViewById(R.id.one_nextpager)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});

Is it possible to set different actions for floating action button in differet fragments?

I am working or an app where I have 4 fragments in viewpager and floating action button (fab). When I click on fab there appear 3 subfabs. Each of them start different activities where user can search data from different fragments. Is it possible to set fab so that if I click on it while I'm in first fragment it'll open an activity for searching inside this fragment, onClick inside second - search for second and etc. The issue is that now clicking on sub fab while I'm in some fragment I can search data from another fragment too and it's a bit weird. I understand question is kinda strange, if something is unclear I'll explain further
You can make FAB button public and implement onClick listener in each fragment.
You should override setUserVisibleHint and put your code onResume so getActivity() will not return null. Fab button will have different actions in different fragments.
Here's an example, inside each fragment when you want Fab click listener:
#Override
public void setUserVisibleHint(boolean visible)
{
super.setUserVisibleHint(visible);
if (visible && isResumed())
{
onResume();
}
}
#Override
public void onResume()
{
super.onResume();
if (!getUserVisibleHint())
{
return;
}
MainActivity mainActivity = (MainActivity)getActivity();
mainActivity.fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do what you want
}
});
}
You can check in onclick method of floating action button which fragment is currently opened and calling it.
Call findFragmentById() on FragmentManager and determine which fragment is in your R.id.frameTitle container.

Android ViewPager scroll limited times smoothly

I have a list of fragments implemented together with FragmentStatePagerAdapter. I must replace the current fragment with a new one each time the button don't like will be clicked. The problem with the ViewPager. In my case the ViewPager scroll exactly list.size() -1 times smoothly, All other scrolling disable the smoothly scrolling.
doNotLike.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
fragmentList.set(viewPager.getCurrentItem(), alternativeList.get(viewPager.getCurrentItem()));
viewPagerAdapter.notifyDataSetChanged();
viewPager.setCurrentItem(currentPos + 1, true);
}
});
I have no idea why the smoothly scrolling is so limited with fragmentList-size and how can I turn off this behaviour.

Android: replacing a fragment from a button click within a fragment

I took over this android project from an outsourced developer. I do iOS development so I am still trying to get up to speed with android. The app has a top tab navigation which appears on all views. The home view is a fragment which has icons which when clicked on produce webviews that replace the home view, which the navigation tab still in view. I want to replace one of the webviews with a list view which is a fragment which I already have coded (EHallSchedFragment.java), so that when the icon is clicked on the list view replaces the home view with the navigation tab still in view. See the images below.
HOME VIEW
VIEW I WANT WHEN EXHIBIT HALL SCHEDULE ICON IS CLICKED ON
Here is the code for the onClick event for that button as it exists. It calls a webView:
ivExhall.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
openInternalWebview("http://www.web.org/m/content.aspx?id=7006");
//Need code here to open EhallSchedFragment.java
}
});
SO what I need is the onClick code that will open the fragment in the existing view, leaving the navigation tab in place.
Try this approach: Define an interface inside your fragment, implement it in your activity and then override the method there. Then finally, from your fragment, when a user clicks an item from a list, call the interface method to notify the activity of the event.
From Your Fragment
#Override
public void onClick()
{
//when a user selects an event from your list
switch(position)
{
((OnScheduleSelectedListener) getActivity()).switchFragment(new DetailsFragment());
}
}
public interface OnScheduleSelectedListener
{
void switchFragment(Fragment frag);
}
You can then do the following in your activity:
public class ScheduleAppActivity extends Activity implements OnScheduleSelectedListener
{
.............................
#Override
switchFragment(Fragment frag)
{
//check if fragment is in view here and if,
replaceFragment(fragment)
//else
addFragment(frag);
commit();
}
}
So at this point, the right fragment will be added to view.
I hope this helps you!

Categories

Resources