PC 自定义ListItem

Posted 十壹、

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PC 自定义ListItem相关的知识,希望对你有一定的参考价值。

先看看效果图:

代码实现主要还是用SWT去实现 自定义一个我这里叫ItemNew,这里可以去控制每一个ItemNew的点击事件,鼠标追踪事件等等,自定义方法,如设置一些显示隐藏或者去进行一些判断,绘制方法将图片以及文字绘制出来,最后一个id我是去根据id去判断需不需要绘制下划线的。

public class ItemNew implements PaintListener 
	private Composite composite;
	private Image image;
	private String title;
	private String id;
	private boolean select = false;

	public ItemNew(Composite parent, Image image, String title, String id) 
		this.title = title;
		this.image = image;
		this.id = id;
		createControl(parent);
	

	public void createControl(Composite parent) 
		composite = new Composite(parent, SWT.NORMAL);
		composite.addPaintListener(this);
		composite.setSize(300, 48);
		composite.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		composite.addMouseTrackListener(mouseTrackListener);
		composite.addMouseListener(new MouseListener() 
			
			@Override
			public void mouseUp(MouseEvent e) 
				// TODO Auto-generated method stub
			
			
			@Override
			public void mouseDown(MouseEvent e) 
				// TODO Auto-generated method stub
			
			
			@Override
			public void mouseDoubleClick(MouseEvent e) 
				// TODO Auto-generated method stub
			
		);
		composite.addDisposeListener(new DisposeListener() 

			@Override
			public void widgetDisposed(DisposeEvent e) 
				// TODO Auto-generated method stub
				onDisposed();
			
		);
	

	protected void onDisposed() 
	
	
	protected void noVisible() 
		composite.setVisible(false);
	
	
	protected void setVisible() 
		composite.setVisible(true);
	
	
	protected boolean isVisible() 
		if (composite.isVisible()) 
			return true;
		
		return false;
	

	private MouseTrackListener mouseTrackListener = new MouseTrackListener() 

		@Override
		public void mouseHover(MouseEvent e) 
		

		@Override
		public void mouseExit(MouseEvent e) 
			composite.setBackground(SWTResourceManager.getColor(255, 255, 255));
		

		@Override
		public void mouseEnter(MouseEvent e) 
			composite.setBackground(SWTResourceManager.getColor(219, 224, 241));
		
	;

	@Override
	public void paintControl(PaintEvent e) 
		GC gc = e.gc;
		gc.setForeground(SWTResourceManager.getColor(230, 232, 237));
		gc.setLineWidth(1);
		if (id != "FALSE") 
			gc.drawLine(12, 47, 287, 47);
		
			
		if (select) 
			gc.setBackground(SWTResourceManager.getColor(219, 224, 241));
			gc.fillRectangle(0, 0, 300, 48);
		
		
		int offsetx = 0;
		int y = 0;
		int width = composite.getSize().x;
		int height = composite.getSize().y;
		if (image != null && !image.isDisposed()) 
			y = (48 - image.getBounds().height) / 2;
			gc.drawImage(image, 12, y);
			offsetx = 12 + image.getBounds().width + 6;
		
		if (!TextUtils.isEmpty(title)) 
			int avalidW = width - offsetx - 50;
			gc.setForeground(UIConstant.DEF_COLOR);
			Font font = SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL);
			gc.setFont(font);
			int realW = gc.stringExtent(title).x + title.length();
			String drawText = title;
			int tempW = 0;
			if (realW > avalidW) 
				for (int i = 0; i < drawText.length(); i++) 
					String tempText = drawText.substring(0, i + 1);
					tempW = gc.stringExtent(tempText).x + tempText.length();
					if (tempW > avalidW) 
						if (tempText.equals(drawText)) 
							break;
						
						drawText = tempText + "...";
						break;
					
				
			
			gc.drawText(drawText, offsetx, y - 2, true);
		
		
		gc.dispose();
	

	public void setSize(int width, int height) 
		composite.setSize(width, height);
	

	public void setLocation(int x, int y) 
		composite.setLocation(x, y);
	

	public Composite getComposite() 
		return composite;
	

	public void setComposite(Composite composite) 
		this.composite = composite;
	

	public Image getImage() 
		return image;
	

	public void setImage(Image image) 
		this.image = image;
	

	public String getTitle() 
		return title;
	

	public void setTitle(String title) 
		this.title = title;
	

	public boolean isSelect() 
		return select;
	

	public void setSelect(boolean select) 
		this.select = select;
	

在你的主类中,可以调用这个类,通过setSelect()这个方法去判断是否有选中,没有则重新绘制,不然会显示2个背景,第一次进去是有一个itemNew 默认选中有背景的。

Image staffImage = SDKGlobal.getContext().getImage(R.drawable.staff);
		staffItem = new ItemNew(secComp, staffImage, "员工", "TRUE");
		staffItem.setLocation(0, 48);
		staffItem.getComposite().addMouseListener(new MouseAdapter() 
			@Override
			public void mouseDown(MouseEvent e) 
				// TODO Auto-generated method stub
				super.mouseDown(e);
				staffItem.setSelect(true);
				if (itemNew != null && itemNew.isSelect() && itemNew != staffItem) 
					itemNew.setSelect(false);
					itemNew.getComposite().redraw();
				
				itemNew = staffItem;
			
		);

		Image outContactsImage = SDKGlobal.getContext().getImage(R.drawable.outContacts);
		outContactsItem = new ItemNew(secComp, outContactsImage, "外部联系人", "TRUE");
		outContactsItem.setLocation(0, 96);
		outContactsItem.getComposite().addMouseListener(new MouseAdapter() 
			@Override
			public void mouseDown(MouseEvent e) 
				// TODO Auto-generated method stub
				super.mouseDown(e);
				outContactsItem.setSelect(true);
				if (itemNew != null && itemNew.isSelect() && itemNew != outContactsItem) 
					itemNew.setSelect(false);
					itemNew.getComposite().redraw();
				
				itemNew = outContactsItem;
			
		);

		Image branchImage = SDKGlobal.getContext().getImage(R.drawable.branch);
		branchItem = new ItemNew(secComp, branchImage, "部门", "FALSE");
		branchItem.setLocation(0, 144);
		branchItem.getComposite().addMouseListener(new MouseAdapter() 
			@Override
			public void mouseDown(MouseEvent e) 
				// TODO Auto-generated method stub
				super.mouseDown(e);
				branchItem.setSelect(true);
				if (itemNew != null && itemNew.isSelect() && itemNew != branchItem) 
					itemNew.setSelect(false);
					itemNew.getComposite().redraw();
				
				itemNew = branchItem;
			
		);

以上是关于PC 自定义ListItem的主要内容,如果未能解决你的问题,请参考以下文章

PC 自定义ListItem

PC 自定义ListItem

tools:listitem 用于扩展 RecyclerView 的自定义视图

如何在WPF中自定义ListBox的ListItem的外观 4-3

Extjs 6.2.0 List 不显示在 IOS 上,默认类型为自定义 ListItem

Qt - 如何从 QListWidget 中获取选定的自定义项目?