notifyitem源码_notifyitemremoved

hacker|
124

文章目录:

list控件 改变部分字体颜色?

自己选择使用下面代码:

用CListCtrl来显示数据比较方便,有时候我们需要标注某一列或某一个单元格的背景和字体颜色,或者需要改变一下行高和字体大小, CListCtrl要改变这些并不是很方便。本文将介绍如何派生一个类来改变CListCtrl及其表头的高度、字体大小、列背景颜色、单元格背景颜色、 列字体颜色、单元格字体颜色。

一、 实现过程

1.表头修改

新建一个MFC类CHeaderCtrlCl,其基类为CHeaderCtrl,响应OnPaint消息实现自绘,实现代码请看源代码(由于代码较占篇幅,所以就不贴上来了,抱歉),在头文件中定义函数

LRESULT OnLayout( WPARAM wParam, LPARAM lParam ),之后手动添加消息响应ON_MESSAGE(HDM_LAYOUT, OnLayout),在消息响应中改变高度,实现代码如下:

LRESULT CHeaderCtrlCl::OnLayout( WPARAM wParam, LPARAM lParam )

{

LRESULT lResult = CHeaderCtrl::DefWindowProc(HDM_LAYOUT, 0, lParam);

HD_LAYOUT hdl = *( HD_LAYOUT * ) lParam;

RECT *prc = hdl.prc;

WINDOWPOS *pwpos = hdl.pwpos;

int nHeight = (int)(pwpos-cy * m_Height); //改变高度,m_Height为倍数

pwpos-cy = nHeight;

prc-top = nHeight;

return lResult;

}2. 表的修改

新建一个MFC类CListCtrlCl,其基类为CListCtrl,定义一个CHeaderCtrlCl的成员变量m_Header,重载PreSubclassWindow(),在函数中修改控件类型为自绘模式,然后子类化表头,代码如下:

void CListCtrlCl::PreSubclassWindow()

{

// TODO: 在此添加专用代码和/或调用基类

ModifyStyle(0,LVS_OWNERDRAWFIXED);

CListCtrl::PreSubclassWindow();

CHeaderCtrl *pHeader = GetHeaderCtrl();

m_Header.SubclassWindow(pHeader-GetSafeHwnd());

}添加成员变量,保存一些基本信息。CPtrList m_ptrListCol; //保存列颜色

CPtrList m_ptrListItem; //保存Item颜色表

CPtrList m_colTextColor; //保存列字体颜色

CPtrList m_ItemTextColor; //保存单元格字体颜色重载DrawItem()实现自绘,实现代码请参照代码。 手动添加消息宏ON_WM_MEASUREITEM_REFLECT(),添加下面的函数,实现修改行高。void CListCtrlCl::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)

{

if (m_nRowHeight0)

{

lpMeasureItemStruct-itemHeight = m_nRowHeight;

}

}添加消息处理OnMeasureItem()void CListCtrlCl::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct)

{

// TODO: 在此添加消息处理程序代码和/或调用默认值

CListCtrl::OnMeasureItem(nIDCtl, lpMeasureItemStruct);

}覆盖基类InsertColumn进行列信息保存int CListCtrlCl::InsertColumn(int nCol, LPCTSTR lpszColumnHeading,

int nFormat /* = LVCFMT_LEFT */,

int nWidth /* = -1 */,

int nSubItem /* = -1 */)

{

m_Header.m_HChar.Add(lpszColumnHeading);

if (nFormat==LVCFMT_LEFT)

{

m_Header.m_Format = m_Header.m_Format + "0";

}

else if (nFormat==LVCFMT_CENTER)

{

m_Header.m_Format = m_Header.m_Format + "1";

}

else if (nFormat==LVCFMT_RIGHT)

{

m_Header.m_Format = m_Header.m_Format + "2";

}

else

{

m_Header.m_Format = m_Header.m_Format + "1";

}

return CListCtrl::InsertColumn(nCol,lpszColumnHeading,nFormat,nWidth,nSubItem);

}

三、使用方法

把HeaderCtrlCl.h, HeaderCtrlCl.cpp, ListCtrlCl.h, ListCtrlCl.cpp添加到你的工程中,在拖一个CListCtrl控件到对话框上,添加成员变量,然后在头文件中#include “ListCtrlCl.h”,把刚添加的成员变量类型改为 CListCtrlCl ,在对话框的OnInitDialog()中添加初始化代码:

m_ListCtrl.SetColColor(0,RGB(10,150,20)); //设置列背景色

m_ListCtrl.SetColColor(2,RGB(30,100,90)); //设置列背景色

m_ListCtrl.SetBkColor(RGB(50,10,10)); //设置背景色

