2017-01-08 115 views
-1

我如何實現幻燈片等動畫過渡當我通過列表視圖項目點擊打開新的活動?Android列表視圖項目的Android活動轉換點擊

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, 
           int position, long id) { 
      Intent intent = null; 
      // global string to class 
      selectedValue = String.valueOf(parent.getItemAtPosition(position)); 

      if (selectedValue.equals("item1")) { 
            // ^^^ use any item value here you want 
       Intent myIntent = new Intent(view.getContext(), activity1.class); 
       startActivityForResult(myIntent,0); 
      } 

      else if (selectedValue.equals("item2")) { 
       Intent myIntent = new Intent(view.getContext(), aactivity4.class); 
       startActivityForResult(myIntent,0); 
      } 
     } 
    }); 

回答

2

santoash庫馬爾答案是正確的。如果你在R.anim.layout資源上掙扎,那麼當你點擊listview項目時,動畫代碼就像其他聊天應用程序如何工作(幻燈片動畫)。

添加這些到您的動畫資源

R.anim.push_left_in

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <translate android:fromXDelta="100%" android:toXDelta="0" android:duration="@android:integer/config_mediumAnimTime"/> 
</set> 

R.anim.push_left_out

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <translate android:fromXDelta="0" android:toXDelta="-20%" android:duration="@android:integer/config_mediumAnimTime"/> 
</set> 

R.anim.push_right_in

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
<translate android:fromXDelta="-20%" android:toXDelta="0" android:duration="@android:integer/config_mediumAnimTime"/> 
    </set> 

R.anim.push_right _OUT

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <translate android:fromXDelta="0" android:toXDelta="100%" android:duration="@android:integer/config_mediumAnimTime"/> 
</set> 

在活動1,否則你會想執行這個動畫,而它推出的任何活動,添加此代碼的設置後setContentView()

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out); 
    //rest of your code include setContentView(); 
} 

現在,當您嘗試導航,你會發現一個問題回到你的listView活動,或者按下後退按鈕,或者點擊代表後退按鈕的視圖,動畫仍然是默認動畫,所以當你嘗試結束你當前的活動時這樣做。

finish(); 
overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out); 

對於後退按鈕按下使用此代碼。

@Override 
    public void onBackPressed() { 
     super.onBackPressed(); 
     overridePendingTransition(R.anim.push_right_in,R.anim.push_right_out); 
    } 

如果動畫我提供了不適合你的願望,你可以嘗試從這個link使自己的自定義動畫動畫。

+0

我曾試過,但它沒有任何變化 –

+0

@WAQARALAM不可能請顯示您的代碼。 –

+0

意圖intent = null; //全局字符串到類 String selectedValue = String.valueOf(parent.getItemAtPosition(position));如果(selectedValue.equals(「item1」)){ Intent myIntent = new Intent(view.getContext(),activity1.class); startActivityForResult(myIntent,0); overridePendingTransition(R.anim.slide_in_left,R.anim.slide_out_left); –

0
startActivityForResult(myIntent,0); 
overridePendingTransition(R.anim.hold, R.anim.fade_in); 

保持和淡入將動畫個XML

相關問題