WP8_检测列表是否滑动

One of the UI features of lists on Windows Phone 7 is that the "scroll bars" don‘t really act like traditional scroll bars; they are non-interactive and they only appear when the list is actually scrolling. To achieve this, the Silverlight team added a new visual state group that is used in the default control template for showing / hiding the scroll bars.

You can get programmatic access to the scroll state with the following approach. First, paste this XAML into the ContentGrid of a new WP7 application:

<ListBox FontSize="50" x:Name="theList">
  <ListBoxItem Content="Item 00"/>
  <ListBoxItem Content="Item 01"/>
  <ListBoxItem Content="Item 02"/>
  <ListBoxItem Content="Item 03"/>
  <ListBoxItem Content="Item 04"/>
  <ListBoxItem Content="Item 05"/>
  <ListBoxItem Content="Item 06"/>
  <ListBoxItem Content="Item 07"/>
  <ListBoxItem Content="Item 08"/>
  <ListBoxItem Content="Item 09"/>
  <ListBoxItem Content="Item 10"/>
  <ListBoxItem Content="Item 11"/>
  <ListBoxItem Content="Item 12"/>
  <ListBoxItem Content="Item 13"/>
  <ListBoxItem Content="Item 14"/>
  <ListBoxItem Content="Item 15"/>
  <ListBoxItem Content="Item 16"/>
  <ListBoxItem Content="Item 17"/>
  <ListBoxItem Content="Item 18"/>
  <ListBoxItem Content="Item 19"/>
  <ListBoxItem Content="Item 20"/>
  <ListBoxItem Content="Item 21"/>
  <ListBoxItem Content="Item 22"/>
  <ListBoxItem Content="Item 23"/>
  <ListBoxItem Content="Item 24"/>
  <ListBoxItem Content="Item 25"/>
  <ListBoxItem Content="Item 26"/>
  <ListBoxItem Content="Item 27"/>
  <ListBoxItem Content="Item 28"/>
  <ListBoxItem Content="Item 29"/>
</ListBox>

Now add the following to the code-behind file:

public MainPage()
{
  InitializeComponent();
  Loaded += new RoutedEventHandler(MainPage_Loaded);
}

bool alreadyHookedScrollEvents = false;

void MainPage_Loaded(object sender, RoutedEventArgs e)
{
  if (alreadyHookedScrollEvents)
    return;

alreadyHookedScrollEvents = true;
  ScrollViewer viewer = FindSimpleVisualChild<ScrollViewer>(theList);
  if (viewer != null)
  {
    // Visual States are always on the first child of the control template
    FrameworkElement element = VisualTreeHelper.GetChild(viewer, 0) as FrameworkElement;
    if (element != null)
    {
      VisualStateGroup group = FindVisualState(element, "ScrollStates");
      if (group != null)
      {
        group.CurrentStateChanging += (s, args) => PageTitle.Text = args.NewState.Name;
      }
    }
  }
}

VisualStateGroup FindVisualState(FrameworkElement element, string name)
{
  if (element == null)
    return null;

IList groups = VisualStateManager.GetVisualStateGroups(element);
  foreach (VisualStateGroup group in groups)
    if (group.Name == name)
      return group;

return null;
}

T FindSimpleVisualChild<T>(DependencyObject element) where T : class
{
  while (element != null)
  {

if (element is T)
      return element as T;

element = VisualTreeHelper.GetChild(element, 0);
  }

return null;
}

What this does is attach to the CurrentStateChanging event of the VisualStateGroup, which will be raised every time the scroll state changes from "Scrolling" to "NotScrolling" or vice-versa. There‘s a bunch of infrastructure code to walk the visual tree and pull out a state group, but the core code is very simple:

  1. First we attach a handler called MainPage_Loaded to the Page.Loaded event

  2. When the page loads, we call FindSimpleVisualChild to get the ScrollViewerchild of the list (this is a pretty dumb function)
    1. We make sure to only do this once, because the page could get loaded more than once if it is navigated
  3. Then we call FindVisualState to get the named visual state from the first child of the ScrollViewer
  4. Then we add a handler to the CurrentStateChanging event

原文作者:Peter Torr - MSFT

原文链接: http://blogs.msdn.com/b/ptorr/archive/2010/07/23/how-to-detect-when-a-list-is-scrolling-or-not.aspx

时间: 2024-08-28 23:48:34

WP8_检测列表是否滑动的相关文章

