Android动画回收视图

Android动画回收视图,第1张

概述我创建了一个带有“事件”列表的recyclerview.这适用于低于5的事件列表.但是当我在列表中获得6个或更多事件时,最后一个事件在点击时不会扩展,而是消失.结束动画也停止使用列表中的6个以上事件. 它应该如何表现: >用户点击事件>视图扩展到全屏 >用户点击展开的事件>视图折叠回原始大小 >用户在展开另一个事件时点击事件>展开事件设置为原始高度.已点击事件扩展为全屏 目前的行为: >用户点击事 我创建了一个带有“事件”列表的recyclervIEw.这适用于低于5的事件列表.但是当我在列表中获得6个或更多事件时,最后一个事件在点击时不会扩展,而是消失.结束动画也停止使用列表中的6个以上事件.

它应该如何表现:

>用户点击事件>视图扩展到全屏
>用户点击展开的事件>视图折叠回原始大小
>用户在展开另一个事件时点击事件>展开事件设置为原始高度.已点击事件扩展为全屏

目前的行为:

>用户点击事件>除了列表中的最后一项外,所有视图都可以正确扩展
>用户点击扩展事件>查看折叠但不动画
>用户在展开另一个事件时点击事件>展开的事件折叠和tapped事件正确扩展
>用户点击列表中的最后一个事件>事件消失(可能会将其大小减小到0以下)

我知道这可能与recyclelervIEw在屏幕外时重用其视图的方式有关.为了解决这个问题,我通过eventID检查tapped事件的位置,而不是列表中的位置,但这仍然留下了我上面谈到的问题.

