2017-06-19 148 views
-2

這是我的代碼,我使用一個片段作爲滑動窗口,但窗口不可拖動。如何從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;} 
    }); 
} 

我不能拖動screen.If任何人都可以請幫我這一點。

回答

0

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

  1. compile 'com.android.support:design:23.2.0'包含在您的build.gradle文件的依賴項中,位於Android Studio項目中。
  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

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

Full [tutorial][2]. 


    [1]: https://material.io/guidelines/components/bottom-sheets.html 
    [2]: https://code.tutsplus.com/articles/how-to-use-bottom-sheets-with-the-design-support-library--cms-26031 
相關問題