I found a library on github that has CarouselPicker com.github.Vatican-Cameos:CarouselPicker:v1.0 i added this in the dependencies and compile also in repositories maven { url 'https://jitpack.io'}
I have successfully make a CarouselPicker this is the JAVA CODE
carouselPicker = (CarouselPicker)findViewById(R.id.carouselPicker);
List<CarouselPicker.PickerItem> itemsImage = new ArrayList<>();
itemsImage.add(new CarouselPicker.DrawableItem(R.drawable.abc));
itemsImage.add(new CarouselPicker.DrawableItem(R.drawable.123));
itemsImage.add(new CarouselPicker.DrawableItem(R.drawable.colors));
itemsImage.add(new CarouselPicker.DrawableItem(R.drawable.shapes));
CarouselPicker.CarouselViewAdapter imageAdapter = new CarouselPicker.CarouselViewAdapter(this, itemsImage,0);
carouselPicker.setAdapter(imageAdapter);
And by having a LinearLayout this is the XML code
<in.goodiebag.carouselpicker.CarouselPicker
android:id="#+id/carouselPicker"
android:layout_marginTop="50dp"
android:layout_marginBottom="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:items_visible="three"
/>
I cant find on google on what if the 1st item in the carousel picker selected like a OnClickListenerto change the intent
I found a library you used on this link
You must use addOnPageChangeListener like this:
carouselPicker.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
//position of the selected item
if(position == 0){
startActivity(new Intent(thisActivity.this, anotherActivity1.class));
}
else if(position == 1){
startActivity(new Intent(thisActivity.this, anotherActivity2.class));
}
// Same conditions for another cases.
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
So, your onClickListener that you want to handle the click event, is onPageSelected method.
Related
i followed this tutorial "https://github.com/DevExchanges/ViewPagerCards"
i got the view like hotdeals section of MMT.But now i also want to change the image while swiping of cardview same as MMT.
you should be add two viewpager.
One for imagePager and second for cardviewPager
cardviewPager .addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
imagePager.setCurrentItem(position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
If you are using Viewpagers you can change the image by using the listener of onPageChanged.
I am using ViewPager which contains Fragment which contains RecyclerView. There I am trying to auto-refresh only current(which is visible) page view every 10 seconds. But the problem is after 10 seconds handler.postDelayed() is updating view of the next page view (not which is visible). I know it's because pager loads 1 extra page and this is causing holder to update with that next view and handler.postDelayed() is updating that next view in holder.
How to solve this problem.
here is my autorefresh method, I am calling it from onBindViewHolder();
private void startAutoRefresh(final ViewHolder holder, final String exchange,
final int currentPosition, final int timer){
new FetchData(holder, exchange, firstTimeLoadPosition).execute(
Configuration.exchangesAPI.get(exchange));
if (MainActivity.pagerPosition == currentPosition) {
counterView.setText(timer + "");
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
if (timer > 0) {
counterView.setText(timer + "");
startAutoRefresh(holder, exchange, currentPosition, timer - 1);
} else {
Log.wtf("Reloading", currentPosition+"");
new FetchData(holder, exchange, currentPosition).execute(
Configuration.exchangesAPI.get(exchange));
startAutoRefresh(holder, exchange, MainActivity.pagerPosition, counter);
}
}
}, 1000);
}
}
use an interface and pass that interface as a parameter of the adapter... in the interface interface GetPositionFromMainActivity extends Parcelable{int getPosition();} and in MainActivity after setting adapter of view paper GetPositionFromMainActivity getpos = new GetPositionFromMainActivity { #override getpos{ return viewpager.getPosition()} and in recycler adapter if(interface.getPos == currentPosition) instead of a static variable.. I mean just use an interface instead of a static variable...
pass the interface to the adapter of viewpager ..and in the getItem of viewPagerAdapter pass the interface as putParcelable as bundle arguments.. get the arguments the fragment and pass the imterface to the recycler adapter...
You can use onPageSelected method of ViewPager.OnPageChangeListenr for this. For example,
ViewPager.OnPageChangeListener mPageChangeListener = new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(final int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(final int position) {
// It returns the position of currently visible page.
// Write your code here, according to your slide position using switch or if else.
}
#Override
public void onPageScrollStateChanged(int state) {
}
};
I want to know the current page position of the ViewPager. I create an adapter and set this adapter to the ViewPager. Now I want to know the current page number or position of the current view, because I want to start some extra events on that particular page.
I used viewPager.setCurrentItem(1); for setting the first item.
Is there a similar method for getting the current page?
in the latest packages you can also use
vp.getCurrentItem()
or
vp is the viewPager ,
pageListener = new PageListener();
vp.setOnPageChangeListener(pageListener);
you have to put a page change listener for your viewPager. There is no method on viewPager to get the current page.
private int currentPage;
private static class PageListener extends SimpleOnPageChangeListener{
public void onPageSelected(int position) {
Log.i(TAG, "page selected " + position);
currentPage = position;
}
}
There is a method object_of_ViewPager.getCurrentItem() which returns the position of currently Viewed page of view pager
If you only want the position, vp.getCurrentItem() will give it to you, no need to apply the onPageChangeListener() for that purpose alone.
You will figure out that setOnPageChangeListener is deprecated, use addOnPageChangeListener, as below:
ViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if(position == 1){ // if you want the second page, for example
//Your code here
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
For this problem Onpagechange listener is the best one But it will also have one small mistake that is it will not detect the starting time time of 0th position Once you will change the page it will starts to detect the Page selected position...For this problem I fount the easiest solution
1.You have to maintain the selected position value then use it....
2. Case 1: At the starting of the position is always Zero....
Case 2: Suppose if you set the current item means you will set that value into maintain position
3.Then do your action with the use of that maintain in your activity...
Public int maintain=0;
myViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int i, float v, int i2) {
//Toast.makeText(MyActivity.this, i+" Is Selected "+data.size(), Toast.LENGTH_SHORT).show();
}
#Override
public void onPageSelected( int i) {
// here you will get the position of selected page
maintain = i;
}
#Override
public void onPageScrollStateChanged(int i) {
}
});
updateButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MyActivity.this, i+" Is Selected "+data.size(), Toast.LENGTH_SHORT).show();
data.set(maintain, "Replaced "+maintain);
myViewPager.getAdapter().notifyDataSetChanged();
}
});
getCurrentItem(), doesn't actually give the right position for the first and the last page I fixed it adding this code:
public void CalcPostion() {
current = viewPager.getCurrentItem();
if ((last == current) && (current != 1) && (current != 0)) {
current = current + 1;
viewPager.setCurrentItem(current);
}
if ((last == 1) && (current == 1)) {
last = 0;
current = 0;
}
display();
last = current;
}
The setOnPageChangeListener() method is deprecated. Use
addOnPageChangeListener(OnPageChangeListener) instead.
You can use OnPageChangeListener and getting the position inside onPageSelected() method, this is an example:
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
Log.d(TAG, "my position is : " + position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
Or just use getCurrentItem() to get the real position:
viewPager.getCurrentItem();
There is no any method getCurrentItem() in viewpager.i already checked the API
I want to make a carousel in Android but must be an interactive one.
I tryed to use this library https://github.com/jacevedo/Android-Apps but i didn't work how i wan't.
What i need is Something like:
The idea is: when a color is clicked the picture change its color with setTint(). If the picture changes the color keep the selected color.
I need that be compatible with android 4.2
Any library or any guide that works similar?
Thanks!
You can try CarouselView library.
Include the following code view in your layout:
<com.synnapps.carouselview.CarouselView
android:id="#+id/carouselView"
android:layout_width="match_parent"
android:layout_height="200dp"
app:fillColor="#FFFFFFFF"
app:pageColor="#00000000"
app:radius="6dp"
app:slideInterval="3000"
app:strokeColor="#FF777777"
app:strokeWidth="1dp"/>
carouselView = (CarouselView) findViewById(R.id.carouselView);
carouselView.setPageCount(sampleImages.length);
carouselView.setImageListener(imageListener);
carouselView.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
// Do your desired action
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
ImageListener imageListener = new ImageListener() {
#Override
public void setImageForPosition(int position, ImageView imageView) {
// Set the desired picture
}
};
I have an application with one Activity (ActivityMain) and some fragments. A NavigationDrawer controls the switch of the fragments. In some fragments the user has the opportunity to switch to another fragment without opening the NavigationDrawer (for example with a button click).
Everything works well, if I use the NavigationDrawer to switch between fragments, but if I use a control (eg. button) within a fragment to switch to another fragment, I cannot set the selectedItem property of the NavigationDraver's (actually a ListView in the ND) selectedItem property.
The NavigationDrawer's selectedItem property is stored with sharedPreferences, and restored in the onDrawerOpened method in the NavigationDrawer fragment.
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition);
}
I've tried to put the selection index within the onClick event of the View to STATE_SELECTED_POSITION value, as follows, but it doesn't worked. I've also cannot get the value from the sharedPreferences in the other Fragment.
public void navigationRowClick(View view) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
switch(view.getId()) {
case R.id.tr_conv:
sp.edit().putInt(STATE_SELECTED_POSITION, 1);
((MainActivity)getActivity()).changeFrame(1);
((MainActivity)getActivity()).restoreActionBar();
break;
case R.id.trCalc:
sp.edit().putInt(STATE_SELECTED_POSITION, 2);
((MainActivity)getActivity()).changeFrame(2);
((MainActivity)getActivity()).restoreActionBar();
break;
case R.id.trCalo:
Integer i = sp.getInt(STATE_SELECTED_POSITION, 100); // get value test
String s = i.toString();
Toast.makeText(getActivity(), s, Toast.LENGTH_SHORT).show();
break;
}
}
My question is, how should I set the selectedItem of the NavigationDrawer from another fragment? Do You have a best practice to this task?
Thanks is advance for the suggestions.
in the onClick event for the button that switches the fragment:
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
((MainActivity) getActivity()).changePosition(1);
sp.edit().putInt(STATE_SELECTED_POSITION, 1).**commit()**;
}
});
//in MainActivity.java
private void changePosition(int position)
{
list.setItemChecked(position, true);
}
this works if you have set the android:choiceMode="singleChoice" attribute to the list.
Another way of doing things is to do it in the adapter of the listview:
.....
{
private int mSelectedItem = 0;
public View getView(int position, View convertView, ViewGroup parent)
{
if(position == mSelectedItem)
{
}
else
{
}
}
public void setSelectedItem(int position)
{
mSelectedItem = position;
}
}
//in MainActivity.java
private void changePosition(int position)
{
adapter.setSelectedItem(position);
adapter.notifyDataSetChanged();
}
Also make sure to commit the changes to the SharedPreferences:
sp.edit().putInt(STATE_SELECTED_POSITION, 1).**commit()**;
maybe you are doing it in some other place, but I don't see it in the snippets you have shown.