带有动画的向上/向下滑动菜单中的ListView不会下降

时间:2015-12-02 08:31:30

标签: android

我有一个从下到上的滑动上/下菜单并填满屏幕。菜单中的简单视图时,它工作正常。但是当我在滑动菜单中使用列表视图时。它没有下降。 我已经应用了onTouchEvent()但它的ACTION_DOWN不起作用。我不知道为什么。我遇到过一些解决方案,例如: - 使用 onInterceptTouchEvent()。但它也没有成功。

这是我的java文件: -

package com.sourcefuse.pocSNS.slideupdown;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

    private Animation animUp;
    private Animation animDown;
    LinearLayout ll;

    TextView chat_bar_up, chat_bar;

    private float startY;


    String[] mobileArray = {"Android", "IPhone", "WindowsMobile", "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", "A", "B", "C", "D", "E", "F", "G"};


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

        chat_bar_up = (TextView) findViewById(R.id.chat_bar_up);
        chat_bar = (TextView) findViewById(R.id.chat_bar);

        ll = (LinearLayout) findViewById(R.id.slider);
        ll.setVisibility(View.GONE);

        animUp = AnimationUtils.loadAnimation(this, R.anim.anim_up);
        animDown = AnimationUtils.loadAnimation(this, R.anim.anim_down);

        ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.activity_listview, mobileArray);

        ListView listView = (ListView) findViewById(R.id.mobile_list);
        listView.setAdapter(adapter);

    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                startY = event.getY();
                break;
            }
            case MotionEvent.ACTION_UP: {
                float endY = event.getY();

                if (endY < startY) {
                    chat_bar_up.setVisibility(View.GONE);
                    ll.setVisibility(View.VISIBLE);
                    ll.startAnimation(animUp);


                    new Handler().postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            chat_bar.setVisibility(View.GONE);
                            getSupportActionBar().setTitle(chat_bar_up.getText());
                        }
                    }, 500);

                } else {
                    chat_bar.setVisibility(View.VISIBLE);
                    ll.startAnimation(animDown);
                    ll.setVisibility(View.GONE);


                    new Handler().postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            getSupportActionBar().setTitle("SlideUpDown");
                            chat_bar_up.setVisibility(View.VISIBLE);

                        }
                    }, 500);

                }

            }
        }
        return true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.menu_main, menu);
        return super.onCreateOptionsMenu(menu);


    }

}

我的主要活动的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:background="#eeffee77">


    <LinearLayout
        android:id="@+id/slider"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:background="#eeff6677"
        android:content="@+id/content"
        android:orientation="vertical">


        <TextView
            android:id="@+id/chat_bar"
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:layout_alignParentBottom="true"
            android:background="@color/colorPrimaryDark"
            android:text="Heloo world"
            android:textColor="#ffffff" />

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


    </LinearLayout>


    <TextView
        android:id="@+id/chat_bar_up"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_alignParentBottom="true"
        android:background="@color/colorPrimaryDark"
        android:text="Heloo world"
        android:textColor="#ffffff" />


</RelativeLayout>

0 个答案:

没有答案