2

正如標題已經指出的那樣,我不確定搜索什麼來獲得對我的問題的幫助。我已經搜索了許多不同的組合,我認爲這是合適的術語,但直到現在,我似乎並沒有把事情說得對。複製視圖以創建視圖的精確副本。相對於屏幕移動視圖

目前我正在努力完成以下任務。 這是它應該如何工作:

我有一個scrollView與不同的意見。每次用戶點擊這個視圖時,視圖都應該被放到前面,獨立於scrollView的滾動位置移動到屏幕的頂部,並最終展開到新的視圖。

我也做了一個它的樣子。其實我想把它擴展到全屏。 animated example of the problem

我不確定是否必須在我的情況下創建新的活動,新的片段或視圖。我知道在Android L中這會更容易,但我也想在4.0 - 4.4中找到一種方法。感謝您的幫助!

回答

2
package com.example.anotherviewaddtest; 

import android.app.Activity; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.view.animation.Animation; 
import android.view.animation.TranslateAnimation; 
import android.view.animation.Animation.AnimationListener; 
import android.widget.FrameLayout; 
import android.widget.LinearLayout; 
import android.widget.ScrollView; 
import android.widget.TextView; 

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     LinearLayout layout=(LinearLayout) findViewById(R.id.Linear); 
     for(int i=0;i<50;i++){ //create a scrollview 
      TextView tv=new TextView(this); 
      tv.setTextColor(Color.BLUE); 
      tv.setText("t\ne\nx\nt\nV\nie\nw "+Integer.toString(i)); 
      tv.setBackgroundColor(Color.GREEN+10000*i); 
      tv.setOnClickListener((new OnClickListener() { 
       public void onClick(View v){      
        func(v); 
       } 
       })); 
      layout.addView(tv); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    public void func(View toAdd){ 

// values for animation x_start 
      int toAdd_top=toAdd.getTop(); 

     ViewGroup root=(ViewGroup) toAdd.getRootView(); 

//remove view from scrollview  
     ViewGroup parent=(ViewGroup)toAdd.getParent(); 
     ScrollView itsParent=(ScrollView)parent.getParent(); 
     parent.removeView(toAdd); 
    // animation values x_start, x_end and y_end 

int scrolledAmount=itsParent.getScrollY();  
      int margin=itsParent.getTop(); 
      int scrollView_left=itsParent.getLeft(); 
      int scrollView_top=itsParent.getTop(); 
      if(scrolledAmount==0) // fix for first element selected 
       scrolledAmount=-scrollView_top; 


//gets the main layout of the app and adds the removed to it. Traverse down along your own decor view if it is different . In this example i have decorView->LinearLayout(child at 0)->FrameLayout (child at 1)->main layout of app(child at 0) 
     ViewGroup base=(ViewGroup) ((FrameLayout)((LinearLayout)root.getChildAt(0)).getChildAt(1)).getChildAt(0); 
     base.addView(toAdd); 


// set the animation 
     TranslateAnimation ani= new TranslateAnimation(scrollView_left,scrollView_left,-scrolledAmount,-(toAdd_top-margin)); 
     ani.setDuration(1000); 
     toAdd.startAnimation(ani); 
//listener to call new activity at end of animation 
     ani.setAnimationListener(new AnimationListener(){ 
      @Override 
      public void onAnimationEnd(Animation anim){ 
       //start new Activity here 
      } 
      @Override 
      public void onAnimationRepeat(Animation arg0) {} 
      @Override 
      public void onAnimationStart(Animation arg0) {} 
      ;}); 
// removes the view that was added to main layout 
     base.removeView(toAdd); 
    } 
    } 

這是一個更簡單的方法。這需要視圖,將它添加到0,0的主佈局,然後動畫以您需要查找的值開始。在這個只有滾動視圖的例子中,視圖從它在屏幕上的位置轉換到滾動視圖的頂部。也許你可以以更簡單的方式做到這一點,只需將動畫設置爲適當的值,並簡單地從滾動佈局中刪除視圖,但我還沒有嘗試過,但

+0

非常感謝您的提示!我想從這個時候開始我可以進入下一步。我仍然想知道爲了尋找類似的結果,google能找到什麼......嘆息... – TheWhiteLlama 2014-10-01 17:52:11

+1

明天再來看看這裏,我會添加評論:-)我稍微佔據了一些rn – Rico 2014-10-01 18:22:10