像网格瓷砖一样翻转gridview的项目

时间:2015-05-06 14:12:10

标签: java android gridview

有一个翻转活动(如FlipBoard),其适配器中有一个GridView项。 GridView使用BaseAdapter。现在,我想像Windows窗格一样翻转GridView的内容。问题是我当前的方法是在2组中翻转网格内容,这仅在我翻转到FlipBoard视图中的最后一项时才有效。

FlipAdapter是来自MainActivity的适配器,可以使FlipBoard像动画一样:

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);
    }

}

HomeAdapterGridView中包含的FlipView的适配器:

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;
    }
}

1 个答案:

答案 0 :(得分:1)

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);

    }