How to set Imageview as an object? - android

I am working with carousel view. With one layout, I put four images.
And I want to set each image as an each object because I want to set a different method when each image is clicked.
ImageButton product_photo = (ImageButton) l.findViewById(R.id.myoffer_image);
product_photo.setImageResource(R.drawable.myoffers_0);
product_photo.setImageResource(R.drawable.myoffers_1);
product_photo.setImageResource(R.drawable.myoffers_2);
product_photo.setImageResource(R.drawable.myoffers_3);
I tried like the code below..but surely it makes an error..
ImageButton one = product_photo.setImageResource(R.drawable.myoffers_0);
I don't know if my question is clear..but if you reply something to know more clear, I will explain it!
Here is pageadapter class!
public class Myoffers_PagerAdapter extends FragmentPagerAdapter implements
ViewPager.OnPageChangeListener {
private Myoffers_LinearLayout cur = null;
private Myoffers_LinearLayout next = null;
private Myoffers context;
private FragmentManager fm;
private float scale;
public Myoffers_PagerAdapter(Myoffers context, FragmentManager fm) {
super(fm);
this.fm = fm;
this.context = context;
}
#Override
public Fragment getItem(int position)
{
// make the first pager bigger than others
if (position == Myoffers.FIRST_PAGE)
scale = Myoffers.BIG_SCALE;
else
scale = Myoffers.SMALL_SCALE;
position = position % Myoffers.PAGES;
return Myoffers_Fragment.newInstance(context, position, scale);
}
#Override
public int getCount()
{
return Myoffers.PAGES * Myoffers.LOOPS;
}
//#Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels)
{
if (positionOffset >= 0f && positionOffset <= 1f)
{
cur = getRootView(position);
next = getRootView(position +1);
cur.setScaleBoth(Myoffers.BIG_SCALE
- Myoffers.DIFF_SCALE * positionOffset);
next.setScaleBoth(Myoffers.SMALL_SCALE
+ Myoffers.DIFF_SCALE * positionOffset);
}
}
//#Override
public void onPageSelected(int position) {}
//#Override
public void onPageScrollStateChanged(int state) {}
private Myoffers_LinearLayout getRootView(int position)
{
return (Myoffers_LinearLayout)
fm.findFragmentByTag(this.getFragmentTag(position))
.getView().findViewById(R.id.root);
}
private String getFragmentTag(int position)
{
return "android:switcher:" + context.pager.getId() + ":" + position;
}
}
Main class!
public class Myoffers extends FragmentActivity {
public final static int PAGES = 4;
public final static int LOOPS = 1000;
public final static int FIRST_PAGE = PAGES * LOOPS / 2;
public final static float BIG_SCALE = 1.0f;
public final static float SMALL_SCALE = 0.5f;
public final static float DIFF_SCALE = BIG_SCALE - SMALL_SCALE;
public Myoffers_PagerAdapter adapter;
public ViewPager pager;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myoffers);
pager = (ViewPager) findViewById(R.id.myviewpager);
adapter = new Myoffers_PagerAdapter(this, this.getSupportFragmentManager());
pager.setAdapter(adapter);
pager.setOnPageChangeListener(adapter);
pager.setCurrentItem(FIRST_PAGE);
pager.setOffscreenPageLimit(3);
pager.setPageMargin(-100);
}
}
Fragment code
public class Myoffers_Fragment extends Fragment {
protected static final String TAG = "Philips";
FileOutputStream mOutputStream;
private String id;
public static Fragment newInstance(Myoffers context, int pos, float scale)
{
Bundle b = new Bundle();
b.putInt("pos", pos);
b.putFloat("scale", scale);
return Fragment.instantiate(context, Myoffers_Fragment.class.getName(), b);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
LinearLayout l = (LinearLayout) inflater.inflate(R.layout.mf, container, false);
int pos = this.getArguments().getInt("pos");
TextView tv = (TextView) l.findViewById(R.id.text);
tv.setText("Product " + pos);
/*
* Put images with Hard-Coding
*/
{
ImageButton product_photo = (ImageButton) l.findViewById(R.id.myoffer_image);
product_photo.setImageResource(R.drawable.myoffers_0);
product_photo.setImageResource(R.drawable.myoffers_1);
product_photo.setImageResource(R.drawable.myoffers_2);
product_photo.setImageResource(R.drawable.myoffers_3);
}
Myoffers_LinearLayout root = (Myoffers_LinearLayout) l.findViewById(R.id.root);
float scale = this.getArguments().getFloat("scale");
root.setScaleBoth(scale);
return l;
}
Thanks!

Your question is a little vague, but I will respond to the best of my ability--if I am misunderstanding anything, feel free to let me know and I will edit my answer accordingly.
Since 'carousel' is not part of the android SDK, it is a little difficult to be sure what you need to do (it would help if you showed what class/library you are using for this carousel). However, chances are that it will be using this class or a subclass of it in the implementation: AbsListView
Every listview-based class has some method that is called when an item is clicked (here, it is performItemClick). When it is called, it will in turn call the onItemClick method of the associated AdapterView. The adapter is something that you (or the library you are using) created, filled with your data to be displayed, and attached to the ListView.
By registering an AdapterView.OnClickListener, you can specify what behavior a click on a list item at any given index will cause.
Without more details about your carousel, I cannot provide any more code-specific help. However, if you post more info I would be happy to update my answer!

ImageButton is a class, and you created an object of that type called 'product_photo'. Then you create another called 'one'. The problem is that you tried to assign a method to one rather than create a new ImageButton. Instead you should have
ImageButton one = new ImageButton();
one.setImageResource(R.drawable.myoffers_0);
Whether or not 'one' needs to be something in your layout that you get with findViewById or not (likely you do) is up to your application.

Related

Multiply recycler view and adapters

Actually I am going to ask more than one question here. Don't ban me, please, just read a full story. Let's begin. So I need to create an activity or fragment (it doesn't matter) with to parts (views) inside (top and bottom). Inside the bottom part dynamically loads buttons (sometimes 2, sometimes 30), there is a click listener on them. When a user clicks on a button, the button appears on the top part (view) and disappears on the bottom view. The buttons on the top view also have click listener and if a user clicks on a button it appears on the bottom view and disappears on a top. So this is a task. I thought how to implement it. The simplest solution that I created is: two views are recycler views with two adapters. Mm, probably it is not the best solution, I am pretty sure of it. I could implement two adapters, but I can't implement the click listener for my second adapter. It doesn't work!? I don't like this way for two reasons: 1. both adapters are the same; 2. I can't use click adapter for second adapter. Below you can find my code.
My adapter - standard adapter:
public class KeyboardAdapter extends RecyclerView.Adapter<KeyboardAdapter.KeyboardAdapterViewHolder> {
private List<String> values;
/*
* An on-click handler that we've defined to make it easy for an Activity to interface with
* our RecyclerView
*/
private final KeyboardAdapterOnClickHandler mClickHandler;
/**
* The interface that receives onClick messages.
*/
public interface KeyboardAdapterOnClickHandler {
void onClick(int position, String nameClicked);
}
/**
* Creates a SourceAdapter.
*
* #param clickHandler The on-click handler for this adapter. This single handler is called
* when an item is clicked.
*/
public KeyboardAdapter(List<String> myDataset, KeyboardAdapterOnClickHandler clickHandler) {
values = myDataset;
mClickHandler = clickHandler;
}
/**
* Cache of the children views for a forecast list item.
*/
public class KeyboardAdapterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
// each data item is just a string in this case
private Button btnValue;
private String mName;
public View layout;
private int parentId;
private KeyboardAdapterViewHolder(View view) {
super(view);
//layout = view;
btnValue = view.findViewById(R.id.btn);
//parentId = ((View) btnValue.getParent()).getId();
// Call setOnClickListener on the view passed into the constructor (use 'this' as the OnClickListener)
view.setOnClickListener(this);
}
public void setData(String name) {
mName = name;
btnValue.setText(mName);
}
#Override
public void onClick(View view) {
int adapterPosition = getAdapterPosition();
mClickHandler.onClick(adapterPosition, mName);
}
}
#Override
public long getItemId(int position) {
return super.getItemId(position);
}
#Override
#NonNull
public KeyboardAdapterViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout, parent, false);
return new KeyboardAdapterViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull KeyboardAdapterViewHolder viewHolder, final int position) {
viewHolder.setData(values.get(position));
}
#Override
public int getItemCount() {
return values.size();
}
#Override
public int getItemViewType(int position) {
return 0;
}
public void remove(int position) {
values.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, values.size());
}
}
MainActivity:
protected void onCreate(Bundle savedInstanceState) {
...
String s = "test it";
mAdapter = new KeyboardAdapter(virtualKeyboardInit(s), MainActivity.this);
recyclerView1.setAdapter(mAdapter);
// empty list just to init rv
answerList = new ArrayList<>();
mAdapter1 = new KeyboardAdapter1(answerList, MainActivity.this); // doesn't work, error message "KeyboardAdapter1.KeyboardAdapterOnClickHandler cannot be applied to MainActivity"
recyclerView2.setAdapter(mAdapter1);
}
private List<String> virtualKeyboardInit(String s) {
boolean checkBool = true;
// convert string to array and then to list
String [] strArray = s.split("(?!^)");
stringList = new ArrayList<>(Arrays.asList(strArray));
// shuffle letters in the list
long seed = System.nanoTime();
Collections.shuffle(stringList, new Random(seed));
// API 24
// /String[] strArray = Stream.of(cArray).toArray(String[]::new);
return stringList;
}
#Override
public void onClick(int position, String nameClicked) {
mAdapter.remove(position);
}
These are just a fragment of code. So, what can I do in this case? Thank you for attention and help.

