Whatsapp 喜欢折叠工具栏

2024-03-04

在我的应用程序中,我想实现 Whatsapp 主页,如可折叠工具栏。也就是说,向下滚动列表时,工具栏应向上移动,选项卡应固定在顶部。我怎样才能实现这个目标?

这是我的应用栏布局

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/appbarLayout"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.design.widget.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/collapsibleToolBarLayout"
        android:fitsSystemWindows="true"
        app:titleEnabled="false"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">                 

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="@color/white"
            app:tabTextColor="@color/product_page_btn_grey"
            app:tabSelectedTextColor="@color/upcomer_background_red"
            app:tabIndicatorColor="@color/upcomer_background_red"
            android:layout_gravity="bottom"
            app:layout_scrollFlags="scroll|enterAlways"
            app:tabContentStart="72dp" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="parallax"
            app:layout_collapseParallaxMultiplier="0.9"
            android:background="@color/upcomer_background_red"
            app:popupTheme="@style/AppTheme.PopupOverlay" >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/title_image_view"
                android:contentDescription="@null"
                android:layout_gravity="start|center_vertical"
                android:visibility="gone"
                android:src="@drawable/title"/>
    
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/title_layout"
                android:visibility="gone">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:id="@+id/toolbarText"
                    style="@android:style/TextAppearance.DeviceDefault.Widget.ActionBar.Title"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/send_button"
                    android:layout_marginRight="10dp"
                    android:layout_marginEnd="10dp"
                    android:text="@string/send_text"
                    android:textColor="@color/white"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    android:visibility="gone"
                    android:layout_centerVertical="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true"/>

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

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

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

Now the TabLayout不可见并且Toolbar即使列表中的列表仍保留在那里ViewPager下面是滚动的。请帮忙。


为了实现此功能,实际上不需要 CollapsingToolbarLayout,您只需折叠设置为 ActionBar 的 Toolbar 即可。

下面是使用工具栏作为将折叠的 ActionBar 以及带有 ViewPager 的 TabLayout 的示例代码。

首先确保 MainActivity 使用的样式没有 ActionBar,例如:

<style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
    <!-- ....... -->
</style>

MainActivity.java,它具有 FragmentPagerAdapter 并设置选项卡:

public class MainActivity extends AppCompatActivity {

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        // Get the ViewPager and set it's PagerAdapter so that it can display items
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        PagerAdapter pagerAdapter =
                new PagerAdapter(getSupportFragmentManager(), MainActivity.this);
        viewPager.setAdapter(pagerAdapter);

        // Give the TabLayout the ViewPager
        TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.setupWithViewPager(viewPager);

        // Iterate over all tabs and set the custom view
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            tab.setCustomView(pagerAdapter.getTabView(i));
        }

    }


    @Override
    public void onResume() {
        super.onResume();
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    class PagerAdapter extends FragmentPagerAdapter {

        String tabTitles[] = new String[] { "Tab One", "Tab Two", "Tab Three" };
        Context context;

        public PagerAdapter(FragmentManager fm, Context context) {
            super(fm);
            this.context = context;
        }

        @Override
        public int getCount() {
            return tabTitles.length;
        }

        @Override
        public Fragment getItem(int position) {

            switch (position) {
                case 0:
                    return new BlankFragment();
                case 1:
                    return new BlankFragment();
                case 2:
                    return new BlankFragment();
            }

            return null;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            // Generate title based on item position
            return tabTitles[position];
        }

        public View getTabView(int position) {
            View tab = LayoutInflater.from(MainActivity.this).inflate(R.layout.custom_tab, null);
            TextView tv = (TextView) tab.findViewById(R.id.custom_text);
            tv.setText(tabTitles[position]);
            return tab;
        }

    }
}

活动主文件

重要部分:

  • 使用 CoordinatorLayout
  • Use app:layout_scrollFlags="scroll|enterAlways|snap"在工具栏中 特性
  • Use app:layout_behavior="@string/appbar_scrolling_view_behavior"在 ViewPager 属性

这里是活动主文件 file:

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_layout"
    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"
    tools:context=".MainActivity">


    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:elevation="6dp">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:elevation="0dp"
        app:layout_scrollFlags="scroll|enterAlways|snap"
        />

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        app:tabMode="fixed"
        android:layout_below="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        app:elevation="0dp"
        app:tabTextColor="#d3d3d3"
        app:tabSelectedTextColor="#ffffff"
        app:tabIndicatorColor="#ff00ff"
        android:minHeight="?attr/actionBarSize"
        />

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

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_below="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        />

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

自定义选项卡.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/custom_text"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="?attr/selectableItemBackground"
        android:gravity="center"
        android:textSize="16dip"
        android:textColor="#ffffff"
        android:singleLine="true"
        />
