ListView项目扩展到WebViews - 过渡动画

时间:2016-12-17 10:11:40

标签: listview android-webview android-animation android-transitions android-adapterview

我有一个自定义ListView,每个项目都打开本地HTML文件(WebView)。 当我单击ListView项目到WebView时,我很难为过渡添加动画。我尝试使用ActivityOptions.makeSceneTransitionAnimation()方法中的startActivity()方法,如下所示: -

MainActivity.java

public class MainActivity extends AppCompatActivity {

ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView = (ListView) findViewById(R.id.list);


    String[] values = new String[] { "LINK 1",
            "LINK 2", "LINK 3"
    };

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1, values) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
            if (position==0||position==1||position==2) {
                view.setBackgroundColor(Color.parseColor("#BBDEFB"));
            } else {
                view.setBackgroundColor(Color.parseColor("#006064"));
            }
            return view;
        }
    };

    listView.setAdapter(adapter);
    //final HashMap<String, Integer> hashMap = new HashMap<String, Integer> ();



    **listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            // TODO Auto-generated method stub
            if (position == 0) {
                Intent myIntent = new Intent(getApplicationContext(),
                        WebViewActivity.class);
                myIntent.putExtra("key", 0);
                startActivity(myIntent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
            }else if (position == 1) {
                Intent myIntent = new Intent(getApplicationContext(),
                        WebViewActivity.class);
                myIntent.putExtra("key", 1);
                startActivity(myIntent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle()   );
            }else if (position == 2) {
                Intent myIntent = new Intent(getApplicationContext(),
                        WebViewActivity.class);
                myIntent.putExtra("key", 2);
                startActivity(myIntent);
            }**



        }

    });

但是我收到以下错误:

Error:(69, 60) error: no suitable method found for makeSceneTransitionAnimation(<anonymous OnItemClickListener>)
method ActivityOptions.makeSceneTransitionAnimation(Activity,View,String) is not applicable
(actual and formal argument lists differ in length)
method ActivityOptions.makeSceneTransitionAnimation(Activity,Pair<View,String>...) is not applicable
(actual argument <anonymous OnItemClickListener> cannot be converted to Activity by method invocation conversion)

我的完整代码:

WebViewActivity.java

public class WebViewActivity extends AppCompatActivity {
WebView webView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web_view);

    webView = (WebView) findViewById(R.id.webView1);
    //webView.setWebViewClient(new myWebClient());
    webView.getSettings().setJavaScriptEnabled(true);
    int pos = getIntent().getIntExtra("key", 0);
    if (pos == 0) {
        webView.loadUrl("file:///android_asset/index.html");
    } else if (pos == 1) {
        webView.loadUrl("file:///android_asset/index2.html");
    } else if (pos == 2) {
        webView.loadUrl("file:///android_asset/index3.html");
    }

}


}

activity_main.xml中

`

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.a405146.listview.MainActivity">

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/list"
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true"
        android:background="?android:attr/selectableItemBackground"/>
</RelativeLayout>

`

activity_web_view.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.a405146.listview.WebViewActivity">

<WebView
    android:id="@+id/webView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />

</RelativeLayout>

请帮助我了解我出错的地方以及如何正确实施过渡。

1 个答案:

答案 0 :(得分:0)

您需要为listView中的所有视图提供唯一的transitionName。您可以在调用getView时在适配器中执行此操作,调用setTransitionName并设置唯一名称,例如字符串的某种组合以及每个项目唯一的当前位置。在getView方法

中写这样的东西
ViewCompat.setTransitionName(convertView, "list_item_"+position);

在下一个活动中,也为WebView提供一个唯一的transitionName,例如&#34; sharedWebView&#34;。现在,当您开始下一个活动时,为视图创建一个Pair对象,并在下一个活动中创建该视图的名称。像这样的东西

Pair<View, String> pair = Pair.create(view, "sharedWebView");

并在启动活动时传递它,并且您在此处传递对内部类的引用,但您必须传递活动的引用。像这样的东西

startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(MainActivity.this, pair).toBundle());