首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >翻转网格视图项,如windows瓷砖

翻转网格视图项,如windows瓷砖
EN

Stack Overflow用户
提问于 2015-05-06 14:12:10
回答 1查看 539关注 0票数 0

有一个翻转活动(如FlipBoard),它的适配器中有一个GridView作为项。GridView使用BaseAdapter。现在,我想像windows一样连续翻转GridView的内容。问题是我当前的方法在2组中翻转网格内容,只有当I已经翻转到FlipBoard视图上的最后一项时,这才能工作。

FlipAdapter是来自MainActivity的适配器,它使FlipBoard类似于动画成为可能:

代码语言:javascript
运行
复制
public class FlipAdapter extends BaseAdapter {

    public interface Callback {
        public void onPageRequested(int page);
    }

    static class Item {
        static long id = 0;

        long mId;

        public Item() {
            mId = id++;
        }

        long getId() {
            return mId;
        }
    }

    private LayoutInflater inflater;
    private Context mContext;
    private int h;
    private Callback callback;
    private List<Item> items = new ArrayList<Item>();

    public FlipAdapter(Context context, int height) {
        mContext = context;
        h = height;
        inflater = LayoutInflater.from(context);
        for (int i = 0; i < 2; i++) {
            items.add(new Item());
        }
    }

    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return items.get(position).getId();
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    ViewHolder holder;

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.page, parent, false);

            holder.gridView = (GridView) convertView.findViewById(R.id.home_grid);


            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.gridView.setAdapter(new HomeAdapter(mContext, h));
        holder.gridView.setVerticalScrollBarEnabled(false);
        holder.gridView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_MOVE) {
                    return true;
                }
                return false;
            }

        });

        holder.gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.e("index:", ""+holder.gridView.getChildCount());
            }
        });

        contAnimate();

        return convertView;
    }

    static class ViewHolder {
        GridView gridView;
    }

    static int lastTile = 0;
    int index = 0;

    public void contAnimate() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                while (lastTile == index)
                    index = 0 + new Random().nextInt(6);
                lastTile = index;

                ViewFlipper viewFlipper = (ViewFlipper) holder.gridView.getChildAt(index);
                Animation animation = AnimationUtils.loadAnimation(mContext, R.animator.card_flip_right_out);
                viewFlipper.setAnimation(animation);
                viewFlipper.startFlipping();
                contAnimate();
            }
        }, 5500);
    }

}

HomeAdapter是包装在FlipView中的GridView的适配器。

代码语言:javascript
运行
复制
public class HomeAdapter extends BaseAdapter {


    private LayoutInflater mLayoutInflater;
    private Context mContext;
    private int h;

    public HomeAdapter(Context context, int height) {
        mContext = context;
        mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        h = height;
    }

    @Override
    public int getCount() {
        return 6;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        FunctionHolder holder;
        if (convertView == null) {
            holder = new FunctionHolder();
            convertView = mLayoutInflater.inflate(R.layout.view_flipper, parent, false);
            holder.flipper = (ViewFlipper) convertView.findViewById(R.id.flipper);

        } else {
            holder = (FunctionHolder) convertView.getTag();
        }


        View topView = mLayoutInflater.inflate(R.layout.first_item, null);
        View bottomView = mLayoutInflater.inflate(R.layout.second_item, null);

        holder.flipper.addView(topView, 0);
        holder.flipper.addView(bottomView, 1);

        convertView.setMinimumHeight(h - 74);
        convertView.setTag(holder);
        return convertView;
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-06 14:49:20

代码语言:javascript
运行
复制
private void startAnimation(final ImageView imageView, int position)
    {
        // TODO Auto-generated method stub
        int iDuration = 500;
        final ScaleAnimation scaleAnimationIN = new ScaleAnimation(1.0f, 0.0f, 1.0f, 1.0f, Animation.RELATIVE_TO_SELF, (float) 0.5,
                Animation.RELATIVE_TO_SELF, (float) 0.5);
        final ScaleAnimation scaleAnimationOUT = new ScaleAnimation(0.0f, 1.0f, 1.0f, 1.0f, Animation.RELATIVE_TO_SELF, (float) 0.5,
                Animation.RELATIVE_TO_SELF, (float) 0.5);
        scaleAnimationIN.setStartOffset(8000 + position * 2000);
        scaleAnimationIN.setDuration(iDuration);
        scaleAnimationOUT.setStartOffset(0);
        scaleAnimationOUT.setDuration(iDuration);
        scaleAnimationIN.setAnimationListener(new AnimationListener()
        {

            @Override
            public void onAnimationStart(Animation animation)
            {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationRepeat(Animation animation)
            {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationEnd(Animation animation)
            {
                // TODO Auto-generated method stub
                imageView.startAnimation(scaleAnimationOUT);
            }
        });
        scaleAnimationOUT.setAnimationListener(new AnimationListener()
        {

            @Override
            public void onAnimationStart(Animation animation)
            {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationRepeat(Animation animation)
            {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationEnd(Animation animation)
            {
                // TODO Auto-generated method stub
                imageView.startAnimation(scaleAnimationIN);
            }
        });
        imageView.startAnimation(scaleAnimationIN);

    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30079281

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档