上下拖动片段



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

main。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);
}
}

谢谢你的帮助。

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

你必须有扩展你的调色板片段与BottomSheetDialogFragment和简单地使用以下行来显示它在活动:

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

在你的PaletteFragment中,使用BottomSheetBehavior.BottomSheetCallback()为调色板添加监听器。在这个回调中,你可以在幻灯片上更新调色板的高度。