首页 > 解决方案 > 上下拖动片段

问题描述

我的主要活动由两个片段组成,一个叫做画布,另一个是调色板。在主要活动中,调色板仅出现在屏幕的 25% 上,我想让它成为可能,以便用户可以上下拖动以从中选择颜色。我不知道该怎么做。

主.xml:

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

    <FrameLayout
        android:id="@+id/canvasFragment"
        android:name="com.example.FragmentOne"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1.5" />

    <FrameLayout
        android:id="@+id/paletteFragment"
        android:name="com.example.FragmentTwo"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5" />

</LinearLayout>

mainActivity.java:

package com.example.paint;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends AppCompatActivity {

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

        /**Canvas**/
        // Create new fragment and transaction
        Fragment canvasFragment = new CanvasFragment();
        //canvasFragment.getView().setBackgroundColor(this.cor);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        // Replace whatever is in the fragment_container view with this fragment,
        // and add the transaction to the back stack
        transaction.replace(R.id.canvasFragment, canvasFragment);
        transaction.addToBackStack(null);
        // Commit the transaction
        transaction.commit();

        /**Palette**/
        // Create new fragment and transaction
        Fragment paletteFragment = new PaletteFragment();
        FragmentTransaction transaction2 = getSupportFragmentManager().beginTransaction();
        // Replace whatever is in the fragment_container view with this fragment,
        // and add the transaction to the back stack
        transaction2.replace(R.id.paletteFragment, paletteFragment);
        transaction2.addToBackStack(null);
        // Commit the transaction
        transaction2.commit();

        setContentView(R.layout.activity_main);
    }


}

PaletteFragment.java:

package com.example.paint;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

public class PaletteFragment extends BottomSheetDialogFragment {

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

}

感谢您提供的任何帮助。

标签: javaandroidxmlandroid-fragments

解决方案


您可以使用 BottomSheetDialogFragment 来实现所需的结果。这是 Android 材质库中的一个对话框片段。

您必须使用 BottomSheetDialogFragment 扩展您的 Palette 片段,并简单地使用以下行在 Activity 中显示它:

PaletteFragment paletteFragment = new PaletteFragment();
paletteFragment.show(getSupportFragmentManager(), "TAGTEXT");

在您的 PaletteFragment 中,使用 BottomSheetBehavior.BottomSheetCallback() 为要滑动的调色板添加侦听器。在此回调中,您可以更新幻灯片上的调色板高度。


推荐阅读