</LinearLayout>

空白片段.java,这只是添加足够的项目以使其滚动:

import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;;

public class BlankFragment extends Fragment {

    public BlankFragment() {
        // Required empty public constructor
    }

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

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_blank, container, false);

        RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.rv_recycler_view);
        rv.setHasFixedSize(true);
        MyAdapter adapter = new MyAdapter(new String[]{"test one", "test two", "test three", "test four", "test five" , "test six" , "test seven", "test eight" , "test nine"});
        rv.setAdapter(adapter);

        LinearLayoutManager llm = new LinearLayoutManager(getActivity());
        rv.setLayoutManager(llm);

        return rootView;
    }

}

片段_空白.xml,使用 RecyclerView 或任何其他支持嵌套滚动的视图(例如NestedScrollView http://developer.android.com/reference/android/support/v4/widget/NestedScrollView.html

(旁注:您可以致电setNestedScrollingEnabled(true) http://developer.android.com/reference/android/support/v4/widget/NestedScrollView.html在 api-21 及更高版本上使其与 ListView 一起使用):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.SearchView
        android:id="@+id/sv_search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Search!"
        android:singleLine="true"
        android:inputType="textNoSuggestions"
        android:layout_gravity="start"
        android:layout_marginRight="18dp"
        android:ems="10" >
    </android.support.v7.widget.SearchView>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_recycler_view"
        android:layout_below="@+id/sv_search"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </android.support.v7.widget.RecyclerView>
</RelativeLayout>

MyAdapter.java,RecyclerView适配器:

import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    private String[] mDataset;

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class MyViewHolder extends RecyclerView.ViewHolder {
        public CardView mCardView;
        public TextView mTextView;
        public MyViewHolder(View v) {
            super(v);

            mCardView = (CardView) v.findViewById(R.id.card_view);
            mTextView = (TextView) v.findViewById(R.id.tv_text);
        }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public MyAdapter(String[] myDataset) {
        mDataset = myDataset;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
                                                     int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.card_item, parent, false);
        // set the view's size, margins, paddings and layout parameters
        MyViewHolder vh = new MyViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.mTextView.setText(mDataset[position]);
    }

    @Override
    public int getItemCount() {
        return mDataset.length;
    }
}

卡_项目.xml,圆形图像和“blah blah blah”字符串都是静态内容,只有tv_text对于这个简单的示例,TextView 是从数据源更新的:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="68dp" >

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_margin="10dp"
        android:layout_height="62dp"
        card_view:cardCornerRadius="4dp"
        card_view:elevation="14dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <ImageView
            android:id="@+id/iv_image"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:src="@drawable/abc_btn_radio_material">
        </ImageView>

        <TextView
            android:id="@+id/tv_text"

            android:layout_toRightOf ="@+id/iv_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center" >
        </TextView>

            <TextView
                android:id="@+id/tv_blah"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="blah blah blah......"
                android:layout_below="@+id/tv_text"
                android:layout_toRightOf="@+id/iv_image"
                android:layout_toEndOf="@+id/iv_image">
            </TextView>

        </RelativeLayout>
    </android.support.v7.widget.CardView>

</RelativeLayout>

构建.gradle依赖项:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile 'com.android.support:design:23.0.1'
    compile 'com.android.support:cardview-v7:23.0.1'
    compile 'com.android.support:recyclerview-v7:23.0.1'
}

Result:

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Whatsapp 喜欢折叠工具栏 的相关文章

  • Android 如何使用意图发送文本和图像或任何对象?

    我知道可以与以下人员分享短信ACTION SEND通过指定Intent EXTRA TEXT 同样的方法适用于图像 Intent EXTRA STREAM 但是如何将文本和图像添加到同一意图呢 您可以通过意图发送文本和图像 例如 如果您要发
  • 可以挂载未加密的 obb 但出现加密错误 21

    这与 kitkat bug 无关 我正在 4 4 2 中测试 我可以毫无问题地挂载 obb 文件 问题是当尝试对加密的 obb 执行相同操作时 我在 Windows 中使用 jobb 如下 jobb d my folder o exp ob
  • 通过存储访问框架 (SAF) 启用显示/隐藏 SD 卡的额外功能

    我正在使用存储访问框架 SAF Intent intent new Intent Intent ACTION OPEN DOCUMENT intent addCategory Intent CATEGORY OPENABLE intent
  • 自定义 ListView 和 onclick

    这是我的代码 一切 我按照你说的做了 但我仍然无法点击任何内容 我的意思是我可以点击但没有任何反应 package fixus core import java util ArrayList import java util Iterato
  • 在 Android 中使用 PhoneGap 打开 PDF

    我需要打开一个位于 url 中的 PDF 文件 我需要用 PDF 查看器打开它 有可能的 谢谢大家 此致 我建议使用儿童浏览器插件 https build phonegap com blog childbrowser plugin并使用 G
  • 如何让精灵对 cocos2d android 中的触摸做出反应?

    我有 1 支枪 当点击屏幕上的任何一点时 子弹都会发射 但根据我的要求 有 3 支枪 精灵 当触摸任何精灵时 子弹必须发射 当谷歌搜索时 我知道这可以是通过使用targetedTouchDelegate或循环所有的精灵并为每个触摸的精灵设置
  • Android 线程和处理程序不工作

    我最近重构了一个旧项目 发现无论我做什么 特定部分都不再需要正常运行 本质上 我有一个带有 TextView 的 Activity 该视图按时间间隔从同一类中调用的线程更新 暂停是通过 Thread sleep 完成的 并且使用 Handl
  • 已将 APK 上传到 Play 商店,获得 0 个受支持的设备,但没有错误

    我正在尝试将我的应用程序的 Alpha 版本发布到 Play 商店 但我得到了 0 个受支持的设备 这令人沮丧 因为我没有看到该错误 该项目是一个新的样式 gradle API声明和版本在build gradle中定义 您可以在此处获取该应
  • 使用 android-async-http (loopj) 发布 JSON/XML

    我在用android async http http loopj com android async http 并且真的很喜欢它 我在发布数据时遇到了问题 我必须按照以下格式将数据发布到 API
  • 如何解决Android中的NullPointerException错误?

    下面的代码在 Eclipse 的模拟器中运行顺利 但在 Android 手机和平板电脑上运行时出现问题 public class RingerActivity extends Activity Called when the activit
  • Fabric Beta 和 APK 拆分

    我根据 ABI 而不是密度来拆分我的应用程序 如下所示 splits abi enable true reset include x86 armeabi armeabi v7a mips arm64 v8a universalApk tru
  • Picasso onBitmapLoaded 从未调用过

    我遇到了同样的问题 我想使用毕加索生成的可绘制对象进行图像缓存 但我无法得到相同的结果 这是我用来访问位图可绘制对象的代码 Target targetBitmap new Target Override public void onPrep
  • Android MapView v2 黑屏

    我一直在尝试实现 android 的 MapView v2 除了这个错误之外 我让它工作得很好 This is what it looks like when I load the app from scratch 如您所见 没有任何问题
  • AWS Cognito-获取带有ID的用户信息

    有没有什么方法可以获取 AWS Cognito 池 在 Android 上 中未登录的用户的信息 并知道他的 ID 我尝试了该代码 AppHelper getPool getUser username getDetailsInBackgro
  • 无法将图像从 url 存储到 SD 卡

    我想将图像存储在 mnt sdcard 中 package com Downld file frm net import java io BufferedInputStream import java io File import java
  • Web 服务凭证 - OpenID/Android AccountManager?

    我正在构建一个网络服务 并想使用用户的谷歌帐户凭据 该服务在 GAE 上运行 并将有一个 Web 客户端和一个 Android 本机客户端 这是我第一次尝试类似的事情 我一直在阅读有关 OpenID 和 Android AccountMan
  • 如何使用游戏循环每五秒在屏幕上出现和消失一个对象

    我正在尝试学习 Android 游戏开发 首先 我尝试每五秒使用游戏循环在屏幕上出现和消失一个对象 但我没有成功 我读过不同的教程和论坛 我按照教程中的方式应用了所有内容 但对象仍然在连续绘制 它并没有消失 我没有得到我所缺少的东西 请指导
  • Android 位图到 WebRtc I420 帧损坏

    尝试通过 WebRtc 流式传输位图 我的 Capturer 类大约如下所示 public class BitmapCapturer implements VideoCapturer VideoSink private Capturer c
  • MediaPlayer() 音频口吃(android)

    我正在我的 Android 应用程序中使用 MediaPlayer 函数从远程服务器传输实时音频流 但音频断断续续且断断续续 问题不在于我的互联网 因为当我在计算机上播放时 提要播放得很好 可能是什么问题 注意 直播正在进行中 这是我正在使
  • 当 SystemUI 在 Android Boot 中加载时

    如何知道系统 UI 何时加载Android启动过程 状态栏和导航栏视图到底绘制在哪里 作为SystemUI是一个特权应用程序 那么它是否在启动器应用程序 主屏幕 启动之前加载 我不知道 欢迎任何建议 下面是一系列简要解释的步骤 希望能够阐明

随机推荐