本文共 3020 字,大约阅读时间需要 10 分钟。
Android音乐、视频类APP常用控件:DraggablePanel(2)
附录文章1主要演示了如何使用DraggablePanel 的DraggableView。DraggablePanel除了DraggableView外,本身DraggablePanel与DraggableView不同的是,DraggableView是一个轻量级的“View”, 而DraggablePanel是一个重的面板、“平台”可以承载更重的如Android Fragment的组件。
写一个测试的MainActivity.java:package zhangphil.demo;import com.github.pedrovgs.DraggableListener;import com.github.pedrovgs.DraggablePanel;import android.os.Bundle;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentActivity;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final DraggablePanel draggablePanel = (DraggablePanel) findViewById(R.id.draggable_panel); findViewById(R.id.image).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { draggablePanel.maximize(); } }); // 注意!添加的监听事件需要放在初始化之前,否则可能无法正常达到监听效果。 draggablePanel.setDraggableListener(new DraggableListener() { @Override public void onMaximized() { Toast.makeText(getApplication(), "onMaximized", Toast.LENGTH_SHORT).show(); } @Override public void onMinimized() { Toast.makeText(getApplication(), "onMinimized", Toast.LENGTH_SHORT).show(); } @Override public void onClosedToLeft() { Toast.makeText(getApplication(), "onClosedToLeft", Toast.LENGTH_SHORT).show(); } @Override public void onClosedToRight() { Toast.makeText(getApplication(), "onClosedToRight", Toast.LENGTH_SHORT).show(); } }); draggablePanel.setFragmentManager(getSupportFragmentManager()); draggablePanel.setTopFragment(new MyTopFragment()); draggablePanel.setBottomFragment(new MyBottomFragment()); draggablePanel.initializeView(); } private static class MyTopFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(android.R.layout.simple_list_item_1, null); v.setBackgroundColor(0xFFEF5350); TextView text = (TextView) v.findViewById(android.R.id.text1); text.setText("zhang phil @ csdn , 顶"); return v; } } private static class MyBottomFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(android.R.layout.simple_list_item_1, null); v.setBackgroundColor(0xFF42A5F5); TextView text = (TextView) v.findViewById(android.R.id.text1); text.setText("zhang phil @ csdn , 底"); return v; } }}
所需的activity_main.xml布局文件:
代码运行结果,从顶向底拖曳,然后到底后再水平向左拖曳,最终点击Android小机器人使之复位,如图所示:
代码中写了两个不同的Android Fragment加载到DraggablePanel中,分别为top和bottom。
附录文章:
1,《Android音乐、视频类APP常用控件:DraggablePanel(1)》链接地址: 2,《Android自底部平滑向上滑出面板的AndroidSlidingUpPanel》链接地址: