Listview首列固定,不能移动到其他列

ListView的首列是checkbox,所以想固定首列,不能移动。
本想找到根本的解决办法,没找到。所以弄了这个笨办法。哪位知道怎么根本解决,麻烦告知,谢谢。
var view = this.lvFiles.View as GridView; if (view != null && view.Columns != null) view.Columns.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Columns_CollectionChanged); } void Columns_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { if (e.Action == NotifyCollectionChangedAction.Move) { var view = this.lvFiles.View as GridView; if (view == null) return; if (view.Columns == null) return; if (e.NewStartingIndex == 0) { view.Columns.CollectionChanged -= new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Columns_CollectionChanged); view.Columns.Move(e.NewStartingIndex, e.OldStartingIndex); view.Columns.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Columns_CollectionChanged); } if (e.OldStartingIndex == 0) { view.Columns.CollectionChanged -= new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Columns_CollectionChanged); view.Columns.Move(e.NewStartingIndex, e.OldStartingIndex); view.Columns.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Columns_CollectionChanged); } } }

  

转载于:https://www.cnblogs.com/xiaokang088/archive/2012/11/12/2766548.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Android中的ListView是一个常用的控件,用于展示数据表,但是在数据非常长的情况下,用户在滚动ListView的时候发现,表头也会随着滚动消失,不再显示在顶部,这个体验并不好。 为了解决这个问题,我们可以通过自定义ListView的头部来实现表头的固定。具体实现如下: 1.在ListView的布局文件中,添加一个LinearLayout布局,作为表头,同时将ListView的头部设为这个LinearLayout。 2.为该LinearLayout添加一个固定高度的属性,例如: android:layout_height="50dp"。这个高度取决于表头的高度。 3.在ListView的布局文件中,为ListView添加下面的属性: android:scrollbars="none",这样就可以禁止滚动条的显示。 4.在Activity中,我们需要定义一个适配器继承自BaseAdapter类。在getView方法中,我们需要添加一段代码: if(position == 0){ headerView = convertView; convertView = null; } 这样,在ListView的第一个元素也就是表头处,我们就可以设置为固定不动的headerView。 5.最后,我们需要在Activity的onCreate方法中,为ListView设置adpter。 通过以上几个步骤,我们就可以实现ListView表头的固定。这样,当我们滚动ListView时,表头仍然保持在屏幕顶部,用户可以清晰地看到表头的信息。 ### 回答2: Android中的ListView是一个非常常用的控件,主要用于展示一系数据。但是,在ListView展示大量数据时,往往需要我们实现固定头的功能,以便用户更好地查看数据。下面,我将分享一下如何实现[ListView固定头](https://blog.csdn.net/u010687392/article/details/78822213)的方法。 首先,我们可以使用两个ListView来实现这个功能。第一个ListView用于展示头,第二个ListView用于展示数据。我们将第二个ListView的滚动事件监听器设置为:当滑动ListView时,首先将第二个ListView的内容向上滚动,然后将第一个ListView的内容向下滚动,以此来实现头的固定。但是,这种方法有一个问题,就是使用两个ListView会影响ListView的滑动效率。 另外,我们还可以通过自定义控件的方式实现固定头。首先,我们要新建一个继承自ListView的类MyListView。然后,在这个类中,我们重写了onMeasure()方法,来计算出第一行item的高度,这个高度将会被用作上面那个固定头的高度。并且在ListView中添加HeaderView,因为HeaderView不会随着滚动而滚动。接下来,我们将一个LinearLayout嵌套在MyListView中,用于显示固定头。在LinearLayout中,我们将会创建一个子控件,用于展示每个名,然后再创建一个HorizontalScrollView来放置起下面的ListView。在这个方法中,我们还需要重写onScrollChanged()方法,使得当下方的ListView滚动时,上方的头也会跟着滚动。 以上两种方法,都可以实现ListView固定头的效果。但是,为了不影响ListView的滑动效率以及为了加强代码复用性,我们建议使用自定义控件的方式实现固定头。自定义控件的方式虽然复杂一些,但是这样做将会更加稳定和可扩展。 ### 回答3: 在Android中,ListView是一个常见的控件,用于在表中显示数据。然而,在一些情况下,我们需要将ListView固定,使其在滚动时保持在屏幕顶部,以便用户在滚动后仍能了解表内容。 首先,我们需要在ListView的布局文件中添加一个Header View,用于显示头,这可以使用ListView的addHeaderView()方法来实现。例如,我们可以在ListView的XML布局文件中添加以下代码: ```xml <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 添加Header视图 --> <LinearLayout android:id="@+id/header" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <!-- 头 --> <TextView android:id="@+id/textview_header_1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="头1" /> <TextView android:id="@+id/textview_header_2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="头2" /> <TextView android:id="@+id/textview_header_3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="头3" /> </LinearLayout> </ListView> ``` 接下来,在代码中使用ViewTreeObserver来监听ListView的布局变化,并在布局完成后将Header View添加到ListView中。在onCreate()方法中添加以下代码: ```java final ListView listView = findViewById(R.id.listview); final LinearLayout headerView = findViewById(R.id.header); final TextView textViewHeader1 = findViewById(R.id.textview_header_1); final TextView textViewHeader2 = findViewById(R.id.textview_header_2); final TextView textViewHeader3 = findViewById(R.id.textview_header_3); // 监听布局变化 ViewTreeObserver vto = listView.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { // 移除监听 listView.getViewTreeObserver().removeOnGlobalLayoutListener(this); // 获取第一项的高度 int height = listView.getChildAt(0).getHeight(); // 将Header视图添加到ListView listView.addHeaderView(headerView); // 设置Header视图的高度 headerView.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, height)); // 设置头的宽度 textViewHeader1.setWidth(headerView.getWidth() / 3); textViewHeader2.setWidth(headerView.getWidth() / 3); textViewHeader3.setWidth(headerView.getWidth() / 3); } }); ``` 这个方法在ListView的布局完成后被调用,它首先获取第一项的高度,并将Header View添加到ListView中。然后,将HeaderView的高度设置为第一项的高度。最后,设置头的宽度以便它们与ListView中的条目对齐。 最后,在ListView中使用OnScrollListener监听滚动事件,并在滚动时调整Header View的位置,使其保持在ListView的顶部。在Activity中添加以下代码: ```java listView.setOnScrollListener(new OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { // TODO Auto-generated method stub } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { // 获取Header View View headerView = listView.getChildAt(0); // 如果ListView的第一项可见,则将Header View添加到WindowManager中 if (listView.getFirstVisiblePosition() == 0) { headerView.setVisibility(View.VISIBLE); // 获取屏幕宽度 DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); int screenWidth = metrics.widthPixels; // 调整Header View的位置 WindowManager.LayoutParams layoutParams = (WindowManager.LayoutParams) headerView.getLayoutParams(); layoutParams.x = 0; layoutParams.y = 0; layoutParams.width = screenWidth; getWindowManager().updateViewLayout(headerView, layoutParams); } else { headerView.setVisibility(View.GONE); } } }); ``` 这个方法在滚动时被调用,它首先获取Header View。如果ListView的第一项可见,则将Header View 添加到WindowManager中,并将其调整到屏幕顶部。否则,将其隐藏。 综上所述,通过在布局文件中添加HeaderView,监听布局变化,设置头宽度和高度,以及在滚动时调整Header View的位置,我们可以实现在Android中使ListView固定的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值