V
V
Valera Kuznetsov2014-08-06 23:31:09
Android
Valera Kuznetsov, 2014-08-06 23:31:09

How to force the viewpager to add new items to the center and allow scrolling to the last item?

Viewpager Adapter Code
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);
    }  
   

}


Use to display multiple items at once
@Override
    public float getPageWidth(int position) {
    	// TODO Auto-generated method stub
    	if(colors.size() < 10)
    		return (1.0f / colors.size());
    	return (1.0f / 10);
    }

Elements are added/removed dynamically.
I tried to set the Viewpager in xml to gravity = "center" - it did not help.
I tried to use a third-party implementation of viewpagera , but it either fell behind the support lib, or I connected it incorrectly.
Also, when displaying several elements, it is not possible to swipe to the last one. Shows what is already at the borders, but I want to put the element in the center. ViewPager.setCurrentPage(position) does not center the last
fragment itself - an imageview of 25x25dp that is filled with a specific color

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question