2015-05-06 31 views
0

有一個翻轉活動(如FlipBoard),其中有一個GridView作爲其適配器中的項目。 GridView使用BaseAdapter。現在,我想翻轉GridView的內容,像窗口瓷磚一樣。問題是我現在的方法是將2格組中的網格內容翻轉過來,這隻有當我翻轉到FlipBoard視圖中的最後一項時才起作用。像窗口瓷磚一樣的GridView翻轉項目

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

任何那些的動畫可以工作:https://developer.android.com/training/animation/cardflip.html? – shkschneider

回答

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

    } 
+0

和這應該在哪裏實施? –

+0

很酷。作品。很棒。 –