在片段中添加上滑面板

Posted

技术标签:

【中文标题】在片段中添加上滑面板【英文标题】:Add a slide up panel in a fragment 【发布时间】:2020-12-06 23:23:45 【问题描述】:

我很难将上滑面板添加到我的一项活动中。我想添加一个面板,它位于屏幕中间,然后一直到应用程序本身的顶部。

我希望屏幕看起来与下图类似。

这是我的 MainActivity 代码

public class MainActivity extends AppCompatActivity 

    private DrawerLayout drawer;//sets what you see in the side panel

    private AppBarConfiguration mAppBarConfiguration;//allows for user to press buttons

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        ListView mylistview = findViewById(R.id.listView);
//        ArrayList<String> myFamily = new ArrayList<String>();
//        myFamily.add("Rob");
//        myFamily.add("May");
//
//        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
//                android.R.layout.simple_expandable_list_item_1, myFamily);
//        mylistview.setAdapter(arrayAdapter);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        drawer = findViewById(R.id.drawer_layout);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
                R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

//        FloatingActionButton fab = findViewById(R.id.fab); //this is for the message button; do not worry about it

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_account, R.id.nav_coupons, R.id.nav_settings)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);

//        ListView listView = findViewById(R.id.listView);
//        listView.setAdapter(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,
//                new String[] "THis","that ","when","where","why","is"));

    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    



    @Override
    public boolean onSupportNavigateUp() 
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    




    @Override
    public void onBackPressed() 
        if (drawer.isDrawerOpen(GravityCompat.START)) 
            drawer.closeDrawer(GravityCompat.START);
         else 
            super.onBackPressed();

        
    




这是我的家庭活动 xml 文件的代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"

    android:layout_
    android:layout_
    android:background="@android:color/holo_red_light"



    android:layout_gravity="bottom"
    android:gravity="bottom"
    app:umanoDragView="@id/dragview"
    app:umanoOverlay="false"
    app:umanoPanelHeight="68dp"
    app:umanoParallaxOffset="100dp"
    app:umanoScrollableView="@id/listView"
    app:umanoShadowHeight="4dp">


    <TextView
        android:id="@+id/text_home"
        android:layout_
        android:layout_
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:textAlignment="center"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

这是我的视图模型的主片段代码


public class HomeFragment extends Fragment 

    private HomeViewModel homeViewModel;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) 
        homeViewModel =
                ViewModelProviders.of(this).get(HomeViewModel.class);
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        final TextView textView = root.findViewById(R.id.text_home);
        homeViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() 
            @Override
            public void onChanged(@Nullable String s) 
                textView.setText(s);
            
        );
        return root;

    

【问题讨论】:

【参考方案1】:

我更喜欢使用 top sheet 的这个实现:

https://github.com/MedveDomg/AndroidTopSheet

你可以自定义任何你需要的东西。

【讨论】:

以上是关于在片段中添加上滑面板的主要内容,如果未能解决你的问题,请参考以下文章

jquery ui 选项卡上滑效果

Android在活动视图中附加片段获取片段已添加错误

在片段中添加按钮? [复制]

如何在 FragmentPagerAdapter 中添加片段标签以便稍后引用片段?

在片段中添加 ListView

如何在表单面板中添加网格面板?