Android 实现用户列表信息滑动删除功能和选择删除功能

在项目开发过程中,常常需要对用户列表的信息进行删除的操作.Android中常用的删除操作方式有两种 ,一种就是类似微信的滑动出现删除按钮方式,还有一种是通过CheckBox进行选择,然后通过按钮进行删除的方式.本来的实例集成上述的两种操作方式来实现用户列表删除的效果. 设计思路:在适配器类MyAdapter一个滑动删除按钮显示或隐藏的Map,一个用于CheckBox是否选中的Map和一个与MainAcitivyt进行数据交互的接口ContentsDeleteListener,同时该接口包含两个方

Android-自定义仿QQ列表Item滑动

效果图: 布局中去指定自定义FrameLayout: <!-- 自定义仿QQ列表Item滑动 --> <view.custom.shangguigucustomview.MyCustomFrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_

提升html5的性能体验系列之二列表流畅滑动

App的顶部一般有titlebar,下面是list.常见的一个需求是要在list滚动时,titlebar不动.这个简单的需求,实现起来其实并不简单. 在普通web上的做法是使用div的滚动条,把list放到div里.但是很不幸的是,div滚动条在Android4.4以下的手机上非常非常卡,滚动完全无法商用. 还有一种做法是把titlebar使用fix来固顶.但此时手机浏览器的滚动条会通到顶部,当titlebar的右侧出现滚动条时,看起来非常怪异.还有一个副作用是滚动太快时,titlebar的di

移动端列表循环滑动

1.循环滑动效果(先看效果)      2.如何布局(以下是我的思路) <style type="text/css"> * { margin: 0; padding: 0; } #scroll div { border: 1px solid red; float: left; background: #abcdef; text-align: center; } </style> </head> <body> <div id=&quo

android listview + checkbox 列表上下滑动导致复选框状态丢失解决办法

以前为这个问题头疼很久.然后去忙其他事情.一直没有去整理,今天好不容易闲下来.就来整整这个listview + checkbox的问题吧 界面: listview_cell: 界面很简单,一个全屏的listview,cell很简单,一个textview一个checkbox activity: package com.example.testlistviewandcheckbox; import java.util.ArrayList; import java.util.List; import

监听列表ListVIew的滑动状态

/*监听列表的滑动状态:暂时用不到 * SCROLL_STATE_FLING 时让图片不显示,提高滚动性能让滚动小姑更平滑 * SCROLL_STATE_IDLE 时显示当前屏幕可见的图片*/ mListView.setOnScrollListener(new OnScrollListener() { //滑动的状态变化 public void onScrollStateChanged(AbsListView view, int scrollState) { switch (scrollStat

RecycleViewScrollHelper--RecyclerView滑动事件检测的辅助类

目录 概述 这是一个关于RecycleView滑动事件的辅助类,该辅助类可以检测RecycleView滑动到顶部或者底部的状态. 可用于实现RecycleView加载更多或者刷新(虽然刷新可以直接用SwipeRefreshLayout).也可用于某些滑动相关的需求,如FloatingActionButton的隐藏与显示之类的. 关于RecycleView的滑动监听 RecycleView本身已经提供了滑动的监听接口,OnScrollListener,这个接口包含了以下的方法. //当recycl

用caffe一步一步实现人脸检测

学习深度学习已有一段时间了,总想着拿它做点什么,今天终于完成了一个基于caffe的人脸检测,这篇博文将告诉你怎样通过caffe一步步实现人脸检测.本文主要参考唐宇迪老师的教程,在这里感谢老师的辛勤付出. 传统机器学习方法实现人脸检测: 人脸检测在opencv中已经帮我们实现了,我们要把它玩起来很简单,只需要简简单单的几行代码其实就可以搞定.(haarcascade_frontalface_alt.xml这个文件在opencv的安装目录下能找到,笔者的路径是:E:\opencv2.4.10\ope

android源码大放送(实战开发必备),免费安卓demo源码,例子大全文件详细列表

免费安卓demo源码,例子大全文件详细列表 本列表源码永久免费下载地址:http://www.jiandaima.com/blog/android-demo 卷 yunpan 的文件夹 PATH 列表 卷序列号为 0000-73EC E:. │ jiandaima.com文件列表生成.bat │ 例子大全说明.txt │ 本例子永久更新地址~.url │ 目录列表2016.03.10更新.txt │ ├─前台界面 │ ├─3D标签云卡片热门 │ │ Android TagCloudView云标签