滚动选项卡中的片段时隐藏/显示工具栏

时间:2015-08-18 10:12:12

标签: android android-fragments webview toolbar android-tablayout

我在我的Android应用中添加了新的ToolbarTablayoutViewpager。我为我的3个标签提供了碎片,它的工作正常。但问题是,当我向上滚动我的工具栏时,不会隐藏。我希望当我滚动我的片段时它应该隐藏。还有一件事,我在片段中使用Webview。 我的代码如下。

MainActivity.Java

public class MainActivity extends AppCompatActivity {
    TabLayout tabLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

        setupToolbar();
        setupTablayout();
    }

private void setupToolbar() {
    // TODO Auto-generated method stub
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarsdfs);
    if (toolbar != null) {
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);}
}

private void setupTablayout() {
    // TODO Auto-generated method stub
    tabLayout = (TabLayout) findViewById(R.id.tabLayout);
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
    tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    final PagerAdapter adapter = new PagerAdapter
            (getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });
}
}

main.xml中

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinatorLayout"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbarsdfs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimaryDark"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    app:layout_scrollFlags="scroll|enterAlways"/>

<android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            android:scrollbars="horizontal"
            android:layout_below="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary" />

<android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/tablayout"/>

</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

PagerAdapter.Java

public class PagerAdapter extends FragmentStatePagerAdapter {
    int mNumOfTabs;

    public PagerAdapter(FragmentManager fm, int NumOfTabs) {
        super(fm);
        this.mNumOfTabs = NumOfTabs;
    }

    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                Fragment_Feeds tab1 = new Fragment_Feeds();
                return tab1;
            case 1:
                Fragment_Facts tab2 = new Fragment_Facts();
                return tab2;
            default:
                return null;
        }
    }

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

Fragment_Feeds.java

public class Fragment_Feeds extends Fragment {
    SwipeRefreshLayout swipeView;
    WebView myWebView;
    ProgressBar progressBar;
    final static String myBlogAddr = "http://myblog.com";
    String myUrl;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragmentfeeds, container, false);
        swipeView = (SwipeRefreshLayout) view.findViewById(R.id.swipe);

        myWebView = (WebView) view.findViewById(R.id.webview);
        progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
        myWebView.setWebViewClient(new MyWebViewClient()); 
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        myWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        myWebView.setHorizontalScrollBarEnabled(false);
        myWebView.loadUrl("http://myblog.com");
        swipeView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener()    
        {
            @Override
            public void onRefresh() 
            {
                  myWebView.loadUrl("http://myblog.com");

            }});
        return view;
    }
    private class MyWebViewClient extends WebViewClient {
        @Override
        public void onPageFinished(WebView view, String url) {
            swipeView.setRefreshing(false);
        }
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
         myUrl = url;
            view.loadUrl(url);
            return true;
        }
        @Override
        public void onReceivedError(WebView view, int errorCod,String description, String failingUrl) {
            myWebView.loadUrl("file:///android_asset/error_page.html");
        }
    }
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.main, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }
}

fragmentfeeds.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#FFFFFF" >
    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <android.support.v4.widget.SwipeRefreshLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/swipe"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="#FFFFFF">s

    <WebView
      android:id="@+id/webview"
      android:layout_width="match_parent"
      android:layout_height="fill_parent"
      android:numColumns="1"
      android:scrollbars="none"
      android:focusableInTouchMode="false"
      android:focusable="false"
      android:background="#FFFFFF" />
   </android.support.v4.widget.SwipeRefreshLayout>

</LinearLayout>

我想要什么?

我想要的是,当我向上滚动webview时,工具栏也应该向上滚动并隐藏,当我向下滚动工具栏时,应该尽快回来。

5 个答案:

答案 0 :(得分:5)

首先,您应该将您的webview移动到NestedScrollView并设置&#39; android:isScrollContainer&#39;属于&#39; false&#39;值。 https://developer.android.com/reference/android/support/v4/widget/NestedScrollView.html

其次,您应该将ViewPager移到AppBarLayout之外。 所以,你的main.xml应该是这样的:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout   
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:background="#FFF"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <!-- your app bar stuff here -->
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="240dp"
        app:layout_scrollFlags="scroll|enterAlways">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitXY"
            android:id="@+id/image_header"/>

        <include layout="@layout/header"/>

    </FrameLayout>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:background="#FFF"
        app:tabTextColor="#a3a3a3"
        app:tabSelectedTextColor="#a3a3a3"
        app:tabIndicatorColor="#0042ab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="gone"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:background="#eaeaea"/>

AppBarLayout childern只有在有标志app时才会隐藏:layout_scrollFlags

在此处阅读有关实施支持设计库的更多信息:
https://android-developers.googleblog.com/2015/05/android-design-support-library.html

答案 1 :(得分:3)

您应该将ViewPager移到AppBarLayout之外,并将行为添加到ViewPager app:layout_behavior="@string/appbar_scrolling_view_behavior"

我写了一个完整的例子! https://github.com/erikcaffrey/AndroidDesignSupportLibrary

希望对你有帮助!

答案 2 :(得分:1)

只需在工具栏或FAB按钮中添加此行。

    class Bullets(pygame.sprite.Sprite):
        def __init__(self,x,y):
            pygame.sprite.Sprite.__init__(self)

            self.image = pygame.image.load("Images/bullet.png")
            self.rect = self.image.get_rect()
            self.rect.x = x
            self.rect.y = y

        def update(self):
            self.rect.y += 4

            if self.rect.y > 520:
                BulletList.empty()

    def main3():
        moveX = 0
        TrapList.empty()

        player.rect.x,player.rect.y = 50,0
        BulletList.add(bullet)
        FiringBullet = pygame.USEREVENT + 1
        pygame.time.set_timer(FiringBullet, 3000)

        GameExit = False

        while GameExit==False:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    quit()

                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_LEFT:
                        player.move(-player.speed,0)
                    if event.key == pygame.K_RIGHT:
                        player.move(player.speed,0)
                    if event.key == pygame.K_UP:
                        player.move(0,-10)

                if event.type == pygame.KEYUP:
                    if event.key == pygame.K_LEFT:
                        player.move(player.speed,0)
                    if event.key == pygame.K_RIGHT:
                        player.move(-player.speed,0)
                    if event.key == pygame.K_UP:
                        player.move(0,0)

                if event.type == FiringBullet:
                    BulletList.add(bullet)
            print(BulletList)

            screen.fill(BLACK)
            level3.update()

            if player.rect.x > 350:
                for eachbullet in BulletList:
                    BulletList.draw(screen)
                    BulletList.update()


            playergroup.update()
            playergroup.draw(screen)

            player.Level3PlatColl(BlockListDirt2)
            pygame.display.update()
            clock.tick(60)
        bullet = Bullets(400,200)
        BulletList = pygame.sprite.Group()
main3()

当用户向下滚动列表时,app:layout_scrollFlags="scroll|enterAlways" 行将导致我们app:layout_scrollFlags="scroll|enterAlways"将滚动屏幕,一旦他开始向上滚动,Toolbar将再次显示。干净简单,这是Toolbar的力量!

请查看此链接 https://mzgreen.github.io/2015/06/23/How-to-hideshow-Toolbar-when-list-is-scrolling(part3)/&re=1&ts=1453224208&sig=ALL1Aj5aKrtAlylt-Qt3Ci6uwff76BqQYw

答案 3 :(得分:0)

隐藏特定片段的菜单:

compile 'com.parse:parse-android:1.12

答案 4 :(得分:0)

只需将app:layout_scrollFlags="scroll|enterAlways"放到工具栏即可。