今天就跟大家聊聊有关怎么在Android实现一个双击TitleBar回到顶部功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
双击返回顶部代码
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); }}
看完上述内容,你们对怎么在Android实现一个双击TitleBar回到顶部功能有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注编程网行业资讯频道,感谢大家的支持。