I'm using FancyCoverFlow to show a CoverView in my Android App.
All works fine but the only think I can't do is overlap the items.
This is what I'm trying to do: preview
I tried using
coverFlow.setSpacing(-90);
or other configuration (such as -200 or -50), but setSpacing doesn't change anything in my final result.
This is the Adapter:
coverFlowAdapter = new FancyCoverFlowAdapter() {
#Override
public View getCoverFlowItem(int position, View reuseableView, ViewGroup viewGroup) {
v = reuseableView;
if (v == null) {
v = LayoutInflater.from(activity).inflate(R.layout.coverflo_item, viewGroup, false);
}
//do my stuff...
return v;
}
#Override
public int getCount() {
//do my stuff...
}
#Override
public Home getItem(int position) {
//do my stuff...
}
#Override
public long getItemId(int position) {
return 0;
}
};
These are coverFlow setting:
coverFlow.setAdapter(coverFlowAdapter);
coverFlow.setUnselectedSaturation(0.0f);
coverFlow.setSpacing(0);
coverFlow.setMaxRotation(0);
coverFlow.setScaleDownGravity(0.2f);
coverFlow.setReflectionEnabled(false);
coverFlow.setUnselectedAlpha(1.0f);
coverFlow.setActionDistance(FancyCoverFlow.ACTION_DISTANCE_AUTO);
coverFlow.setUnselectedScale(0.75f);
There is another StackOverflow question about this issue, but is not useful:
Android Cover Flow gallery and remove space between
I tried also other sources, but I found only coverFlow libraries that use image as item, while I need to use my custom layout.
Thanks.
Related
Okay so we have some realm results returning a set of stock orders that we have made. Depending on if the stock order is fully checked in we are showing the background colour of the row item as green or blue using an if statement in the adaptor.
The issue is that on load the orders display as green/blue correctly. But when you scroll down the blue ones are changing to green. (See Images). We think it may be that the data is getting changed somewhere but not sure why this would be happening as we are just scrolling on the page.
Code that creates this:
StockOrdersFragment.java
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.stock_orders_layout, container, false);
listView = myView.findViewById(R.id.listView);
stockOrdersService = new StockOrdersService(this);
stockOrdersService.fetchFromServer(getActivity());
fetchStockOrders();
return myView;
}
public void fetchStockOrders()
{
stockOrders.clear();
RealmResults<StockOrder> savedStockOrders = stockOrdersService.all();
stockOrders.addAll(savedStockOrders);
StockOrdersAdaptor adaptor = new StockOrdersAdaptor(stockOrders, getActivity());
listView.setAdapter(adaptor);
}
#Override
public void ordersReceived() {
fetchStockOrders();
}
StockOrdersAdaptor.java (The bit that does the colour )
lastPosition = position;
if(stockOrder != null){
viewHolder.stockOrderId.setText(String.valueOf(stockOrder.id));
viewHolder.supplierName.setText(stockOrder.supplier.name);
if (stockOrder.allItemsHaveBeenReceived()) {
convertView.setBackgroundResource(R.color.colorGreen);
}
}
// Return the completed view to render on screen
return convertView;
The allItemsHaveBeenRecieved()
public Boolean allItemsHaveBeenReceived()
{
for (StockOrderDetails detail: details) {
if (detail.quantity != detail.quantityReceived) {
return false;
}
}
return true;
}
Add else statement here:
if (stockOrder.allItemsHaveBeenReceived()) {
convertView.setBackgroundResource(R.color.colorGreen);
} else {
convertView.setBackgroundResource(R.color.colorBlue);
}
Problem is when your item gets recycled it remains dirty. Make sure you are populating all UI items from your list item layout every time adapter binds to it.
I have fragment from which I'm launching activity with shared element transition that has viewpager in it, the enter transition works fine but when i scroll in view pager and finish transition the shared image comes from left side which is not desired it should reposition itself to where it was launched, here is my code:
Intent myIntent = new Intent(getActivity(), EnlargeActivity.class);
ActivityOptionsCompat options = ActivityOptionsCompat.
makeSceneTransitionAnimation(getActivity(),
imageView,
ViewCompat.getTransitionName(imageView));
startActivity(myIntent, options.toBundle());
I'm updating view and its name in activity that contains viewpager when finishing activity, but its going with blink:
public void finishAfterTransition() {
setEnterSharedElementCallback(new SharedElementCallback() {
#Override
public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
// Clear all current shared views and names
names.clear();
sharedElements.clear();
ViewGroup viewGroup = (ViewGroup) viewPagerDetail.getAdapter()
.instantiateItem(viewPagerDetail, viewPagerDetail.getCurrentItem());
if (viewGroup == null) {
return;
}
// Map the first shared element name to the child ImageView.
sharedElements.put(viewGroup.findViewById(R.id.img).getTransitionName(), viewGroup.findViewById(R.id.img));
// setExitSharedElementCallback((SharedElementCallback) this);
}
});
super.finishAfterTransition();
Basically, Android start the transition with your pre-defined View and transitionName and automatically use the same properties for the return transition. When you change your focused View in ViewPager, Android doesn't know about that and keep the transition on the previous one on its way back. So you need to inform Android about the changes:
Remap the transition properties: Use setEnterSharedElementCallback to change the transitionName and View to the new one before returning from Activity2.
Wait for the Activity1 to finish rendering addOnPreDrawListener.
It's a bit complex in the final implementation. But you can look at my sample code https://github.com/tamhuynhit/PhotoGallery. I try to implement the shared-element-transition from many simple to complex sections.
Your problem appeared from Level 3 and solved in Level 4.
I am writing a tutorial about this but it's not in English so hope the code can help
UPDATE 1: Work flow
Here is how I implement it in my code:
Override finishAfterTransition in Activity2 and call setEnterSharedElementCallback method to re-map the current selected item in ViewPager. Also, call setResult to pass the new selected index back to previous activity here.
#Override
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void finishAfterTransition() {
setEnterSharedElementCallback(new SharedElementCallback() {
#Override
public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
View selectedView = getSelectedView();
if (selectedView == null)
return;
// Clear all current shared views and names
names.clear();
sharedElements.clear();
// Store new selected view and name
String transitionName = ViewCompat.getTransitionName(selectedView);
names.add(transitionName);
sharedElements.put(transitionName, selectedView);
setExitSharedElementCallback((SharedElementCallback) null);
}
});
Intent intent = new Intent();
intent.putExtra(PHOTO_FOCUSED_INDEX, mCurrentIndex);
setResult(RESULT_PHOTO_CLOSED, intent);
super.finishAfterTransition();
}
Write a custom ShareElementCallback so I can set the callback before knowing which View is going to be used.
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static class CustomSharedElementCallback extends SharedElementCallback {
private View mView;
/**
* Set the transtion View to the callback, this should be called before starting the transition so the View is not null
*/
public void setView(View view) {
mView = view;
}
#Override
public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
// Clear all current shared views and names
names.clear();
sharedElements.clear();
// Store new selected view and name
String transitionName = ViewCompat.getTransitionName(mView);
names.add(transitionName);
sharedElements.put(transitionName, mView);
}
}
Override onActivityReenter in Activity1, get the selected index from the result Intent. Set setExitSharedElementCallback to re-map new selected View when the transition begins.Call supportPostponeEnterTransition to delay a bit because your new View may not be rendered at this point. Use getViewTreeObserver().addOnPreDrawListener to listen for the layout changes, find the right View by the selected index and continue the transition supportStartPostponedEnterTransition.
#Override
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void onActivityReenter(int resultCode, Intent data) {
if (resultCode != LevelFourFullPhotoActivity.RESULT_PHOTO_CLOSED || data == null)
return;
final int selectedIndex = data.getIntExtra(LevelFourFullPhotoActivity.PHOTO_FOCUSED_INDEX, -1);
if (selectedIndex == -1)
return;
// Scroll to the new selected view in case it's not currently visible on the screen
mPhotoList.scrollToPosition(selectedIndex);
final CustomSharedElementCallback callback = new CustomSharedElementCallback();
getActivity().setExitSharedElementCallback(callback);
// Listen for the transition end and clear all registered callback
getActivity().getWindow().getSharedElementExitTransition().addListener(new Transition.TransitionListener() {
#Override
public void onTransitionStart(Transition transition) {}
#Override
public void onTransitionPause(Transition transition) {}
#Override
public void onTransitionResume(Transition transition) {}
#Override
public void onTransitionEnd(Transition transition) {
removeCallback();
}
#Override
public void onTransitionCancel(Transition transition) {
removeCallback();
}
private void removeCallback() {
if (getActivity() != null) {
getActivity().getWindow().getSharedElementExitTransition().removeListener(this);
getActivity().setExitSharedElementCallback((SharedElementCallback) null);
}
}
});
// Pause transition until the selected view is fully drawn
getActivity().supportPostponeEnterTransition();
// Listen for the RecyclerView pre draw to make sure the selected view is visible,
// and findViewHolderForAdapterPosition will return a non null ViewHolder
mPhotoList.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
#Override
public boolean onPreDraw() {
mPhotoList.getViewTreeObserver().removeOnPreDrawListener(this);
RecyclerView.ViewHolder holder = mPhotoList.findViewHolderForAdapterPosition(selectedIndex);
if (holder instanceof ViewHolder) {
callback.setView(((ViewHolder) holder).mPhotoImg);
}
// Continue the transition
getActivity().supportStartPostponedEnterTransition();
return true;
}
});
}
UPDATE 2: getSelectedItem
To get selected View from the ViewPager, don't use getChildAt or you get the wrong View, use findViewWithTag instead
In the PagerAdapter.instantiateItem, use position as tag for each View:
#Override
public View instantiateItem(ViewGroup container, int position) {
// Create the View
view.setTag(position)
// ...
}
Listen to onPageSelected event to get the selected index:
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
mSelectedIndex = position;
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
Call getSelectedView to get the current view by the selected index
private View getSelectedView() {
try {
return mPhotoViewPager.findViewWithTag(mSelectedIndex);
} catch (IndexOutOfBoundsException | NullPointerException ex) {
return null;
}
}
This is actually a default behavior, I was struggling SharedElementTransitions a lot, but I have nested fragments. I got my solution from an article (very recent article), it shows an implementation with a RecyclerView, which I assume you have. In short, the solution is to override onLayoutChange :
recyclerView.addOnLayoutChangeListener(
new OnLayoutChangeListener() {
#Override
public void onLayoutChange(View view,
int left,
int top,
int right,
int bottom,
int oldLeft,
int oldTop,
int oldRight,
int oldBottom) {
recyclerView.removeOnLayoutChangeListener(this);
final RecyclerView.LayoutManager layoutManager =
recyclerView.getLayoutManager();
View viewAtPosition =
layoutManager.findViewByPosition(MainActivity.currentPosition);
// Scroll to position if the view for the current position is null (not
// currently part of layout manager children), or it's not completely
// visible.
if (viewAtPosition == null
|| layoutManager.isViewPartiallyVisible(viewAtPosition, false, true)){
recyclerView.post(()
-> layoutManager.scrollToPosition(MainActivity.currentPosition));
}
}
});
Here is the article, and you will also find the project on GitHub.
Question from Noob android developer
Issue Defitition :
I'm trying to achieve endless scrolling implement functionality of loading more data via network request when the FragmentStatePagerAdapter reaches last item, currently i'm setting static number for getCount to 10//, what i'd like to do is trigger a network request as soon as it hits 7th item to get 10 more items and refresh the list, keeping the cycle going and potentially end up with more than 100 items hence why i'm using FragmentStatePagerAdapter, also store/cache the data so to support left to right & right to left swipe
Here's what i've tried so far
Read this article infinite viewpager however it only works for
limited set of fragments perhaps a static number, what i'm trying to
do is more dynamic as such I dont have a fixed getCount.
Read the article endless scrolling adapters, i'm not trying to use
recycler view as viewpager works just fine for what i'm trying to do
Few more pageradapter implementations
what i've learnt so far
need to override getcount to return the maximum possible value
public int getCount() {
#Override
return Integer.MAX_VALUE;
}
*I'm not sure if should also override getItemPosition or implement some kind on pageListener there are many examples available online using pagerAdapter/fragmentPageradapter i'm getting confused as to which ones are related to FragmentStatePagerAdapter and which ones are not
*
// My Framgent class
public class ScreenSlidePageFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.activity_screen_slide_page_fragment,container,false);
return rootView;
}
public static ScreenSlidePageFragment newInstance (String url){
ScreenSlidePageFragment newFragment = new ScreenSlidePageFragment();
Bundle args = new Bundle();
args.putString("imagePathUrl", url);
newFragment.setArguments(args);
return newFragment;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ImageView mImageView = (ImageView) getView().findViewById(R.id.imageView);
String imageUrl = getArguments().getString("imagePathUrl");
if (imageUrl==null){
Log.i("ScreenSlidePageFragment","no data passed");
}else {
Glide.with(this).load(imageUrl).into(mImageView);
}
}
}
// My adapter implementation
public class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
List<Resource> mResources;
String url;
Context mContext;
private int NUM_PAGES = 5;
private int currentPosition = 0;
public ScreenSlidePagerAdapter(FragmentManager fm, List<Resource> res,
Context context) {
super(fm);
mResources = res;
mContext = context;
}
#Override
public Fragment getItem(int position) {
if (mResources != null & mResources.size() > 0){
url = mResources.get(position).getUrl();
Log.i("url",url);
return ScreenSlidePageFragment.newInstance(url);
}else {
Toast.makeText(mContext,"no results returned",Toast.LENGTH_LONG).show();
return null;
}
}
#Override
public int getItemPosition(Object object) {
return super.getItemPosition(object);
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
any help will be highly appreciated
Use FragmentStatePagerAdapter (support.v13) and implement getItemPosition like this:
public int getItemPosition(Object object) {
return POSITION_NONE;
}
https://hedgehogjim.wordpress.com/2013/10/03/android-updatable-swipe-navigation-with-fragmentstatepageradapter/
return POSITION_NONE "Causes adapter to reload all Fragments when notifyDataSetChanged is called"
Add a OnPageChangeListener to your ViewPager and load more data after comparing the given position value with your current data size
#Override
public void onPageSelected(int position) {
//Load previous data set if position == 0
//Load next data set if position == myAdapter.mResources.size() - 1
}
After the new data set has been loaded (and sorted), call myAdapter.notifyDataSetChanged() then calculate and set the new index to match the old offset so the user don't see any shift
myViewPager.setCurrentItem(myNewIndex, false)
Note: Sorting and new index calculation is only necessary when loading a previous data set.
Ive searched and cant find anything to do with this problem im getting, im loading images into multiple grid views each inside a tabbed page view. Performance is great but any grid view that has lots of images and needs to scroll the images loaded are incorrect, it dublicates images and loads them into the wrong position. Below is one of the adapters im using for the grid view:
public class PcAdapter extends BaseAdapter {
private Context context;
private Integer[] imageIds = {
R.drawable.pcserioussam, R.drawable.pc_trinetwo,
R.drawable.pc_leftfordead, R.drawable.pc_dungeondefenders,
R.drawable.pc_portaltwo, R.drawable.pc_spaz,
R.drawable.pc_laracroftattoo, R.drawable.pc_goatsim,
R.drawable.pc_deadblock, R.drawable.pc_dynasty,
R.drawable.pc_minecraft, R.drawable.pc_kanelynch,
R.drawable.pc_toy, R.drawable.pc_awesomenauts,
R.drawable.pc_bioniccomm, R.drawable.pc_fastandfurious,
R.drawable.gen_harryone, R.drawable.gen_harrytwo,
R.drawable.gen_watchmen
};
public PcAdapter(Context c) {
context = c;
}
public int getCount() {
return imageIds.length;
}
public Object getItem(int position) {
return imageIds[position];
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View view, ViewGroup parent) {
ImageView iview;
if(view == null){
iview = new ImageView(context);
Picasso.with(context).load(imageIds[position]).
placeholder(R.drawable.placeholder).
resize(230, 300).centerInside().into(iview);
} else {
iview = (ImageView) view;
}
return iview;
}
}
Any help with this would be much appreciated
Move your picasso code out of the if statement. Currently it will only load a new image if view is null? Put it at the bottom of the if statement just before the return statement.
I have a simple array of Strings that I was displaying in a horizontal ListView with an ArrayAdapter. What I'm looking to do is: when the user selects an item from the ListView, make that item not clickable and change the background color of that item. Perhaps like a "grayed-out" look to it. I was looking into creating a custom Adapter and overriding the isEnabled(int position) method but I don't know how I would go about this. Any advice, suggestions, or help will be greatly appreciated thanks!
I was looking into creating a custom Adapter and overriding the isEnabled(int position) method but I don't know how I would go about this.
This is quite easy to do. I recommend a SparseBooleanArray to track the enabled items for efficiency:
public class MyAdapter extends ArrayAdapter<String> {
private SparseBooleanArray enabledItems = new SparseBooleanArray();
public MyAdapter(Context context, int textViewResourceId, List<String> objects) {
super(context, textViewResourceId, objects);
}
#Override
public boolean areAllItemsEnabled() {
return false;
}
#Override
public boolean isEnabled(int position) {
return enabledItems.get(position, true);
}
public void toggleItem(int position) {
boolean state = enabledItems.get(position, true);
enabledItems.put(position, !state);
}
}
The AutoComplete feature of Eclipse did must of the work, but here are some quick notes:
You must override areAllItemsEnabled() along with isEnabled()
I designed toggle() to be used by an onItemClickListener() you only need to call adapter.toggle(position)
If you want to change the row's appearance (more than what enabling and disabling does by default) simply override getView(). Don't forget to cover both cases:
public View getView(int position, View convertView, ViewGroup parent) {
convertView = super.getView(position, convertView, parent);
if(!isEnabled(position)) {
/* change to disabled appearance */
}
else {
/* restore default appearance */
}
return convertView;
}
Hope that helps!
pass position to adapter class when you click on list item
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
adapter.setSelectedIndex(position);
}
add method of setSelectedIndex to adapter class
public void setSelectedIndex(int ind)
{
selectedIndex = ind;
notifyDataSetChanged();
}
Now check the postion of this listview if same then enable and disable value in getView me method
if(selectedIndex!= -1 && position == selectedIndex)
{
holder.tv.setBackgroundColor(Color.BLACK);
}
else
{
holder.tv.setBackgroundColor(selectedColor);
}
holder.tv.setText("" + (position + 1) + " " + testList.get(position).getTestText());
Reference from here
Use setEnabled(bool) property:
yourlistview.setEnabled(false);
Not sure whether it will work or not
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// your code
view.setBackgroundColor(Color.BLUE);
view.setEnabled(false);
}