public class EventRecyclerAdapter extends RecyclerVIEw.Adapter<EventRecyclerAdapter.VIEwHolder> {    private Context c;    private List<Event> items = new ArrayList<>();    private relativeLayout container;    private int screenheight;    private EventListFragment eventListFragment;    private int expandedposition = -1;    private static final String TAG = "EventRecyclerAdapter";    public interface ItemClickedListener {        voID itemClicked(int position);    }    private ItemClickedListener itemClickedListener;    public EventRecyclerAdapter(List<Event> itemList,Context c,EventListFragment eventListFragment,ItemClickedListener Listener) {        this.items = itemList;        this.c = c;        this.eventListFragment = eventListFragment;        this.itemClickedListener = Listener;    }    @OverrIDe    public VIEwHolder onCreateVIEwHolder(VIEwGroup parent,int vIEwType) {        // create a new vIEw        VIEw itemLayoutVIEw = LayoutInflater.from(parent.getContext()).inflate(R.layout.List_item,null);        WindowManager wm = (WindowManager) c.getSystemService(Context.WINDOW_SERVICE);        display display = wm.getDefaultdisplay();        Point size = new Point();        display.getSize(size);        screenheight = size.y;        // Get the screen height from the device        Resources r = c.getResources();        float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,80,r.getdisplayMetrics());        screenheight -= px;        VIEwHolder vIEwHolder = new VIEwHolder(itemLayoutVIEw);        return vIEwHolder;    }    // Replace the contents of a vIEw (invoked by the layout manager)    @OverrIDe    public voID onBindVIEwHolder(VIEwHolder vIEwHolder,int position) {        Event event = items.get(position);        // - get data from your itemsData at this position        // - replace the contents of the vIEw with that itemsData        vIEwHolder.tvname.setText(event.getname());        vIEwHolder.tvLocation.setText(event.getLocation().getname());        vIEwHolder.tvDate.setText(Helper.dateDoubletoString(event.getStartDate()));        vIEwHolder.tvTicketCount.setText(String.valueOf(event.getNumberOfTickets()));        vIEwHolder.background.setBackgroundcolor(color.GRAY);        vIEwHolder.eventID = event.getID();        // Load the background image        if (event.getEventimageID() != null) {            Picasso.with(c).load(Helper.imageUrlString(event.getEventimageID())).into(vIEwHolder.background);            colorMatrix matrix = new colorMatrix();            matrix.setSaturation(0);            colorMatrixcolorFilter filter = new colorMatrixcolorFilter(matrix);            vIEwHolder.background.setcolorFilter(filter);        }        // Check if the vIEw needs to be expanded,collapsed or just drawn normally.        if (expandedposition == event.getID()) {            if (event.expanded) {                collapseVIEw(vIEwHolder,event);            } else if (!event.expanded) {                expandVIEw(vIEwHolder,position,event);            }        } else {            setContainerHeight(vIEwHolder,event);        }    }    private voID expandVIEw(final EventRecyclerAdapter.VIEwHolder vIEwHolder,final int pos,Event event) {        ResizeAnimation resizeAnimation = new ResizeAnimation(                vIEwHolder.container,vIEwHolder.container.getHeight(),screenheight        );        resizeAnimation.setDuration(Constants.ANIMATION_SPEED);        resizeAnimation.setAnimationListener(new Animation.AnimationListener() {            @OverrIDe            public voID onAnimationStart(Animation animation) {            }            @OverrIDe            public voID onAnimationEnd(Animation animation) {                vIEwHolder.infoContainer.setVisibility(VIEw.VISIBLE);                vIEwHolder.closeIcon.setVisibility(VIEw.VISIBLE);                itemClickedListener.itemClicked(pos);            }            @OverrIDe            public voID onAnimationRepeat(Animation animation) {            }        });        vIEwHolder.itemVIEw.startAnimation(resizeAnimation);        vIEwHolder.expanded = true;        event.expanded = true;    }    private voID collapseVIEw(final EventRecyclerAdapter.VIEwHolder vIEwHolder,getContainerCollapsedHeight()        );        resizeAnimation.setDuration(Constants.ANIMATION_SPEED);        vIEwHolder.infoContainer.setVisibility(VIEw.INVISIBLE);        vIEwHolder.closeIcon.setVisibility(VIEw.INVISIBLE);        vIEwHolder.itemVIEw.startAnimation(resizeAnimation);        vIEwHolder.expanded = false;        event.expanded = false;}    private voID setContainerHeight(EventRecyclerAdapter.VIEwHolder vIEwHolder,Event event) {        vIEwHolder.container.setLayoutParams(new FrameLayout.LayoutParams(VIEwGroup.LayoutParams.MATCH_PARENT,getContainerCollapsedHeight()));        vIEwHolder.infoContainer.setVisibility(VIEw.INVISIBLE);        vIEwHolder.closeIcon.setVisibility(VIEw.INVISIBLE);        event.expanded = false;        vIEwHolder.expanded = false;    }    private int getContainerCollapsedHeight() {        int containerHeight;        // define the item containers height        if (items.size() <= 3) {            containerHeight = screenheight / items.size();        } else {            containerHeight = screenheight / 3;        }        return containerHeight;    }    /**     * Clear all current data and swap add the new data List.     * The expanded position also gets reset     * @param events     */    public voID swap(List<Event> events) {        this.items.clear();        this.items.addAll(events);        this.expandedposition = -1;        Log.v(TAG,"SWAP SIZE : " + items.size());        notifyDataSetChanged();    }    // inner class to hold a reference to each item of RecyclerVIEw    class VIEwHolder extends RecyclerVIEw.VIEwHolder {        public TextVIEw tvLocation,tvDate,tvTicketCount;        public TextVIEw tvname;        public ImageVIEw background;        public VIEw container;        public VIEw infoContainer;        public TextVIEw closeIcon;        public int eventID;        public boolean expanded = false;        public VIEwHolder(final VIEw itemLayoutVIEw) {            super(itemLayoutVIEw);            tvname = (TextVIEw) itemLayoutVIEw.findVIEwByID(R.ID.tvname);            tvLocation = (TextVIEw) itemLayoutVIEw.findVIEwByID(R.ID.tvLocation);            tvDate = (TextVIEw) itemLayoutVIEw.findVIEwByID(R.ID.tvDate);            background = (ImageVIEw) itemLayoutVIEw.findVIEwByID(R.ID.background);            tvTicketCount = (TextVIEw) itemLayoutVIEw.findVIEwByID(R.ID.ticket_count);            container = itemLayoutVIEw.findVIEwByID(R.ID.List_item_container);            infoContainer = itemLayoutVIEw.findVIEwByID(R.ID.info_container);            closeIcon = (TextVIEw) itemLayoutVIEw.findVIEwByID(R.ID.close_icon);            infoContainer.setonClickListener(new VIEw.OnClickListener() {                @OverrIDe                public voID onClick(VIEw vIEw) {                    Activity mainActivity = (Activity) c;                    FragmentManager fm = mainActivity.getFragmentManager();                    //add                    FragmentTransaction ft = fm.beginTransaction();                    ft.setCustomAnimations(R.animator.slIDe_to_top,R.animator.slIDe_from_bottom);                    ft.addToBackStack(ft.toString());                    ft.add(R.ID.content_frame,EventFragment.newInstance(items.get(getAdapterposition())),Constants.EVENT_FRAGMENT_TAG);                    //commit change                    ft.commit();                }            });            container.setonClickListener(new VIEw.OnClickListener() {                @OverrIDe                public voID onClick(VIEw v) {                    expandedposition = eventID;                    notifyDataSetChanged();                }            });        }    }    // Return the size of your itemsData (invoked by the layout manager)    @OverrIDe    public int getItemCount() {        return items.size();    }}

我认为当我点击列表中的最后一项时,它以某种方式运行collapseVIEw方法,导致其高度低于0.但我无法弄清楚为什么会发生这种情况.
我希望有人能够发现这里的错误.

解决方法 你可以在的onClilck中尝试使用recyclevIEw Item

@OverrIDepublic voID onClick(VIEw vIEw){LayoutParams params = vIEw.getLayoutParams();     if (!large)        {            params.height = 2 * vIEw.getHeight();             } else {            params.height = vIEw.getHeight()/2;        }        large = !large;        vIEw.setLayoutParams(params);}
总结

以上是内存溢出为你收集整理的Android动画回收视图全部内容,希望文章能够帮你解决Android动画回收视图所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1123972.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存