当前位置:主页 > 移动开发 > Android代码 >

Android实现双击TitleBar回顶部的功能示例代码

时间:2021-06-25 09:25:08 | 栏目:Android代码 | 点击:

前言

本文介绍的内容是偶然发现的这个功能,就给移过来了,整理了一下,也是一个类就实现的,使用很方便

特别感谢@TakWolf大大的开源项目,学了好多Android方面的东西

双击返回顶部代码

public class DoubleClickBackToContentTopListener implements View.OnClickListener {

 private final long delayTime = 300;
 private long lastClickTime = 0;
 private final IBackToContentTopView backToContentTopView;

 public interface IBackToContentTopView {
 void backToContentTop();
 }

 @Override
 public final void onClick(View v) {
 long nowClickTime = System.currentTimeMillis();
 if (nowClickTime - lastClickTime > delayTime) {
  lastClickTime = nowClickTime;
 } else {
  onDoubleClick(v);
 }
 }

 public DoubleClickBackToContentTopListener(@NonNull IBackToContentTopView backToContentTopView) {
 this.backToContentTopView = backToContentTopView;
 }

 public void onDoubleClick(View v) {
 backToContentTopView.backToContentTop();
 }

}

使用方法

public class CNodeActivity extends Activity implements 
 DoubleClickBackToContentTopListener.IBackToContentTopView {

 private Toolbar toolbar;
 private ListView listView;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_cnode);

 toolbar = (Toolbar) findViewById(R.id.toolbar);
 toolbar.setTitle(R.string.cnode);
 toolbar.setNavigationOnClickListener(v -> this.finish());
 toolbar.setOnClickListener(new DoubleClickBackToContentTopListener(this));

 listView = (ListView) findViewById(R.id.list_view);
 
 listView.setAdapter(//TODO);

 initData();

 }

 private void initData() {
 //TODO
 }

 @Override
 public void backToContentTop() {
 listView.setSelection(0);
 }

}

说明一下,DoubleClickBackToContentTopListener类里的delayTime属性是双击之间的时间差,这里默认设置的是300毫秒

就是这么简单,一个好的封装,可以省多少事呀!

总结

参考

https://github.com/TakWolf/CNode-Material-Design

您可能感兴趣的文章:

相关文章