Android Studio 简单实现下滑(ListView)隐藏标题栏 上滑显示

Posted 彬sir哥

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android Studio 简单实现下滑(ListView)隐藏标题栏 上滑显示相关的知识,希望对你有一定的参考价值。

1.测试效果如下:

2.activity_main.xml,标题栏和列表,容易

<?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:orientation="vertical">

    <RelativeLayout
        android:id="@+id/id_title"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="#00ccff">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="Ace "
            android:textColor="#ffffff"
            android:textSize="22sp" />
    </RelativeLayout>

    <ListView
        android:id="@+id/id_lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
        
</RelativeLayout>

3.MainActivity.java,上滑,标题栏隐藏,下滑显示,这是动画的,比较容易,我暂时不说多

public class MainActivity extends Activity {
    private ListView mListView;
    private RelativeLayout mTitle;
    private int mTouchSlop;
    private SimpleAdapter mAdapter;
    private float mFirstY;
    private float mCurrentY;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        mTouchSlop = ViewConfiguration.get(this).getScaledTouchSlop();

        initViews();
        showHideTitleBar(true);
    }

    private void initViews() {
        mListView = (ListView) findViewById(R.id.id_lv);
        mTitle = (RelativeLayout) findViewById(R.id.id_title);

        mAdapter = new SimpleAdapter(this, getData(),
                R.layout.lv_item,
                new String[]{"info"},
                new int[]{R.id.num_info});
        mListView.setAdapter(mAdapter);
        mListView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        mFirstY = event.getY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        mCurrentY = event.getY();
                        if (mCurrentY - mFirstY > mTouchSlop) {
                            System.out.println("mtouchislop:" + mTouchSlop);
                            // 下滑 显示titleBar
                            showHideTitleBar(true);
                        } else if (mFirstY - mCurrentY > mTouchSlop) {
                            // 上滑 隐藏titleBar
                            showHideTitleBar(false);
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                }
                return false;
            }
        });
    }

    private Animator mAnimatorTitle;
    private Animator mAnimatorContent;

    private void showHideTitleBar(boolean tag) {
        if (mAnimatorTitle != null && mAnimatorTitle.isRunning()) {
            mAnimatorTitle.cancel();
        }
        if (mAnimatorContent != null && mAnimatorContent.isRunning()) {
            mAnimatorContent.cancel();
        }
        if (tag) {
            mAnimatorTitle = ObjectAnimator.ofFloat(mTitle, "translationY", mTitle.getTranslationY(), 0);
            mAnimatorContent = ObjectAnimator.ofFloat(mListView, "translationY", mListView.getTranslationY(), getResources().getDimension(R.dimen.title_height));

        } else {
            mAnimatorTitle = ObjectAnimator.ofFloat(mTitle, "translationY", mTitle.getTranslationY(), -mTitle.getHeight());
            mAnimatorContent = ObjectAnimator.ofFloat(mListView, "translationY", mListView.getTranslationY(), 0);
        }
        mAnimatorTitle.start();
        mAnimatorContent.start();

    }

    private List<Map<String, Object>> getData() {
        List<Map<String, Object>> data = new ArrayList<>();
        for (int i = 'A'; i < 'z'; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("info", (char) i);
            data.add(map);
        }
        return data;
    }
}

以上是关于Android Studio 简单实现下滑(ListView)隐藏标题栏 上滑显示的主要内容,如果未能解决你的问题,请参考以下文章

Android Studio实现简单的页面跳转(简单教程)

(Android Studio)简单实现:双击返回键,退出应用首页

android studio简单记事本实现

Android Studio实现简单的QQ应用

android studio实现简单考试应用程序

Android Studio实现简单的QQ登录界面