【问题标题】:Collapsible Toolbar - Make Fragment Footer Always Visible in Android可折叠工具栏 - 使片段页脚在 Android 中始终可见
【发布时间】:2017-08-25 16:48:03
【问题描述】:

我正在制作一个包含三个片段的 ProfilePage 的应用程序 - 关于 |帖子 |图库,我正在使用带有用户图像的可折叠工具栏。我的第二个和第三个片段将有一个应该始终可见的页脚,但这就是我得到的:

当我的图片展开时,页脚会消失。我想要的是我的两个片段让这些页脚始终可见,而不取决于工具栏是否折叠。我的画廊页脚应该类似于帖子页脚。

profile_layout.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: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="256dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        contentScrim="?attr/colorPrimary"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <ImageView
            android:id="@+id/profile_image"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            android:scaleType="centerCrop"
            android:contentDescription="@string/profile_photo"
            app:layout_collapseMode="none"/>

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin">

            <ImageView
                android:id="@+id/toolbarEdit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end"
                android:contentDescription="@string/block"
                android:paddingEnd="20dp"
                android:paddingStart="5dp"
                app:srcCompat="@drawable/ic_icon_edit" />
        </android.support.v7.widget.Toolbar>

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.design.widget.TabLayout
        android:id="@+id/myProfileTabs"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_gravity="bottom"
        app:tabContentStart="72dp"
        app:tabGravity="fill"
        app:tabMode="scrollable" />

    <android.support.v4.view.ViewPager
        android:id="@+id/myProfileViewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</LinearLayout>

posts_fragment.xml:

<LinearLayout 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:background="@drawable/tile_bg"
android:orientation="vertical" >

<ListView
    android:id="@+id/list_view_messages"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="@null"
    android:divider="@null"
    android:transcriptMode="alwaysScroll"
    android:stackFromBottom="true">
</ListView>

<LinearLayout
    android:id="@+id/llMsgCompose"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:orientation="horizontal"
    android:weightSum="4" >

    <EditText
        android:id="@+id/inputMsg"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="3"
        android:background="@color/bg_msg_input"
        android:textColor="@color/text_msg_input"
        android:paddingLeft="6dp"
        android:paddingRight="6dp"/>

    <Button
        android:id="@+id/btnSend"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/ppdColorOrange"
        android:textColor="@color/white"
        android:text="@string/send" />
</LinearLayout>

我的 PostsFragment(尚未实现):

public class MyProfilePostsFragment extends Fragment {     
    @Override     
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {          
        View view = inflater.inflate(R.layout.navdrawer_my_profile_fragment_posts, container, false);         
        return view;
    } 
}

【问题讨论】:

    标签: android xml android-fragments android-coordinatorlayout android-collapsingtoolbarlayout


    【解决方案1】:

    我对我的一个应用程序有相同的要求。我刚刚做了解决方法。
    在选项卡布局中添加所需的布局:

    <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: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="256dp"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar"
                android:layout_width="match_parent"
                contentScrim="?attr/colorPrimary"
                android:layout_height="match_parent"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">
    
                <ImageView
                    android:id="@+id/profile_image"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:fitsSystemWindows="true"
                    android:scaleType="centerCrop"
                    android:contentDescription="@string/profile_photo"
                    app:layout_collapseMode="none"/>
    
                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="pin">
    
                    <ImageView
                        android:id="@+id/toolbarEdit"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="end"
                        android:contentDescription="@string/block"
                        android:paddingEnd="20dp"
                        android:paddingStart="5dp"
                        app:srcCompat="@drawable/ic_icon_edit" />
                </android.support.v7.widget.Toolbar>
    
            </android.support.design.widget.CollapsingToolbarLayout>
        </android.support.design.widget.AppBarLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
            <android.support.design.widget.TabLayout
                android:id="@+id/myProfileTabs"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:layout_gravity="bottom"
                app:tabContentStart="72dp"
                app:tabGravity="fill"
                app:tabMode="scrollable" />
    
            <android.support.v4.view.ViewPager
                android:id="@+id/myProfileViewpager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />
        </LinearLayout>
    
        <-- Your required layout -->
        <LinearLayout
            android:id="@+id/tab123"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:gravity="bottom"
            android:orientation="horizontal"
            android:visibility="gone"
            android:weightSum="4">
    
            <EditText
                android:id="@+id/inputMsg"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="3"
                android:paddingLeft="6dp"
                android:paddingRight="6dp" />
    
            <Button
                android:id="@+id/btnSend"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="send" />
        </LinearLayout>
    </android.support.design.widget.CoordinatorLayout>
    

    在您的 Tab 活动中添加以下代码:

    TabLayout tabLayout;    
    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);//add your viewpager to TabLayout 
    
    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
    
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
            LinearLayout l=(LinearLayout) findViewById(R.id.tab123);
            final EditText text=(EditText) findViewById(R.id.inputMsg);
            Button send=(Button) findViewById(R.id.btnSend);
    
            if (tab.getPosition() == 0) {
                l.setVisibility(View.GONE);
                System.out.println("About tab");
            } else if (tab.getPosition() == 1) {
                l.setVisibility(View.VISIBLE);
                System.out.println("Posts tab");
    
                send.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String msg = null;
                        msg = text.getText().toString(); //get input
                        // Perform your desired task.
                    }
                });
            } else if (tab.getPosition() == 2) {
                l.setVisibility(View.VISIBLE);
                System.out.println("Gallery tab");
    
                send.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String msg = null;
                        msg = text.getText().toString(); //get input
                        //Perform your desired task.
                    }
                });
            }
        }
    
        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
    
        }
    
        @Override
        public void onTabReselected(TabLayout.Tab tab) {
    
        }
    });
    

    【讨论】:

    • 感谢您的回答 Shashidhar。我是否也必须将我的 gallery_fragment.xml 添加到选项卡布局中?这是否也意味着我必须更改我的片段并将此布局添加到所有片段中?有没有更清洁的解决方案?
    • @Kemo,只需从我们添加到选项卡 xml 文件中的所有片段中删除线性布局。
    • 试过了,效果很好。一个问题 - 我应该如何处理我的片段 - 我应该如何实现它,因为我也必须编辑它。当我输入消息并单击输入时,我将如何在我的片段或活动中显示它?我是否需要将它与具有这些片段的 MainActivity 结合起来。我正在使用这个tutoiral
    • 另外,如何在页脚上方显示 post_fragment.xml 中的列表视图?
    • @Kemo,编码快乐!
    猜你喜欢
    • 2016-10-01
    • 1970-01-01
    • 2015-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-24
    • 2018-06-24
    • 1970-01-01
    相关资源
    最近更新 更多