m_ListCtrl.SetItemColor(1,1,RGB(100,100,10)); //设置指定单元背景色

m_ListCtrl.SetRowHeigt(25); //设置行高度

m_ListCtrl.SetHeaderHeight(1.5); //设置头部高度

m_ListCtrl.SetHeaderFontHW(16,0); //设置头部字体高度,和宽度,0表示缺省,自适应

m_ListCtrl.SetHeaderTextColor(RGB(255,200,100)); //设置头部字体颜色

m_ListCtrl.SetTextColor(RGB(0,255,255)); //设置文本颜色

m_ListCtrl.SetHeaderBKColor(100,255,100,8); //设置头部背景色

m_ListCtrl.SetFontHW(15,0); //设置字体高度,和宽度,0表示缺省宽度

m_ListCtrl.SetColTextColor(2,RGB(255,255,100)); //设置列文本颜色

m_ListCtrl.SetItemTextColor(3,1,RGB(255,0,0)); //设置单元格字体颜色四、结语

android中添加头部视图的功能怎么实现

ListView是有addHeaderView和 addFooterView两个方法的.

但是作为官方推荐的ListView的升级版RecyclerView缺无法实现这两个方法。

那么如果使用RecyclerView实现这两个方法的效果该怎么做呢?

网上查询了很久,试过各种各样的实现方式,终于让我发现一个还不错的实现方法,那么就给大家推荐一下。

项目地址(别人写的,非博主的)

我看了下这个源码,很简单,即写了一个继承RecyclerView的控件,自己实现addHeaderView和addFooterView两个方法

?

code

package com.xqx.com.recyclerviewheaderdemo;

import android.content.Context;

import android.support.v7.widget.RecyclerView;

import android.util.AttributeSet;

import android.util.Log;

import android.view.View;

import android.view.ViewGroup;

import java.util.ArrayList;

import java.util.List;

public class XCRecyclerView extends RecyclerView{

private ArrayListView mHeaderViews = new ArrayList();

private ArrayListView mFooterViews = new ArrayList();

private RecyclerView.Adapter mAdapter;

private RecyclerView.Adapter mWrapAdapter;

private static final int TYPE_HEADER = -101;

private static final int TYPE_FOOTER = -102;

private static final int TYPE_LIST_ITEM = - 103;

public XCRecyclerView(Context context) {

this(context, null);

}

public XCRecyclerView(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

public XCRecyclerView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

init(context);

}

private void init(Context context){

}

@Override

public void setAdapter(Adapter adapter) {

mAdapter = adapter;

mWrapAdapter = new WrapAdapter(mHeaderViews, mFooterViews, adapter);

super.setAdapter(mWrapAdapter);

mAdapter.registerAdapterDataObserver(mDataObserver);

}

public void addHeaderView(View view){

mHeaderViews.clear();

mHeaderViews.add(view);

}

public void addFooterView(View view){

mFooterViews.clear();

mFooterViews.add(view);

}

public int getHeaderViewsCount(){

return mHeaderViews.size();

}

public int getFooterViewsCount(){

return mFooterViews.size();

}

private final RecyclerView.AdapterDataObserver mDataObserver = new RecyclerView.AdapterDataObserver() {

@Override

public void onChanged() {

mWrapAdapter.notifyDataSetChanged();

}

@Override

public void onItemRangeChanged(int positionStart, int itemCount) {

mWrapAdapter.notifyItemRangeChanged(positionStart, itemCount);

}

// @Override

// public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {

// mWrapAdapter.notifyItemRangeChanged(positionStart, itemCount, payload);

// }

@Override

public void onItemRangeInserted(int positionStart, int itemCount) {

mWrapAdapter.notifyItemRangeInserted(positionStart, itemCount);

}

@Override

public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {

mWrapAdapter.notifyItemMoved(fromPosition, toPosition);

}

@Override

public void onItemRangeRemoved(int positionStart, int itemCount) {

mWrapAdapter.notifyItemRangeRemoved(positionStart, itemCount);

}

};

private class WrapAdapter extends RecyclerView.AdapterViewHolder{

private Adapter mAdapter;

private ListView mHeaderViews;

private ListView mFooterViews;

public WrapAdapter(ListView headerViews,ListView footerViews,Adapter adapter){

this.mAdapter = adapter;

this.mHeaderViews = headerViews;

this.mFooterViews = footerViews;

}

public int getHeaderCount(){

return this.mHeaderViews.size();

}

public int getFooterCount(){

return this.mFooterViews.size();

}

public boolean isHeader(int position){

return position = 0 position this.mHeaderViews.size();

}

public boolean isFooter(int position){

return position getItemCount() position = getItemCount() - this.mFooterViews.size();

}

@Override

public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

if(viewType == TYPE_HEADER){

return new CustomViewHolder(this.mHeaderViews.get(0));

}else if(viewType == TYPE_FOOTER){

return new CustomViewHolder(this.mFooterViews.get(0));

}else{

return this.mAdapter.onCreateViewHolder(parent,viewType);

}

}

@Override

public void onBindViewHolder(ViewHolder holder, int position) {

if(isHeader(position)) return;

if(isFooter(position)) return;

int rePosition = position - getHeaderCount();

int itemCount = this.mAdapter.getItemCount();

if(this.mAdapter != null){

if(rePosition itemCount){

Log.v("czm","rePosition/itemCount="+rePosition+"/"+itemCount);

this.mAdapter.onBindViewHolder(holder,rePosition);

return;

}

}

}

@Override

public long getItemId(int position) {

if (this.mAdapter != null position = getHeaderCount()) {

int rePosition = position - getHeaderCount();

int itemCount = this.mAdapter.getItemCount();

if (rePosition itemCount) {

return this.mAdapter.getItemId(rePosition);

}

}

return -1;

}

@Override

public int getItemViewType(int position) {

if(isHeader(position)){

return TYPE_HEADER;

}

if(isFooter(position)){

return TYPE_FOOTER;

}

int rePosition = position - getHeaderCount();

int itemCount = this.mAdapter.getItemCount();

if(rePosition itemCount){

return this.mAdapter.getItemViewType(position);

}

return TYPE_LIST_ITEM;

}

@Override

public int getItemCount() {

if(this.mAdapter != null){

return getHeaderCount() + getFooterCount() + this.mAdapter.getItemCount();

}else{

return getHeaderCount() + getFooterCount();

}

}

@Override

public void registerAdapterDataObserver(AdapterDataObserver observer) {

if(this.mAdapter != null){

this.mAdapter.registerAdapterDataObserver(observer);

}

}

@Override

public void unregisterAdapterDataObserver(AdapterDataObserver observer) {

if(this.mAdapter != null){

this.mAdapter.unregisterAdapterDataObserver(observer);

}

}

private class CustomViewHolder extends ViewHolder{

public CustomViewHolder(View itemView) {

super(itemView);

}

}

}

}