How can I find out which image is the current one in a ViewPager?

I'm fairly new to Android programming, and I'm trying to design an app where when I swipe through several images on a ViewPager that takes up some of the screen, other elements of that same activity/screen will change with it without necessarily 'scrolling' horizontally like the ViewPager does. My problem is that I can't find a way to identify which image in the ViewPager is the 'current' one outside of the ViewPager.
I tried to create a getter for grabbing the position from instantiateItem method, but no luck - because I assume it simply creates everything once, not updating it again, so when I swipe nothing will happen. Also, I realize that my dieValue variable doesn't do anything - but it's meant to serve as an example of what I want to accomplish. The dieValue variable would change based on which image was the current one.
CustomSwipeAdapter.java
public class CustomSwipeAdapter extends PagerAdapter {
private Context ctx;
public int[] image_resources = {R.drawable.d20, R.drawable.d8, R.drawable.d6};
public CustomSwipeAdapter(Context ctx) {
this.ctx = ctx;
}
#Override
public int getCount() {
return image_resources.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item_view = layoutInflater.inflate(R.layout.swipe_layout, container, false);
ImageView imageView = (ImageView) item_view.findViewById(R.id.dieImageView);
imageView.setImageResource(image_resources[position]);
TextView textView = (TextView) item_view.findViewById(R.id.dieValueTop);
if (position == 0) { textView.setText(R.string.d20Label); }
if (position == 1) { textView.setText(R.string.d8Label); }
if (position == 2) { textView.setText(R.string.d6Label); }
container.addView(item_view);
return item_view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
}
DiceRollerActivity.java
public class DiceRollerActivity extends Activity implements View.OnClickListener {
ViewPager viewPager;
CustomSwipeAdapter adapter;
private int dieValue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dice_roller);
viewPager = (ViewPager) findViewById(R.id.dieViewPager);
adapter = new CustomSwipeAdapter(this);
viewPager.setAdapter(adapter);
Button RollDieButton = (Button) findViewById(R.id.rollDieButton);
RollDieButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.rollDieButton:
Random rand = new Random();
int random = rand.nextInt(dieValue) + 1;
setDieResults(random);
}
}
// Prints the results of the die roll to the results log
private void setDieResults(int random) {
TextView dieResults = (TextView) findViewById(R.id.dieResultsLabel);
if (random == dieValue) {
dieResults.setText(getString(R.string.YouRolledLabel) + random + getString(R.string.criticalHit));
} else if (random == dieValue) {
dieResults.setText(getString(R.string.YouRolledLabel) + random + getString(R.string.criticalMiss));
}else{
dieResults.setText(getString(R.string.YouRolledLabel) + random);
}
}
}
Create a method inside your CustomSwipeAdapter
public int getCurrentImageResource(int currentPosition){
return image_resources[currentPosition];
}
To access to the current image call this method passing current position with viewPager.getCurrentItem() like this
adapter.getCurrentImageResource(viewPager.getCurrentItem());
Hope this helps!
You could use
viewPager.getCurrentItem();
on your DiceRollerActivity.class to get the current position.
Then, to get the image a better approach would be to have the image array on your DiceRollerActivity.class and pass it onto the CustomSwipeAdapter constructor.

