多次防止动画RecycleView项目

时间:2016-11-14 06:53:13

标签: java android android-recyclerview recycler-adapter

我有一个RecycleView,在该类别中显示。

还有屏幕的页眉和页脚视图。并且RecycleView位于页眉和页脚视图之间。

默认情况下,页眉和页脚的可见性为GONE,但RecycleView中的VISIBLE可见性

当我在RecyclerView上设置类别数据时,它工作正常,所有项目都是单次动画。

但在我设置标题横幅列表并显示标题后的类别数据集。那时,recycleView向下移动,所有项目再次动画。

那么如何防止在屏幕上看到其他视图后的第二次动画?

布局文件:

        <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/margin_default"
            android:layout_marginRight="@dimen/margin_default"
            android:layout_marginTop="@dimen/margin_5dp">

            <RelativeLayout
                android:id="@+id/layoutBannerHeader"
                android:layout_width="match_parent"
                android:layout_height="@dimen/banner_height"
                android:layout_marginBottom="@dimen/margin_default"
                android:visibility="gone">

                <cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager
                    android:id="@+id/viewPagerHeader"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />

                <com.app.danube.utility.CirclePageIndicator
                    android:id="@+id/indicatorHeader"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true"
                    android:layout_marginBottom="@dimen/margin_5dp"
                    android:padding="@dimen/padding_default"
                    app:fillColor="@color/purple"
                    app:pageColor="@android:color/white"
                    app:strokeColor="@android:color/white" />
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/layoutContainer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/layoutBannerHeader">

                <ImageView
                    android:id="@+id/imgCategory"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:adjustViewBounds="true"
                    android:scaleType="fitXY" />

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <com.app.danube.utility.MyRecyclerView
                        android:id="@+id/recyclerSubCategory"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />

                    <ImageView
                        android:id="@+id/imgSubCategory"
                        android:layout_width="match_parent"
                        android:layout_height="@dimen/sub_category_image_height"
                        android:layout_alignParentBottom="true"
                        android:scaleType="fitXY"
                        android:visibility="gone" />
                </RelativeLayout>
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/layoutBannerFooter"
                android:layout_width="match_parent"
                android:layout_height="@dimen/banner_height"
                android:layout_below="@+id/layoutContainer"
                android:layout_marginBottom="@dimen/margin_default"
                android:visibility="gone">

                <cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager
                    android:id="@+id/viewPagerFooter"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />

                <com.app.danube.utility.CirclePageIndicator
                    android:id="@+id/indicatorFooter"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true"
                    android:layout_marginBottom="@dimen/margin_5dp"
                    android:padding="@dimen/padding_default"
                    app:fillColor="@color/purple"
                    app:pageColor="@android:color/white"
                    app:strokeColor="@android:color/white" />
            </RelativeLayout>
        </RelativeLayout>
    </ScrollView>

标头适配器

public class BannerAdapter extends PagerAdapter {
    private Context mContext = null;
    private ArrayList<ClsBanner> bannerList = new ArrayList<>();
    private LayoutInflater mLayoutInflater = null;

    public BannerAdapter(Context context, ArrayList<ClsBanner> arrayList) {
        this.mContext = context;
        this.bannerList = arrayList;
        mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return bannerList.size();
    }

    public void setData(ArrayList<ClsBanner> arrayList) {
        this.bannerList = arrayList;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((LinearLayout) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        View itemView = mLayoutInflater.inflate(R.layout.layout_banner_image, container, false);

        ImageView imageView = (ImageView) itemView.findViewById(R.id.imgBanner);
        Glide.with(mContext).load(bannerList.get(position).getFileName()).placeholder(R.mipmap.ic_placeholder).into(imageView);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mContext instanceof  HomeFragmentActivity)
                    ((HomeFragmentActivity) mContext).setBannerItemClick(bannerList.get(position));
                else if(mContext instanceof NotificationFragmentActivity)
                    ((NotificationFragmentActivity) mContext).setBannerItemClick(bannerList.get(position));
            }
        });

        container.addView(itemView);
        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((LinearLayout) object);
    }

}

