Answer the question
In order to leave comments, you need to log in
How to force the viewpager to add new items to the center and allow scrolling to the last item?
package com.kuznetsov.adapters;
import java.util.ArrayList;
import java.util.Random;
import com.kuznetsov.lightingcontrol.ColorFragment;
import com.kuznetsov.models.ColorModel;
import com.kuznetsov.utils.DataBaseHelper;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.util.SparseArray;
import android.view.ViewGroup;
public class ColorListAdapter extends FragmentStatePagerAdapter {
public static final int NUMBER_OF_COLORS = (int) (256);
public static final String COLOR_LIST_MODE = "colorMode";
public static final String COLOR_NUMBER = "colorNumber";
public ArrayList<ColorModel> colors;
Context context;
Random r = new Random();
SparseArray<ColorFragment> registeredFragments = new SparseArray<ColorFragment>();
public ColorListAdapter(FragmentManager fm) {
super(fm);
colors = new ArrayList<>(DataBaseHelper.getColors());
}
@Override
public Fragment getItem(int number) {
// TODO Auto-generated method stub
ColorFragment colorFragment = new ColorFragment();
Bundle colorBundle = new Bundle();
colorBundle.putInt(COLOR_LIST_MODE, (1) );
Random r = new Random();
colorBundle.putInt(COLOR_NUMBER, colors.get(number).color);
colorBundle.putInt("debug", number);
colorFragment.setArguments(colorBundle);
return colorFragment;
}
@Override
public int getCount() {
return colors.size();
}
@Override
public int getItemPosition (Object object)
{
return POSITION_NONE;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ColorFragment fragment = (ColorFragment) super.instantiateItem(container, position);
registeredFragments.put(position, fragment);
return fragment;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
registeredFragments.remove(position);
super.destroyItem(container, position, object);
}
public ColorFragment getRegisteredFragment(int position) {
return registeredFragments.get(position);
}
public void removeItem(int position) {
try
{
colors.get(position).delete();
colors.remove(position);
}
catch (IndexOutOfBoundsException ex) {
}
finally {
this.notifyDataSetChanged();
}
}
@Override
public float getPageWidth(int position) {
// TODO Auto-generated method stub
if(colors.size() < 10)
return (1.0f / colors.size());
return (1.0f / 10);
}
}
@Override
public float getPageWidth(int position) {
// TODO Auto-generated method stub
if(colors.size() < 10)
return (1.0f / colors.size());
return (1.0f / 10);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question