PinnedHeader Google plus GridView?

Background
Google plus (google+) app has a nice viewing of images on the "highlights" category.
For each section on this screen, they made a header that contains a clickable text and a button to select all photos of this section. for each section they also show the photos in a grid-like manner.
Here's how it looks like :
Here's another more updated image: link .
For some reason, the images here show a sharing button instead of selections, but that's not the issue I wish to talk about.
The problem
I need to have a similar viewing of photos (including button/s on the headers) , but also make the top header always be visible (AKA "pinned header" , like on this project) .
In fact, I don't even care if it will be pinned (though it could be a nice feature).
What I've tried
I've found only 2 libraries that have pinned header gridViews:
StickyGridHeaders - it seemed fine. the API and code design is very nice . However, i've played with it on some devices and found out it crashes with a very weird exception. i've reported about it here, but as I look at the other issues, I think this project won't get fixed anytime soon.
AStickyHeader - this one doesn't have any crashes and bugs that I can find, but it lacks good code design, and it's not so customizable. the header cannot be clicked and it cannot have a button like on Google-plus. i've tried adding it but for some reason the button isn't shown. I've reported about my remarks on it here.
The question
Is there anyone who have tried to handle such a thing?
Any library available or a modification to the libraries I've tried that allow to have what I've written?
since i can't find any other solution, i've decided to make my own solution (code based on another code i've made, here)
it's used on a ListView instead, but it works quite well. you just set the adapter on the listView and you are good to go. you can set exactly how the headers look like and how each cell look like.
it works by having 2 types of rows: header-rows and cells-rows .
it's not the best solution, since it creates extra views instead of having the ListView/GridView (or whatever you use) put the cells correctly, but it works fine and it doesn't crash
it also doesn't have items clicking (since it's for listView), but it shouldn't be hard to add for whoever uses this code.
sadly it also doesn't have the header as a pinned header, but maybe it's possible to be used with this library (PinnedHeaderListView) .
here's the code :
public abstract class HeaderGridedListViewAdapter<SectionData, ItemType> extends BaseAdapter {
private static final int TYPE_HEADER_ROW = 0;
private static final int TYPE_CELLS_ROW = 1;
private final int mNumColumns;
private final List<Row<SectionData, ItemType>> mRows = new ArrayList<Row<SectionData, ItemType>>();
private final int mCellsRowHeight;
private final Context mContext;
public HeaderGridedListViewAdapter(final Context context, final List<Section<SectionData, ItemType>> sections,
final int numColumns, final int cellsRowHeight) {
this.mContext = context;
this.mNumColumns = numColumns;
this.mCellsRowHeight = cellsRowHeight;
for (final Section<SectionData, ItemType> section : sections) {
// add header
Row<SectionData, ItemType> row = new Row<SectionData, ItemType>();
row.section = section;
row.type = TYPE_HEADER_ROW;
mRows.add(row);
int startIndex = 0;
// add section rows
for (int cellsLeft = section.getItemsCount(); cellsLeft > 0;) {
row = new Row<SectionData, ItemType>();
row.section = section;
row.startIndex = startIndex;
row.type = TYPE_CELLS_ROW;
cellsLeft -= Math.min(mNumColumns, cellsLeft);
startIndex += mNumColumns;
mRows.add(row);
}
}
}
#Override
public int getViewTypeCount() {
return 2;
}
#Override
public int getItemViewType(final int position) {
return getItem(position).type;
}
#Override
public int getCount() {
return mRows.size();
}
#Override
public Row<SectionData, ItemType> getItem(final int position) {
return mRows.get(position);
}
#Override
public long getItemId(final int position) {
return position;
}
#Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
final Row<SectionData, ItemType> item = getItem(position);
switch (item.type) {
case TYPE_CELLS_ROW:
LinearLayout rowLayout = (LinearLayout) convertView;
if (rowLayout == null) {
rowLayout = new LinearLayout(mContext);
rowLayout.setOrientation(LinearLayout.HORIZONTAL);
rowLayout.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, mCellsRowHeight));
rowLayout.setWeightSum(mNumColumns);
}
final int childCount = rowLayout.getChildCount();
// reuse previous views of the row if possible
for (int i = 0; i < mNumColumns; ++i) {
// reuse old views if possible
final View cellConvertView = i < childCount ? rowLayout.getChildAt(i) : null;
// fill cell with data
final View cellView = getCellView(item.section, item.startIndex + i, cellConvertView, rowLayout);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) cellView.getLayoutParams();
if (layoutParams == null) {
layoutParams = new LinearLayout.LayoutParams(0, mCellsRowHeight, 1);
cellView.setLayoutParams(layoutParams);
} else {
final boolean needSetting = layoutParams.weight != 1 || layoutParams.width != 0
|| layoutParams.height != mCellsRowHeight;
if (needSetting) {
layoutParams.width = 0;
layoutParams.height = mCellsRowHeight;
layoutParams.weight = 1;
cellView.setLayoutParams(layoutParams);
}
}
if (cellConvertView == null)
rowLayout.addView(cellView);
}
return rowLayout;
case TYPE_HEADER_ROW:
return getHeaderView(item.section, convertView, parent);
}
throw new UnsupportedOperationException("cannot create this type of row view");
}
#Override
public boolean areAllItemsEnabled() {
return false;
}
#Override
public boolean isEnabled(final int position) {
return false;
}
/** should handle getting a single header view */
public abstract View getHeaderView(Section<SectionData, ItemType> section, View convertView, ViewGroup parent);
/**
* should handle getting a single cell view. <br/>
* NOTE:read the parameters description carefully !
*
* #param section
* the section that this cell belongs to
* #param positionWithinSection
* the position within the section that we need to fill the data with. note that if it's larger than what
* the section can give you, it means we need an empty cell (same the the others, but shouldn't show
* anything, can be invisible if you wish)
* #param convertView
* a recycled row cell. you must use it when it's not null, and fill it with data
* #param parent
* the parent of the view. you should use it for inflating the view (but don't attach the view to the
* parent)
*/
public abstract View getCellView(Section<SectionData, ItemType> section, int positionWithinSection,
View convertView, ViewGroup parent);
// ////////////////////////////////////
// Section//
// /////////
public static class Section<SectionData, ItemType> {
private final List<ItemType> mItems;
private final SectionData mSectionData;
public Section(final SectionData sectionData, final List<ItemType> items) {
this.mSectionData = sectionData;
this.mItems = items;
}
public SectionData getSectionData() {
return mSectionData;
}
public int getItemsCount() {
return mItems.size();
}
public ItemType getItem(final int posInSection) {
return mItems.get(posInSection);
}
#Override
public String toString() {
return mSectionData;
}
}
// ////////////////////////////////////
// Row//
// /////
private static class Row<SectionData, ItemType> {
int type, startIndex;
Section<SectionData, ItemType> section;
}
}

