ViewPager更改页面

时间:2016-04-06 00:54:09

标签: android android-viewpager android-pagetransformer

我已经考虑了一段时间,但我似乎无法弄明白。我有一个简单的应用退出3个标签(TabLayout + ViewPager),我想在滑动标签时更改ToolBar的颜色。我想要实现的效果是在用户滑动到下一个或上一个选项卡时动态更改颜色。

Check video here

过去,我通过为每个子视图指定颜色并使用ViewPager来修改PageTransformer子视图,从而改变了ToolBar子视图的背景。 alpha组件工作正常,但ViewPager不是ToolBar的孩子,因此我无法为每个页面设置颜色,因为只有# iptables-save # Generated by iptables-save v1.4.21 on Tue Apr 5 12:52:32 2016 *nat :PREROUTING ACCEPT [1345:188285] :INPUT ACCEPT [1332:187243] :OUTPUT ACCEPT [24:1510] :POSTROUTING ACCEPT [24:1510] :DOCKER - [0:0] -A PREROUTING -m addrtype --dst-type LOCAL -j DOCKER -A OUTPUT ! -d 127.0.0.0/8 -m addrtype --dst-type LOCAL -j DOCKER -A POSTROUTING -s 172.17.0.0/16 ! -o docker0 -j MASQUERADE -A DOCKER -i docker0 -j RETURN COMMIT # Completed on Tue Apr 5 12:52:32 2016 # Generated by iptables-save v1.4.21 on Tue Apr 5 12:52:32 2016 *filter :INPUT ACCEPT [10563:913002] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [745:59756] :DOCKER - [0:0] :DOCKER-ISOLATION - [0:0] -A FORWARD -j DOCKER-ISOLATION -A FORWARD -o docker0 -j DOCKER -A FORWARD -o docker0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT -A FORWARD -i docker0 ! -o docker0 -j ACCEPT -A FORWARD -i docker0 -o docker0 -j ACCEPT -A DOCKER-ISOLATION -j RETURN COMMIT # Completed on Tue Apr 5 12:52:32 2016 Row Athlete Event Mark Meet 1 1 3 10 A 2 2 2 5 A 3 3 3 3 A 4 4 4 7 A 5 2 2 4 A 6 3 2 5 B 7 1 1 10 C } 每时每刻。

关于如何实现这一点的任何想法?

2 个答案:

答案 0 :(得分:3)

在这种情况下,您应该将onPageChangeListener添加到ViewPager,然后单独处理每个更改。

public class MyActivity extends AppCompatActivity {
private Toolbar toolbar;
private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.oncreate(savedInstanceState);

    //Initialize your views

    viewPager.addOnPageChangeListener(new OnPageChangeListener() {
    //Override default methods and change toolbar color inside of here
    });
  }
}

答案 1 :(得分:0)

你可以借助于一个接口和片段内的setUserVisibleHint(boolean isVisibleToUser)来做到这一点。

<强> colorChangeInterface:

public interface ColorChangeInterface {
    void setToolbarAndTabColor(int position);
}

<强> MainActivity:

public class MainActivity extends AppCompatActivity implements ColorChangeInterface
{
public void setToolbarAndTabColor(int position)
{
     if(position==1)
        {
            toolbar.setBackgroundColor(ContextCompat.getColor(getApplicationContext(),R.color.classColor4));
            tabLayout.setBackgroundColor(ContextCompat.getColor(getApplicationContext(),R.color.classColor4));
            Log.d("colorchange",String.valueOf(position));
        }
        else if(position ==2)
        {
            toolbar.setBackgroundColor(ContextCompat.getColor(getApplicationContext(),R.color.classColor2));
            tabLayout.setBackgroundColor(ContextCompat.getColor(getApplicationContext(),R.color.classColor2));
            Log.d("colorchange", String.valueOf(position));


        }

}

<强> FragmentOne:

使您的片段使用默认覆盖方法setUserVisibleHint。 这是为了检查当前片段在ViewPager中是否可见,如果Visible使用Interface更改工具栏和Tabbar的颜色。

public class FragmentOne extends Fragment {
 @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
       if (isVisibleToUser)
       {
          ColorChangeInterface colorChangeInterface = (ColorChangeInterface) getActivity();
          colorChangeInterface.setToolbarAndPagerColor(1);
           Log.d("TAG","Current Fragment is visible");
        }
    }
}

<强> FragmentTwo:

 public class FragmentOne extends Fragment {
     @Override
        public void setUserVisibleHint(boolean isVisibleToUser) {
            super.setUserVisibleHint(isVisibleToUser);
           if (isVisibleToUser)
           {
              ColorChangeInterface colorChangeInterface = (ColorChangeInterface) getActivity();
              colorChangeInterface.setToolbarAndPagerColor(2);
               Log.d("TAG","Current Fragment is visible");
            }
        }
    }

如果您只想在每次滑动中更改工具栏颜色。

public class FragmentOne extends Fragment {
Toolbar toolbar;
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);
 toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
       }
         @Override
         public void setUserVisibleHint(boolean isVisibleToUser) {
                super.setUserVisibleHint(isVisibleToUser);
               if (isVisibleToUser)
               {
                 toolbar.setBackgroundColor(ContextCompat.getColor(getApplicationContext(),R.color.classColor2));  
             }
            }
        }