类别文件

   public class SubCategoryAdapter extends RecyclerView.Adapter<SubCategoryAdapter.MyViewHolder> implements KeyInterface {
    private SubCategoryListFragment fragment = null;
    private ArrayList<ClsCategory> subCategoryList = null;

    public SubCategoryAdapter(SubCategoryListFragment fragment, ArrayList<ClsCategory> subCategoryList) {
        this.fragment = fragment;
        this.subCategoryList = subCategoryList;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView txtTitle = null;
        private ImageView imgCategory = null;

        public MyViewHolder(View itemView) {
            super(itemView);
            txtTitle = (TextView) itemView.findViewById(R.id.txtTitle);
            imgCategory = (ImageView) itemView.findViewById(R.id.imgCategory);

            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            fragment.itemClickListener(getAdapterPosition());
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_sub_category, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        ClsCategory clsCategory = subCategoryList.get(position);
        holder.txtTitle.setText(clsCategory.getName());
        if (clsCategory.getCategoryId().equals("-1"))
            holder.imgCategory.setVisibility(View.GONE);
        else {
            holder.imgCategory.setVisibility(View.VISIBLE);
            //Glide.with(holder.itemView.getContext()).load(clsCategory.getImage()).into(holder.imgCategory);
            Glide.with(holder.itemView.getContext()).load(clsCategory.getImage()).asBitmap().listener(new RequestListener<String, Bitmap>() {
                @Override
                public boolean onException(Exception e, String model, Target<Bitmap> target, boolean isFirstResource) {
                    return false;
                }

                @Override
                public boolean onResourceReady(Bitmap resource, String model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {
                    holder.imgCategory.setImageBitmap(resource);
                    return false;
                }
            }).into(new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                    holder.imgCategory.setImageBitmap(resource);
                }
            });
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return subCategoryList.size();
    }

}

设置适配器。

RecyclerView recyclerSubCategory = (RecyclerView) root.findViewById(R.id.recyclerSubCategory);
    recyclerSubCategory.setLayoutManager(new LinearLayoutManager(mContext));
    recyclerSubCategory.addItemDecoration(new SimpleDividerItemDecoration(mContext, R.drawable.ic_list_divider));
    subCategoryAdapter = new SubCategoryAdapter(this, subCategoryList);
    recyclerSubCategory.setAdapter(subCategoryAdapter);



      viewPagerHeader = (AutoScrollViewPager) root.findViewById(R.id.viewPagerHeader);
        headerBannerAdapter = new BannerAdapter(mContext, headerBannerList);
        viewPagerHeader.setAdapter(headerBannerAdapter);

viewPagerFooter = (AutoScrollViewPager) root.findViewById(R.id.viewPagerFooter);
        footerBannerAdapter = new BannerAdapter(mContext, footerBannerList);
        viewPagerFooter.setAdapter(footerBannerAdapter);

现在在WS调用成功。

if (subCategoryAdapter != null)
      subCategoryAdapter.notifyDataSetChanged();
callBannerListService();

横幅WS电话。

 if (headerBannerAdapter != null)
        headerBannerAdapter.notifyDataSetChanged();
    if (footerBannerAdapter != null)
        footerBannerAdapter.notifyDataSetChanged();
    if (headerBannerList.size() > 0) {
        if (layoutBannerHeader != null)
            layoutBannerHeader.setVisibility(View.VISIBLE);
        if (viewPagerHeader != null) {
            viewPagerHeader.startAutoScroll((int) DELAY_MILLIS);
            viewPagerHeader.setInterval(BANNER_DELAY_MILLIS);
        }
    }




if (footerBannerList.size() > 0) {
            if (layoutBannerFooter != null)
                layoutBannerFooter.setVisibility(View.VISIBLE);
            if (viewPagerFooter != null) {
                viewPagerFooter.startAutoScroll((int) DELAY_MILLIS);
                viewPagerFooter.setInterval(BANNER_DELAY_MILLIS);
            }
        }

MyRecycleView

public class MyRecyclerView extends RecyclerView {
    private boolean mScrollable = false;

    public MyRecyclerView(Context context) {
        super(context, null);
    }

    public MyRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs, 0);
    }

    public MyRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return !mScrollable || super.dispatchTouchEvent(ev);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        Log.d("onLayout", "onLayout: ");
        if (!mScrollable) {
            for (int i = 0; i < getChildCount(); i++) {
                animate(getChildAt(i), i);

                if (i == getChildCount() - 1) {
                    getHandler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            mScrollable = true;
                        }
                    }, i * 100);
                }
            }
        }
    }

    private void animate(View view, int pos) {
        Log.d("animate", "animate: " + pos);
        view.animate().cancel();
        view.setTranslationY(100);
        view.setAlpha(0);
        view.animate().alpha(1.0f).translationY(0).setDuration(300).setStartDelay(pos * 100);
    }
}

0 个答案:

没有答案