此处介绍的情境是:

(1)使用table布局ListView。

(2)ListView的数据源是List<T>。

(3)排序字段2个(帖子的回复次数和浏览次数),都是int类型。

基本思路:

ListView触发数据源排序,使用数据源(即List<T>)的Sort()方法,又一次绑定数据源到ListView。

实现步骤:

(1)可查知,List<T>的Sort()方法带有一个ICompare<T>泛型接口类型的形參。所以,首先构造继承该泛型接口的类型:

    /// <summary>
/// 回复升序比較类
/// </summary>
public class PostReplyCountAscCompare : IComparer<PostInfo>
{
#region IComparer<PostInfo> 成员 public int Compare(PostInfo x, PostInfo y)
{
return x.ReplyCount.CompareTo(y.ReplyCount);
} #endregion
}
/// <summary>
/// 回复降序比較类
/// </summary>
public class PostReplyCountDescCompare : IComparer<PostInfo>
{
#region IComparer<PostInfo> 成员 public int Compare(PostInfo x, PostInfo y)
{
return y.ReplyCount.CompareTo(x.ReplyCount);
} #endregion
} /// <summary>
/// 浏览升序比較类
/// </summary>
public class PostViewCountAscCompare : IComparer<PostInfo>
{
#region IComparer<PostInfo> 成员 public int Compare(PostInfo x, PostInfo y)
{
return x.ViewCount.CompareTo(y.ViewCount);
} #endregion
}
/// <summary>
/// 浏览降序比較类
/// </summary>
public class PostViewCountDescCompare : IComparer<PostInfo>
{
#region IComparer<PostInfo> 成员 public int Compare(PostInfo x, PostInfo y)
{
return y.ViewCount.CompareTo(x.ViewCount);
} #endregion
}

注意:上述的PostInfo模型类,读者能够杜撰,但要有ViewCount和ReplyCount属性(int类型)。

(2)因为有4个排序规则,相应上述(1)中的4个类。所以构造一个排序辅助类:SortHelper,代码例如以下:

    public class SortHelper
{
/// <summary>
/// 对集合进行排序——泛型方法
/// </summary>
/// <typeparam name="T1">集合中的对象类型</typeparam>
/// <typeparam name="T2">排序类型</typeparam>
/// <param name="collection">要排序的集合</param>
/// <param name="comparer">排序器</param>
public static void Sort<T1,T2>(List<T1> collection,T2 comparer) where T2:IComparer<T1>
{
collection.Sort(comparer);
}
}

(3)设计ListView,构造排序字段

<LayoutTemplate>
<table class="PostList">
<tr class="PostListHeader">
<td colspan="2">
标题
</td>
<td>
公布日期
</td>
<td>
<asp:LinkButton runat="server" ID="lbtnReply" Text="回复" CommandName="Sort" CommandArgument="ReplyCount"
CssClass="sortLink"></asp:LinkButton>
</td>
<td>
<asp:LinkButton runat="server" ID="lbtnView" Text="浏览" CommandName="Sort" CommandArgument="ViewCount"
CssClass="sortLink"></asp:LinkButton>
</td>
<td>
最后发表
</td>
<td>
删除
</td>
</tr>
<tr runat="server" id="itemPlaceholder">
</tr>
</table>
<div class="pager">
<asp:DataPager ID="pagerBottom" runat="server" PageSize="5">
<Fields>
<asp:NextPreviousPagerField ButtonCssClass="command" FirstPageText="<<" PreviousPageText="<"
RenderDisabledButtonsAsLabels="true" ShowFirstPageButton="true" ShowLastPageButton="false"
ShowNextPageButton="false" ShowPreviousPageButton="true" />
<asp:NumericPagerField ButtonCount="7" CurrentPageLabelCssClass="current" NextPreviousButtonCssClass="command"
NumericButtonCssClass="command" />
<asp:NextPreviousPagerField ButtonCssClass="command" LastPageText=">>" NextPageText=">"
RenderDisabledButtonsAsLabels="true" ShowFirstPageButton="false" ShowLastPageButton="true"
ShowNextPageButton="true" ShowPreviousPageButton="false" />
</Fields>
</asp:DataPager>
</div>
</LayoutTemplate>

注意:上面LayoutTemplate中的两个LinkButton,用来作为用户排序接口。其CommandName属性为Sort(固定),CommandArgument分别为ReplyCount和ViewCount。

(4)ListView公开了两个与排序相关的事件:Sorting和Sorted。

        /// <summary>
/// listview排序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lvPosts_Sorting(object sender, ListViewSortEventArgs e)
{
//推断是否指定了排序字段
if (string.IsNullOrEmpty(e.SortExpression))
{
return;
}
//数据源
if (ViewState["posts"] != null)
{
posts = ViewState["posts"] as List<PostInfo>;
}
else
{
posts = new PostInfoBLL().GetAllPosts(begin, end);
ViewState["posts"] = posts;
}
//升序还是降序
if (ViewState["SortDirection"] != null)
{
e.SortDirection=(SortDirection)ViewState["SortDirection"];
} //按哪个字段排序
if (e.SortExpression == "ReplyCount")
{
if (e.SortDirection == SortDirection.Ascending)
{
//泛型方法调用
SortHelper.Sort<PostInfo, PostReplyCountAscCompare>(posts, new PostReplyCountAscCompare());
ViewState["SortDirection"] = SortDirection.Descending;
}
else
{
SortHelper.Sort<PostInfo, PostReplyCountDescCompare>(posts, new PostReplyCountDescCompare());
ViewState["SortDirection"] = SortDirection.Ascending;
}
}
else if (e.SortExpression == "ViewCount")
{
if (e.SortDirection == SortDirection.Ascending)
{
SortHelper.Sort<PostInfo,PostViewCountAscCompare>(posts, new PostViewCountAscCompare());
ViewState["SortDirection"] = SortDirection.Descending;
}
else
{
SortHelper.Sort<PostInfo,PostViewCountDescCompare>(posts, new PostViewCountDescCompare());
ViewState["SortDirection"] = SortDirection.Ascending;
}
}
BindPosts(true);
}