ListView not visible in ViewPager in custom ViewGroup

I have a custom ViewGroup that has a ViewPager as a child. This is because I want to add a static header to the ViewPager and mess with onDraw() to make the header scroll vertically with the ViewPager content. My ViewPager has a ListView as a child. I'm able to create the ListView and the adapter and even set the adapter to the ListView. When I read wallList.getAdapter().getItemAt(0).toString() it returns the data I would expect it to return. But for some reason, I can't see the ListView at all.
The ViewPager still works as intended and the other four pages have arbitrary text for the time being. But the center page which is supposed to contain the ListView shows a blank screen. I can't figure out what's going on, but it sounds similar to a problem I had before here and was able to hack my way to get something acceptable, but wasn't able to answer my original question.
It seems like you can only call setAdapter() in certain places, not only does it have to be on the UI thread (I think) but it seems to have trouble doing it within certain methods, custom or overrided. Here's my custom ViewGroup code.
public class CustomProfilePager extends ViewGroup{
Bitmap coverPhoto, profilePhoto;
Paint coverStyle, profileStyle;
String name;
int coverHeight;
ViewPager pager;
ProfilePagerAdapter pagerAdapter;
ListView wallList;
Context context;
public CustomProfilePager(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
Log.i("CustomPager", "calling onLayout()");
pager.layout(0, coverHeight+240, getWidth(), getHeight());
// for(int i=0; i<getChildCount(); i++){
// getChildAt(i).layout(l, t, r, b);
// }
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
#Override
protected void onDraw(Canvas canvas) {
Log.i("CustomPager", "calling onDraw()");
super.onDraw(canvas);
if(coverPhoto!=null){
canvas.drawBitmap(coverPhoto, 0, 0, coverStyle);
}
}
public void init(String name){
Log.i("CustomPager", "calling init()");
this.name = name;
coverStyle = new Paint();
coverHeight = (int) (getWidth()/2.7);
profileStyle = new Paint();
wallList = new ListView(context);
pagerAdapter = new ProfilePagerAdapter();
pager = new ViewPager(context);
pager.setAdapter(pagerAdapter);
addView(pager);
pager.setCurrentItem(2);
pager.setOffscreenPageLimit(4);
}
public void setCoverPhoto(Bitmap bitmap){
Log.i("CustomPager", "calling setCoverPhoto()");
int initialWidth = bitmap.getWidth();
int initialHeight = bitmap.getHeight();
int finalHeight = (int) (initialWidth/2.7);
int initialYoffset = (int) (initialHeight-finalHeight)/2;
this.coverPhoto = Bitmap.createBitmap(bitmap, 0, initialYoffset, bitmap.getWidth(), finalHeight);
invalidate();
}
public void setProfilePhoto(Bitmap bitmap){
this.profilePhoto = bitmap;
}
public ViewPager getViewPager(){
return pager;
}
public void setWallAdapter(Profile.WallAdapter adapter){
Log.i("CustomPager", "calling setWallAdapter()");
wallList.setAdapter(adapter);
}
public class ProfilePagerAdapter extends PagerAdapter {
#Override
public void destroyItem(ViewGroup collection, int position, Object view) {
((ViewPager) collection).removeView((View) view);
}
#Override
public Object instantiateItem(ViewGroup collection, int position) {
ViewPager parent = (ViewPager) collection;
switch (position) {
case 2: // Wall
wallList.setDividerHeight(0);
parent.addView(wallList);
return wallList;
default:
TextView testText = new TextView(context);
testText.setText(String.valueOf(position) + ": " + name);
testText.setTextSize(46);
testText.setGravity(Gravity.CENTER);
parent.addView(testText);
return testText;
}
}
#Override
public int getCount() {
return 5;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
}
I too have experience that, Some of the View in my ViewPager were getting lost. When debug came to know that it contain only 2 (some time even 1) views in ViewPager. But actually there has to be 3 views since i implemented circular viewpager.
After many trial and error method, i commented the removeView line from destoryItem and i was surprise and it was working perfectly.
I am not sure will this solved your problem since the documentation for destroyItem says
Remove a page for the given position. The adapter is responsible for
removing the view from its container, although it only must ensure
this is done by the time it returns from finishUpdate(ViewGroup).
For me, there should always be 3 views in my ViewPager. since it was a circular ViewPager.
public class ProfilePagerAdapter extends PagerAdapter {
#Override
public void destroyItem(ViewGroup collection, int position, Object view) {
}
#Override
public Object instantiateItem(ViewGroup collection, int position) {
ViewPager parent = (ViewPager) collection;
switch (position) {
case 2: // Wall
wallList.setDividerHeight(0);
parent.addView(wallList);
return wallList;
default:
TextView testText = new TextView(context);
testText.setText(String.valueOf(position) + ": " + name);
testText.setTextSize(46);
testText.setGravity(Gravity.CENTER);
parent.addView(testText);
return testText;
}
}
#Override
public int getCount() {
return 5;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
There is a chance that you may only really need to call
notifyDataSetChanged()
on the PagerAdapter and the ListView will then be displayed properly.
Have you called setWillNotDrawEnabled(false) on your customViewGroup?
By default ViewGroups dont call the onDraw method, also try implmenting the onDispatchDraw instead of the onDraw method
Android Custom Layout - onDraw() never gets called
The problem wasn't really obvious and I'm not sure exactly what caused it in the first place. All I had to do was sublass FrameLayout instead of ViewGroup and everything works.

Changing ViewPager to enable infinite page scrolling

Jon Willis has posted on how to enable an infinite scrolling with his code.
In there he said that he made some changes in the ViewPager class in the android support library. Which changes have been made and how is it possible to "recompile" the library with the ViewPager change?
I solved this problem very simply using a little hack in the adapter. Here is my code:
public class MyPagerAdapter extends FragmentStatePagerAdapter
{
public static int LOOPS_COUNT = 1000;
private ArrayList<Product> mProducts;
public MyPagerAdapter(FragmentManager manager, ArrayList<Product> products)
{
super(manager);
mProducts = products;
}
#Override
public Fragment getItem(int position)
{
if (mProducts != null && mProducts.size() > 0)
{
position = position % mProducts.size(); // use modulo for infinite cycling
return MyFragment.newInstance(mProducts.get(position));
}
else
{
return MyFragment.newInstance(null);
}
}
#Override
public int getCount()
{
if (mProducts != null && mProducts.size() > 0)
{
return mProducts.size()*LOOPS_COUNT; // simulate infinite by big number of products
}
else
{
return 1;
}
}
}
And then, in the ViewPager, we set current page to the middle:
mAdapter = new MyPagerAdapter(getSupportFragmentManager(), mProducts);
mViewPager.setAdapter(mAdapter);
mViewPager.setCurrentItem(mViewPager.getChildCount() * MyPagerAdapter.LOOPS_COUNT / 2, false); // set current item in the adapter to middle
Thank you for your answer Shereef.
I solved it a little bit differently.
I changed the code of the ViewPager class of the android support library. The method setCurrentItem(int)
changes the page with animation. This method calls an internal method that requires the index and a flag enabling smooth scrolling. This flag is boolean smoothScroll.
Extending this method with a second parameter boolean smoothScroll solved it for me.
Calling this method setCurrentItem(int index, boolean smoothScroll) allowed me to make it scroll indefinitely.
Here is a full example:
Please consider that only the center page is shown.
Moreover did I store the pages seperately, allowing me to handle them with more ease.
private class Page {
View page;
List<..> data;
}
// page for predecessor, current, and successor
Page[] pages = new Page[3];
mDayPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
#Override
public void onPageScrollStateChanged(int state) {
if (state == ViewPager.SCROLL_STATE_IDLE) {
if (mFocusedPage == 0) {
// move some stuff from the
// center to the right here
moveStuff(pages[1], pages[2]);
// move stuff from the left to the center
moveStuff(pages[0], pages[1]);
// retrieve new stuff and insert it to the left page
insertStuff(pages[0]);
}
else if (mFocusedPage == 2) {
// move stuff from the center to the left page
moveStuff(pages[1], pages[0]);
// move stuff from the right to the center page
moveStuff(pages[2], pages[1]);
// retrieve stuff and insert it to the right page
insertStuff(pages[2]);
}
// go back to the center allowing to scroll indefinitely
mDayPager.setCurrentItem(1, false);
}
}
});
However, without Jon Willis Code I wouldn't have solved it myself.
EDIT: here is a blogpost about this:
Infinite view pager by overriding 4 adapter methods in your existing adapter class
#Override
public int getCount() {
return Integer.MAX_VALUE;
}
#Override
public CharSequence getPageTitle(int position) {
String title = mTitleList.get(position % mActualTitleListSize);
return title;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
int virtualPosition = position % mActualTitleListSize;
return super.instantiateItem(container, virtualPosition);
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
int virtualPosition = position % mActualTitleListSize;
super.destroyItem(container, virtualPosition, object);
}
Actually, I've been looking at the various ways to do this "infinite" pagination, and even though the human notion of time is that it is infinite (even though we have a notion of the beginning and end of time), computers deal in the discrete. There is a minimum and maximum time (that can be adjusted as time goes on, remember the basis of the Y2K scare?).
Anyways, the point of this discussion is that it is/should be sufficient to support a relatively infinite date range through an actually finite date range. A great example of this is the Android framework's CalendarView implementation, and the WeeksAdapter within it. The default minimum date is in 1900 and the default maximum date is in 2100, this should cover 99% of the calendar use of anyone within a 10 year radius around today easily.
What they do in their implementation (focused on weeks) is compute the number of weeks between the minimum and maximum date. This becomes the number of pages in the pager. Remember that the pager doesn't need to maintain all of these pages simultaneously (setOffscreenPageLimit(int)), it just needs to be able to create the page based on the page number (or index/position). In this case the index is the number of weeks that the week is from the minimum date. With this approach you just have to maintain the minimum date and the number of pages (distance to the maximum date), then for any page you can easily compute the week associated with that page. No dancing around the fact that ViewPager doesn't support looping (a.k.a infinite pagination), and trying to force it to behave like it can scroll infinitely.
new FragmentStatePagerAdapter(getFragmentManager()) {
#Override
public Fragment getItem(int index) {
final Bundle arguments = new Bundle(getArguments());
final Calendar temp_calendar = Calendar.getInstance();
temp_calendar.setTimeInMillis(_minimum_date.getTimeInMillis());
temp_calendar.setFirstDayOfWeek(_calendar.getStartOfWeek());
temp_calendar.add(Calendar.WEEK_OF_YEAR, index);
// Moves to the first day of this week
temp_calendar.add(Calendar.DAY_OF_YEAR,
-UiUtils.modulus(temp_calendar.get(Calendar.DAY_OF_WEEK) - temp_calendar.getFirstDayOfWeek(),
7));
arguments.putLong(KEY_DATE, temp_calendar.getTimeInMillis());
return Fragment.instantiate(getActivity(), WeekDaysFragment.class.getName(), arguments);
}
#Override
public int getCount() {
return _total_number_of_weeks;
}
};
Then WeekDaysFragment can easily display the week starting at the date passed in its arguments.
Alternatively, it seems that some version of the Calendar app on Android uses a ViewSwitcher (which means there's only 2 pages, the one you see and the hidden page). It then changes the transition animation based on which way the user swiped and renders the next/previous page accordingly. In this way you get infinite pagination because it just switching between two pages infinitely. This requires using a View for the page though, which is way I went with the first approach.
In general, if you want "infinite pagination", it's probably because your pages are based off of dates or times somehow. If this is the case consider using a finite subset of time that is relatively infinite instead. This is how CalendarView is implemented for example. Or you can use the ViewSwitcher approach. The advantage of these two approaches is that neither does anything particularly unusual with the ViewSwitcher or ViewPager, and doesn't require any tricks or reimplementation to coerce them to behave infinitely (ViewSwitcher is already designed to switch between views infinitely, but ViewPager is designed to work on a finite, but not necessarily constant, set of pages).
All you need to do is look at the example here
You will find that in line 295 the page is always set to 1 so that it is scrollable
and that the count of pages is 3 in getCount() method.
Those are the 2 main things you need to change, the rest is your logic and you can handle them differently.
Just make a personal counter that counts the real page you are on because position will no longer be usable after always setting current page to 1 on line 295.
p.s. this code is not mine it was referenced in the question you linked in your question
infinite slider adapter skeleton based on previous samples
some critical issues:
remember original (relative) position in page view (tag used in sample), so we will look this position to define relative position of view. otherwise child order in pager is mixed
have to fill first time absolute view inside adapter. (the rest of times this fill will be invalid) found no way to force it fill from pager handler. the rest times absolute view will be overriden from pager handler with correct values.
when pages are slided quickly, side page (actually left) is not filled from pager handler. no workaround for the moment, just use empty view, it will be filled with actual values when drag is stopped. upd: quick workaround: disable adapter's destroyItem.
you may look at the logcat to understand whats happening in this sample
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/calendar_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:padding="5dp"
android:layout_gravity="center_horizontal"
android:text="Text Text Text"
/>
</RelativeLayout>
And then:
public class ActivityCalendar extends Activity
{
public class CalendarAdapter extends PagerAdapter
{
#Override
public int getCount()
{
return 3;
}
#Override
public boolean isViewFromObject(View view, Object object)
{
return view == ((RelativeLayout) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position)
{
LayoutInflater inflater = (LayoutInflater)ActivityCalendar.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewLayout = inflater.inflate(R.layout.layout_calendar, container, false);
viewLayout.setTag(new Integer(position));
//TextView tv = (TextView) viewLayout.findViewById(R.id.calendar_text);
//tv.setText(String.format("Text Text Text relative: %d", position));
if (!ActivityCalendar.this.scrolledOnce)
{
// fill here only first time, the rest will be overriden in pager scroll handler
switch (position)
{
case 0:
ActivityCalendar.this.setPageContent(viewLayout, globalPosition - 1);
break;
case 1:
ActivityCalendar.this.setPageContent(viewLayout, globalPosition);
break;
case 2:
ActivityCalendar.this.setPageContent(viewLayout, globalPosition + 1);
break;
}
}
((ViewPager) container).addView(viewLayout);
//Log.i("instantiateItem", String.format("position = %d", position));
return viewLayout;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object)
{
((ViewPager) container).removeView((RelativeLayout) object);
//Log.i("destroyItem", String.format("position = %d", position));
}
}
public void setPageContent(View viewLayout, int globalPosition)
{
if (viewLayout == null)
return;
TextView tv = (TextView) viewLayout.findViewById(R.id.calendar_text);
tv.setText(String.format("Text Text Text global %d", globalPosition));
}
private boolean scrolledOnce = false;
private int focusedPage = 0;
private int globalPosition = 0;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calendar);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setOnPageChangeListener(new OnPageChangeListener()
{
#Override
public void onPageSelected(int position)
{
focusedPage = position;
// actual page change only when position == 1
if (position == 1)
setTitle(String.format("relative: %d, global: %d", position, globalPosition));
Log.i("onPageSelected", String.format("focusedPage/position = %d, globalPosition = %d", position, globalPosition));
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
{
//Log.i("onPageScrolled", String.format("position = %d, positionOffset = %f", position, positionOffset));
}
#Override
public void onPageScrollStateChanged(int state)
{
Log.i("onPageScrollStateChanged", String.format("state = %d, focusedPage = %d", state, focusedPage));
if (state == ViewPager.SCROLL_STATE_IDLE)
{
if (focusedPage == 0)
globalPosition--;
else if (focusedPage == 2)
globalPosition++;
scrolledOnce = true;
for (int i = 0; i < viewPager.getChildCount(); i++)
{
final View v = viewPager.getChildAt(i);
if (v == null)
continue;
// reveal correct child position
Integer tag = (Integer)v.getTag();
if (tag == null)
continue;
switch (tag.intValue())
{
case 0:
setPageContent(v, globalPosition - 1);
break;
case 1:
setPageContent(v, globalPosition);
break;
case 2:
setPageContent(v, globalPosition + 1);
break;
}
}
Log.i("onPageScrollStateChanged", String.format("globalPosition = %d", globalPosition));
viewPager.setCurrentItem(1, false);
}
}
});
CalendarAdapter calendarAdapter = this.new CalendarAdapter();
viewPager.setAdapter(calendarAdapter);
// center item
viewPager.setCurrentItem(1, false);
}
}
Its hacked by CustomPagerAdapter:
MainActivity.java:
import android.content.Context;
import android.os.Handler;
import android.os.Parcelable;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<String> numberList = new ArrayList<String>();
private CustomPagerAdapter mCustomPagerAdapter;
private ViewPager mViewPager;
private Handler handler;
private Runnable runnable;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
numberList.clear();
for (int i = 0; i < 10; i++) {
numberList.add(""+i);
}
mViewPager = (ViewPager)findViewById(R.id.pager);
mCustomPagerAdapter = new CustomPagerAdapter(MainActivity.this);
EndlessPagerAdapter mAdapater = new EndlessPagerAdapter(mCustomPagerAdapter);
mViewPager.setAdapter(mAdapater);
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
int modulo = position%numberList.size();
Log.i("Current ViewPager View's Position", ""+modulo);
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
handler = new Handler();
runnable = new Runnable() {
#Override
public void run() {
mViewPager.setCurrentItem(mViewPager.getCurrentItem()+1);
handler.postDelayed(runnable, 1000);
}
};
handler.post(runnable);
}
#Override
protected void onDestroy() {
if(handler!=null){
handler.removeCallbacks(runnable);
}
super.onDestroy();
}
private class CustomPagerAdapter extends PagerAdapter {
Context mContext;
LayoutInflater mLayoutInflater;
public CustomPagerAdapter(Context context) {
mContext = context;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return numberList.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
View itemView = mLayoutInflater.inflate(R.layout.row_item_viewpager, container, false);
TextView textView = (TextView) itemView.findViewById(R.id.txtItem);
textView.setText(numberList.get(position));
container.addView(itemView);
return itemView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
}
private class EndlessPagerAdapter extends PagerAdapter {
private static final String TAG = "EndlessPagerAdapter";
private static final boolean DEBUG = false;
private final PagerAdapter mPagerAdapter;
EndlessPagerAdapter(PagerAdapter pagerAdapter) {
if (pagerAdapter == null) {
throw new IllegalArgumentException("Did you forget initialize PagerAdapter?");
}
if ((pagerAdapter instanceof FragmentPagerAdapter || pagerAdapter instanceof FragmentStatePagerAdapter) && pagerAdapter.getCount() < 3) {
throw new IllegalArgumentException("When you use FragmentPagerAdapter or FragmentStatePagerAdapter, it only supports >= 3 pages.");
}
mPagerAdapter = pagerAdapter;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
if (DEBUG) Log.d(TAG, "Destroy: " + getVirtualPosition(position));
mPagerAdapter.destroyItem(container, getVirtualPosition(position), object);
if (mPagerAdapter.getCount() < 4) {
mPagerAdapter.instantiateItem(container, getVirtualPosition(position));
}
}
#Override
public void finishUpdate(ViewGroup container) {
mPagerAdapter.finishUpdate(container);
}
#Override
public int getCount() {
return Integer.MAX_VALUE; // this is the magic that we can scroll infinitely.
}
#Override
public CharSequence getPageTitle(int position) {
return mPagerAdapter.getPageTitle(getVirtualPosition(position));
}
#Override
public float getPageWidth(int position) {
return mPagerAdapter.getPageWidth(getVirtualPosition(position));
}
#Override
public boolean isViewFromObject(View view, Object o) {
return mPagerAdapter.isViewFromObject(view, o);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
if (DEBUG) Log.d(TAG, "Instantiate: " + getVirtualPosition(position));
return mPagerAdapter.instantiateItem(container, getVirtualPosition(position));
}
#Override
public Parcelable saveState() {
return mPagerAdapter.saveState();
}
#Override
public void restoreState(Parcelable state, ClassLoader loader) {
mPagerAdapter.restoreState(state, loader);
}
#Override
public void startUpdate(ViewGroup container) {
mPagerAdapter.startUpdate(container);
}
int getVirtualPosition(int realPosition) {
return realPosition % mPagerAdapter.getCount();
}
PagerAdapter getPagerAdapter() {
return mPagerAdapter;
}
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="180dp">
</android.support.v4.view.ViewPager>
</RelativeLayout>
row_item_viewpager.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtItem"
android:textAppearance="#android:style/TextAppearance.Large"/>
</LinearLayout>
Done
For infinite scrolling with days it's important you have the good fragment in the pager therefore I wrote my answer on this on page (Viewpager in Android to switch between days endlessly)
It's working very well! Above answers did not work for me as I wanted it to work.
I built a library that can make any ViewPager, pagerAdapter (or FragmentStatePagerAdapter), and optional TabLayout infinitely Scrolling.
https://github.com/memorex386/infinite-scroll-viewpager-w-tabs
Based on https://github.com/antonyt/InfiniteViewPager I wrote up this which works nicely:
class InfiniteViewPager #JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null
) : ViewPager(context, attrs) {
// Allow for 100 back cycles from the beginning.
// This should be enough to create an illusion of infinity.
// Warning: scrolling to very high values (1,000,000+) results in strange drawing behaviour.
private val offsetAmount get() = if (adapter?.count == 0) 0 else (adapter as InfinitePagerAdapter).realCount * 100
override fun setAdapter(adapter: PagerAdapter?) {
super.setAdapter(if (adapter == null) null else InfinitePagerAdapter(adapter))
currentItem = 0
}
override fun setCurrentItem(item: Int) = setCurrentItem(item, false)
override fun setCurrentItem(item: Int, smoothScroll: Boolean) {
val adapterCount = adapter?.count
if (adapterCount == null || adapterCount == 0) {
super.setCurrentItem(item, smoothScroll)
} else {
super.setCurrentItem(offsetAmount + item % adapterCount, smoothScroll)
}
}
override fun getCurrentItem(): Int {
val adapterCount = adapter?.count
return if (adapterCount == null || adapterCount == 0) {
super.getCurrentItem()
} else {
val position = super.getCurrentItem()
position % (adapter as InfinitePagerAdapter).realCount
}
}
fun animateForward() {
super.setCurrentItem(super.getCurrentItem() + 1, true)
}
fun animateBackwards() {
super.setCurrentItem(super.getCurrentItem() - 1, true)
}
internal class InfinitePagerAdapter(private val adapter: PagerAdapter) : PagerAdapter() {
internal val realCount: Int get() = adapter.count
override fun getCount() = if (realCount == 0) 0 else Integer.MAX_VALUE
override fun instantiateItem(container: ViewGroup, position: Int) = adapter.instantiateItem(container, position % realCount)
override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) = adapter.destroyItem(container, position % realCount, `object`)
override fun finishUpdate(container: ViewGroup) = adapter.finishUpdate(container)
override fun isViewFromObject(view: View, `object`: Any) = adapter.isViewFromObject(view, `object`)
override fun restoreState(bundle: Parcelable?, classLoader: ClassLoader?) = adapter.restoreState(bundle, classLoader)
override fun saveState(): Parcelable? = adapter.saveState()
override fun startUpdate(container: ViewGroup) = adapter.startUpdate(container)
override fun getPageTitle(position: Int) = adapter.getPageTitle(position % realCount)
override fun getPageWidth(position: Int) = adapter.getPageWidth(position)
override fun setPrimaryItem(container: ViewGroup, position: Int, `object`: Any) = adapter.setPrimaryItem(container, position, `object`)
override fun unregisterDataSetObserver(observer: DataSetObserver) = adapter.unregisterDataSetObserver(observer)
override fun registerDataSetObserver(observer: DataSetObserver) = adapter.registerDataSetObserver(observer)
override fun notifyDataSetChanged() = adapter.notifyDataSetChanged()
override fun getItemPosition(`object`: Any) = adapter.getItemPosition(`object`)
}
}
For consuming it simply change your ViewPager to InfiniteViewPager and that's all you need to change.

Categories

Resources