如何在android

时间:2017-06-19 17:48:21

标签: android

这是我的代码,我使用片段作为滑动窗口。但窗口不可拖动。

            Fragment fg= new addplace();
            FragmentManager f = getSupportFragmentManager();
            FragmentTransaction trans = f.beginTransaction().add(R.id.gg,fg);
            trans.commit();

            Interpolator ip = new OvershootInterpolator(5);
            fl.animate().setInterpolator(ip).translationYBy(-200).setDuration(250);

            fl.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {

                    switch(event.getAction())
                    {
                        case MotionEvent.ACTION_DOWN:
                            mlastY=event.getY();
                            return true;

                        case MotionEvent.ACTION_MOVE:
                             double currentY=event.getY();
                             double deltaY=mlastY-currentY;

                            double transY = v.getTranslationY();

                            transY-=deltaY;

                            if(transY<0){
                                transY = 0;
                            }
                            transY=v.getTranslationY();
                    }


                    return false;
                }

            });
            return false;}
    });
}

我无法拖动屏幕。如果有人可以帮助我。

1 个答案:

答案 0 :(得分:0)

您可以使用[BottomSheet] [1]。

  1. 在Android Studio的项目中,在build.gradle文件的依赖项中包含compile 'com.android.support:design:23.2.0'
  2. 在布局文件中添加ViewGroup(LinearLayout,Relativelayout等)

    <android.support.v4.widget.NestedScrollView
    android:id="@+id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="350dp"
    android:clipToPadding="true"
    android:background="@android:color/holo_orange_light"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
    android:fillViewport="true">
    
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/ipsum"
        android:padding="16dp"
        android:textSize="16sp"/>
    

  3. 添加代码

    private BottomSheetBehavior mBottomSheetBehavior;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        View bottomSheet = findViewById( R.id.bottom_sheet );
        Button button1 = (Button) findViewById( R.id.button_1 );
        Button button2 = (Button) findViewById( R.id.button_2 );
        Button button3 = (Button) findViewById( R.id.button_3 );
    
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
    
        mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
    }
    
  4. 显示/隐藏BottomSheet

  5. @Override
    public void onClick(View v) {
        switch( v.getId() ) {
            case R.id.button_1: {
                mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                break;
            }
        }
    }
    

    @Override public void onClick(View v) { switch( v.getId() ) { case R.id.button_1: { mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED); break; } } }