注意:上述方法中的数据源的获取和BindPosts()方法,读者可自行杜撰。

(5)执行界面例如以下图:

在ListView中实现排序的更多相关文章

  1. Android入门 在ListView中如何进行精确的定位

      在android的开发中,经常会遇到需要主动去设定某条ListItem的位置的需求.设置位置的函数有 ListView.setSelection(int position) ListView.se ...

  2. ListView 字母导航排序

    一.概述 ListView字母导航排序,网上已经有很多代码和博客了, 这篇博文也是照搬网上的.  之所以写到这里,不是为了说明什么,只是为了以后自己查阅方便.本来公司要求实现expandablelis ...

  3. ImageLoader在Listview中的使用

    图片加载框架之ImageLoader 1_特点 1)多线程下载图片,图片可以来源于网络,文件系统,项目文件夹assets中以及drawable中等 2)支持随意的配置ImageLoader,例如线程池 ...

  4. ListView中的setOnScrollListener

    ListView是Android中最常用的控件之一,随着时代发展,RecyclerView有取代它的趋势,但是在一些老代码中,ListView依然扮演着重要的作用.项目中遇到一个需求,需要监听List ...

  5. Hadoop学习笔记—11.MapReduce中的排序和分组

    一.写在之前的 1.1 回顾Map阶段四大步骤 首先,我们回顾一下在MapReduce中,排序和分组在哪里被执行: 从上图中可以清楚地看出,在Step1.4也就是第四步中,需要对不同分区中的数据进行排 ...

  6. ListView中的数据表格写入Excel中

    SaveFileDialog sfd = new SaveFileDialog(); sfd.DefaultExt = "xls"; sfd.Filter = "Exce ...

  7. ListView中动态显示和隐藏Header&Footer

    ListView的模板写法 ListView模板写法的完整代码: android代码优化----ListView中自定义adapter的封装(ListView的模板写法) 以后每写一个ListView ...

  8. Android 如何在 ListView 中更新 ProgressBar 进度

    =======================ListView原理============================== Android 的 ListView 的原理打个简单的比喻就是: 演员演 ...

  9. Xamarin.Forms listview中的button按钮,实现带着参数返回上一级页面

    今天在做列表显示的时候遇到一个问题,就是在ListView中如何才能让一个button的按钮工作并且包含参数呢? 其实有点类似于rep里的控件无法起获取一样.在Xamarin中,当你button绑定事 ...

随机推荐

  1. [转]mysql慢查询日志

    原文链接:http://www.cnblogs.com/zhangjing0502/archive/2012/07/30/2615570.html 参考博文:http://blog.chinaunix ...

  2. Python 第三篇(下):collections系列、集合(set)、单双队列、深浅copy、内置函数

     一.collections系列: collections其实是python的标准库,也就是python的一个内置模块,因此使用之前导入一下collections模块即可,collections在py ...

  3. 01-UIKit

    目录: 一.视图和控制器 二.interface Builder 回到顶部 一.视图和控制器 1 视图(view)ios程序运行期间用户所能看见的东西都可以认为是视图,比如UIwindow就是一个视图 ...

  4. android 配置环境变量

    在创建AVD时,在DOS下输入android list targets 会出现android不是内部或外部命令,如图-1.这主要是没有配置好android sdk环境变量所致的. 图-1   andr ...

  5. stringstream clear()的疑问 - yuanshuilee的日志 - 网易博客

    stringstream clear()的疑问 - yuanshuilee的日志 - 网易博客 stringstream clear()的疑问   2013-09-05 08:43:13|  分类: ...

  6. 在Myeclipse中安装java Decompiler

    由于在myeclipse中的Help选项中没有Install New Software,所以在eclipse中安装插件的方法并不适应于Myeclipse,但是我们可以通过点击Windows->P ...

  7. 一步一步重写 CodeIgniter 框架 (2) —— 实现简单的路由功能

    在上一课中,我们实现了简单的根据 URI 执行某个类的某个方法.但是这种映射没有扩展性,对于一个成熟易用的框架肯定是行不通的.那么,我们可以让 框架的用户 通过自定义这种转换来控制,用 CI 的术语就 ...

  8. Eclipse 优化方法(经典收藏)

    第一步: 取消自动validationvalidation有一堆,什么xml.jsp.jsf.js等等,我们没有必要全部都去自动校验一下,只是需要的时候才会手工校验一下! 取消方法:windows–& ...

  9. Eclipse用法和技巧二十三:查看JDK源码

    使用java开发,如果能阅读JDK的经典代码,对自己的水平提高是很有帮助的.笔者在实际工作中总结了两种阅读JDK源码的方式.第一种下载android源代码,直接在android源码代码中,这里的代码虽 ...

  10. winform利用代码将控件置于顶端底端

    有时,我们可能动态的添加控件,并准备将其置于对顶层或最底层.实现的方法有两个: 一种方法是在WinForm窗体中使用Controls控件集的SetChildIndex方法,该方法将子控件设定为指定的索 ...