用于更改android上每个片段滑动的操作栏标题的动画

时间:2017-07-25 13:13:59

标签: android android-fragments animation android-viewpager

我使用工具栏小部件和片段设置了一个带有滑动(ViewPager)标签的自定义操作栏。

我希望每个标签都有不同的操作栏“标题”。如果我可以在滑动时设置动画,那也很棒。

我很感激任何建议和教程链接!在此先感谢:)

MainActivity.Java

public class MainActivity extends FragmentActivity implements Json.Listener {


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

    Toolbar topToolBar = (Toolbar) findViewById(R.id.toolbar);
    topToolBar.setTitle("Weather");
    topToolBar.inflateMenu(R.menu.menu_home);
    topToolBar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement
            if (id == R.id.add_city) {
                try {
                    AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
                            .setTypeFilter(AutocompleteFilter.TYPE_FILTER_CITIES)
                            .build();
                    Intent intent =
                            new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN)
                                    .setFilter(typeFilter)
                                    .build(MainActivity.this);
                    startActivityForResult(intent, 1);
                } catch (GooglePlayServicesRepairableException e) {
                    // TODO: Handle the error.
                } catch (GooglePlayServicesNotAvailableException e) {
                    // TODO: Handle the error.
                }
                return true;
            }

            return false;
        }
    });


    ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
    pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));

    String city_name = (PreferenceManager.getDefaultSharedPreferences(this).getString("name", ""));;
    callAPI(city_name);

}

@Override
public void onLoaded(Response androidList) {
    new DBHandler(this).insertDetailsDB(androidList);
}

@Override
public void onError() {
    Toast.makeText(this, "Error !", Toast.LENGTH_SHORT).show();
}

private class MyPagerAdapter extends FragmentPagerAdapter {

    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int pos) {
        switch (pos) {

            case 0:
                return new CityListFragment();
            case 1:
                return new WeatherDeatailsFragment();
            default:
                return new WeatherDeatailsFragment();
        }
    }

    @Override
    public int getCount() {
        return 2;
    }
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/back"
tools:context="in.edu.apoorv.weather.MainActivity">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:minHeight="?attr/actionBarSize"
    android:background="@android:color/transparent"
    android:layout_width="match_parent"
    android:titleTextColor="#fff"
    android:layout_height="60dp">
</android.support.v7.widget.Toolbar>


<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/viewPager"
    android:layout_marginTop="60dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

3 个答案:

答案 0 :(得分:2)

您可以执行以下操作

1.在TextView内部Toolbar包含此内容

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:background="@android:color/transparent"
android:layout_width="match_parent"
android:titleTextColor="#fff"
android:layout_height="60dp">

    <TextView
    style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
    android:id="@+id/toolbar_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />      

</android.support.v7.widget.Toolbar>

2.获取活动中的视图

TextView titleTv = (TextView) findViewById(R.id.toolbar_title);

3.在res/anim文件夹中创建动画xml文件。

<强> slide.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >

<scale
  android:duration="500"
  android:fromXScale="1.0"
  android:fromYScale="1.0"
  android:interpolator="@android:anim/linear_interpolator"
  android:toXScale="1.0"
  android:toYScale="0.0" />
</set>

4.将动画初始化为TextView。

Animation animation1 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide);

5.将OnPageChangeListener设置为ViewPager。在侦听器的onPageSelected()方法中调用动画。

viewPager.setOnPageChangeListener(new OnPageChangeListener() {
    public void onPageScrollStateChanged(int state) {}
    public void onPageScrolled(int position, float positionOffset, int 
positionOffsetPixels) {}

    public void onPageSelected(int position) {

         titleTv.startAnimation(animation1);

         switch(position){
         case 0:
            titleTv.setText("Title1");//set your title here
            break;

         case 1:
            titleTv.setText("Title2");//set your title here
            break;
        }
    }
});

我希望这有效。 :)

答案 1 :(得分:0)

Here是一个动画视图库。也许你想用它来制作动画片标题。

答案 2 :(得分:0)

您可以将此方法添加到 FragmentPagerAdapter 类中。 这是为了允许 TabLayout 中的标题在滑动期间根据不同的标签进行更改。希望这会有所帮助=)

void sort_array(unsigned int a[], int start, int end){
  if(start < end){
    int pos_piv = partition(a, start, end);
    sort_array(a, start, pos_piv-1);
    sort_array(a, pos_piv+1, end);
  }
}