XCRecyclerView

使用方法github里也写的清清楚楚的

?

code

private MyAdapter mAdapter;

private XCRecyclerView mRecyclerView;

private ListString mData;

private View mHeaderView;

private View mFooterView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mData = new ArrayListString();

for(int i = 0; i 10 ;i++){

mData.add("item_" + i);

}

mAdapter = new MyAdapter(mData);

mRecyclerView = (XCRecyclerView) findViewById(R.id.recycler_view);

mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

mHeaderView = LayoutInflater.from(this).inflate(R.layout.layout_header,mRecyclerView,false);

mFooterView = LayoutInflater.from(this).inflate(R.layout.layout_footer,mRecyclerView,false);

mRecyclerView.addHeaderView(mHeaderView);

mRecyclerView.addFooterView(mFooterView);

mRecyclerView.setAdapter(mAdapter);

}

注意点:

addHeaderView之后 列表的数据坐标即相应发生变化!即addHeadView一次,列表第一个数据的下坐标+1(0--1)

5条大神的评论

  • avatar
    访客 2022-07-13 下午 11:22:47

    荐一下。项目地址(别人写的,非博主的)我看了下这个源码,很简单,即写了一个继承RecyclerView的控件,自己实现addHeaderView和addFooterView两个方法?codepackage com.xqx.com.recyclerviewheaderdemo;import

  • avatar
    访客 2022-07-14 上午 08:39:31

    YOUT, 0, lParam);HD_LAYOUT hdl = *( HD_LAYOUT * ) lParam;RECT *prc = hdl.prc;WINDOWPOS *pwpos = hdl.pwpos;int nHeight = (int)(pwpos-cy * m_He

  • avatar
    访客 2022-07-14 上午 01:30:23

    到对话框上,添加成员变量,然后在头文件中#include “ListCtrlCl.h”,把刚添加的成员变量类型改为 CListCtrlCl ,在对话框的OnInitDialog()中添加初始化代码:m_ListCtrl.SetColColor(0,RGB(10

  • avatar
    访客 2022-07-14 上午 12:52:33

    id onItemRangeChanged(int positionStart, int itemCount) { mWrapAdapter.notifyItemRangeChanged(positionStart, itemCou

  • avatar
    访客 2022-07-14 上午 09:47:52

    derViews; private ListView mFooterViews; public WrapAdapter(ListView headerVie

发表评论