【gitee源码怎么部署】【松子直装源码】【坦克游戏编程源码】layoutmanager源码
1.å¦ä½ç¨SwingBuilder使ç¨èªå®ä¹çswingç»ä»¶
2.RecyclerView详解
3.è°è°RecyclerViewä¸çç¼å
å¦ä½ç¨SwingBuilder使ç¨èªå®ä¹çswingç»ä»¶
groovyå·²ç»å¨SwingBuilderé为æ们é¢è®¾äºå¾å¤swingç»ä»¶ï¼ä»¥ä½¿æ们å¯ä»¥ç¨ä¸é¢è¿æ ·çå¿«æ·å½¢å¼æ¥æ建çé¢ï¼
swingBuilder.frame(
//å±æ§é
){
//åç»ä»¶
}
对äºä» ä» ä½¿ç¨swingçåçç»ä»¶ï¼è¿æ²¡æä»»ä½é®é¢ï¼ä½å¨å®é å¼åä¸å¯è½ä¼éå°éè¦ä½¿ç¨å¯¹åçç»ä»¶æ©å±åçèªå®ä¹ç»ä»¶ï¼å¦ä¸ä¸ªç»§æ¿äºJPanelçMyPanelç±»ï¼çæ åµï¼èè¿ä¸ªç±»ææ¾ä¸å¯è½åºç°å¨SwingBuilderçé¢è®¾å表éï¼é£è¿ç§æ åµä¸æ¯ä¸æ¯å°±æå³çä¸å¯ä»¥ä½¿ç¨groovy为æ们æä¾ç便å©ï¼åªè½ç¨åä¼ ç»çjavaå½¢å¼å»æ建çé¢å¢ï¼
çæ¡æ¯å¦å®çãSwingBuilderè½ç¶ä¸ä¼é¢è®¾ç¨æ·èªå®ä¹çç»ä»¶ï¼ä½å®æä¾äºå 个æ¥å£å¯ä»¥è®©ç¨æ·æèªå®ä¹çç»ä»¶è®¾ç½®è¿å»ï¼è¿æ ·å°±ä¾ç¶å¯ä»¥ç¨groovyçå¿«æ·å½¢å¼æ¥æ建çé¢äºãè¿å 个æ¥å£åå«æ¯ï¼
public void registerFactory(String name,gitee源码怎么部署 Factory factory)
public void registerBeanFactory(String theName, Class beanClass)
registerFactory()
registerFactoryæ¹æ³å¯ä»¥æ³¨åä¸ä¸ªèªå®ä¹ç»ä»¶ï¼nameæå®äºéè¿swingBuilderæ建ç»ä»¶çå称ï¼å¦JFrameå¨swingBuilderéé¢è®¾çnameæ¯"frame"ï¼è¿å°±ä½¿å¾æ们å¯ä»¥éè¿swingBuilder.frame()æ建ä¸ä¸ªJFrameã
第äºä¸ªåæ°æ¥æ¶ä¸ä¸ªå®ç°äºFactoryæ¥å£çç±»ï¼è¿ä¸ªç±»éææï¼å®ç°ï¼å¦ä½å®ä¾åèªå®ä¹ç»ä»¶ï¼newInstanceï¼ï¼å¦ä½å¤çaddè¿æ¥çåç»ä»¶ï¼setChildï¼çä¸ç³»åçç¥ãé常并ä¸éè¦ç´æ¥å®ç°Factoryæ¥å£ï¼groovy为æ们æä¾äºä¸ä¸ªæ½è±¡ç±»ï¼AbstractFactoryï¼ï¼æ们åºä¼å 继æ¿è¿ä¸ªç±»ï¼ç¶ååæéæç¸åºçæ¹æ³è¿è¡éåãä¸é¢æ¥çä¸ä¸ªå¦ä½ä½¿ç¨è¿ä¸ªæ¥å£çä¾åï¼
/
*** èªå®ä¹äºä¸ä¸ªå¯ä»¥æ¾ç¤ºèæ¯å¾ççé¢æ¿
* @author keenlight
*
*/
class ImagePanel extends JPanel{
/
***
*/
private static final long serialVersionUID = 1L
private BufferedImage image
public ImagePanel(BufferedImage image){
super()
this.image = image
}
public ImagePanel(BufferedImage image, LayoutManager layout) {
super(layout)
this.image = image
}
public void paintComponent(Graphics g)
{
super.paintComponent(g)
if(image != null){
g.drawImage(image, 0, 0, this)
}
}
}
/
*** ImagePanelçFactory
* @author keenlight
*/
class ImagePanelFactory extends AbstractFactory{
private BufferedImage image
ImagePanelFactory(BufferedImage image){
this.image = image
}
@Override
public Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map properties) throws InstantiationException, IllegalAccessException {
def layout = properties.remove("layout")
new ImagePanel(image, layout)
}
public void setChild(FactoryBuilderSupport builder, Object parent, Object child) {
if (!(child instanceof Component) || (child instanceof Window)) {
return
}
parent = parent as ImagePanel
try {
def constraints = builder.context.constraints
if (constraints != null) {
parent.add(child, constraints)
if (child instanceof JComponent) {
child.putClientProperty(LayoutFactory.DEFAULT_DELEGATE_PROPERTY_CONSTRAINT, constraints)
}
builder.context.remove('constraints')
} else {
parent.add(child)
}
} catch (MissingPropertyException mpe) {
parent.add(child)
}
}
}
å®ä¹å¥½è¿äºç±»åå¦ä½ä½¿å ¶çæï¼åªéä¸æ¥ï¼
def bgImage = ImageIO.read(new File(image_path))
swingBuilder.registerFactory("imagePanel", new ImagePanelFactory(bgImage))
注å好ä¹åï¼ImagePanelç»ä»¶ä¾¿å¯åå ¶ä»åçç»ä»¶ä¸æ ·ä½¿ç¨äºï¼å¦ä¸æçJFrameä¸æ ·ï¼ã
registerBeanFactory()
registerBeanFactoryæ¯registerFactoryçä¸ç§ä¾¿å©å½¢å¼ï¼è°ç¨æ¤æ¥å£æ éæä¾Factoryï¼åªéæä¾èªå®ä¹ç±»çç±»åå³å¯ãæ¯å¦åçç»ä»¶éçJPanelå°±æ¯ç¨è¿ä¸ªæ¥å£æ³¨åçï¼SwingBuilderç±»ä¸æ³¨åJPanelçæºç å¦ä¸ï¼
registerBeanFactory("panel", JPanel)
è¿æ ·å°±å¯ä»¥æJPanel注å为"panel"æ¥ä½¿ç¨äºãå ¶å®registerBeanFactoryè¿ä¸ªæ¹æ³éé¢ä¸æ ·ä¹å®ä¾åäºä¸ä¸ªç»§æ¿äºAbstractFactoryç±»ï¼å®ä¼æè°ç¨è¿ä¸ªæ¹æ³æ³¨åçç»ä»¶å½åä¸ä¸ªjavaBeanï¼æ¹æ³åå·²ç»å¾ææ¾äºï¼ï¼æ以å®å¨ç»§æ¿AbstractFactoryç±»æ¶éåçnewInstanceæ¹æ³éæ¯ç´æ¥è¿åè°ç¨class.newInstance()çå®ä¾åç»æï¼classç±ç¬¬äºä¸ªå ¥åæä¾ãæ以对äºé£äºå¨å®ä¾åæ¶è°ç¨é»è®¤æé å¨å°±è¶³å¤çç»ä»¶ï¼ç¨registerBeanFactory()æ¹æ³æ¥æ³¨åä¼æ´ä¸ºä¾¿å©ãå½ç¶ï¼å¯¹äºæä¸é¢å®ä¹çImagePanelæ¥è¯´å°±ä¸è¡äºï¼å 为ImagePanelç±»å¨å®ä¾åæ¶éè¦æ¥åä¸ä¸ªå¾ç对象ï¼æä»¥å¿ é¡»ä½¿ç¨ç¥éº»ç¦ä¸ç¹çregisterFactory()ã
RecyclerView详解
RecyclerViewä½ä¸ºListViewåGridViewçæ¿ä»£ï¼ä½æ¯åListViewä¸ä¸æ ·çæ¯ï¼RecyclerViewä¸åè´è´£Itemçææ¾çæ¾ç¤ºæ¹é¢çåè½ï¼ææåå¸å±ãç»å¶çæ¹é¢çå·¥ä½é½æåæä¸åçç±»è¿è¡ç®¡çã
RecyclerViewä¸ListViewçä¸åç¹ï¼ä¸»è¦å¨äºä»¥ä¸å 个ç¹æ§ï¼
å¦æä½ æ³ä½¿ç¨RecyclerViewï¼éè¦å以ä¸æä½ï¼
æ们å¯ä»¥ä»ä¸å¾æ´ç´è§çäºè§£å°RecyclerViewçåºæ¬ç»æï¼
RecyclerViewä¸åè´è´£Itemè§å¾çå¸å±åæ¾ç¤ºï¼æ以RecyclerViewä¹æ²¡æ为Itemå¼æ¾OnItemClickçç¹å»äºä»¶ãå¯ä»¥éè¿ä»¥ä¸æ¹å¼è¿è¡ï¼å 为å¨ViewHolderæ们å¯ä»¥æ¿å°æ¯ä¸ªItemçæ ¹å¸å±ï¼æ以å¦ææä»¬ä¸ºæ ¹å¸å±è®¾ç½®åç¬çOnClickçå¬å¹¶å°å ¶å¼æ¾ç»Adapterï¼é£ä¹å°±å¯ä»¥å¨ç»è£ RecyclerViewæ¶å°±è½å¤è®¾ç½®ItemClickListenerï¼åªä¸è¿è¿ä¸ªListenerä¸æ¯è®¾ç½®å°RecyclerViewä¸èæ¯è®¾ç½®å°Adapterã
å¤Itemå¸å±ï¼getItemViewTypeæ¹æ³ï¼ç¨æ³åListView没æä»»ä½åºå«ï¼è¿éè¦æ³¨æçæ¯å½æ°onCreateViewHolder(ViewGroup parent, int viewType)ç第äºä¸ªåæ°å°±æ¯Viewçç±»åï¼å¯ä»¥æ ¹æ®è¿ä¸ªç±»åå¤æå»å建ä¸åitemçViewHolderï¼ä»èå®æå¤Itemå¸å±ã
å¨RecylerViewä¸ï¼Adapteræ®æ¼ç两个è§è²ï¼ä¸æ¯æ ¹æ®ä¸åviewTypeå建ä¸ä¹ç¸åºççitemViewï¼äºæ¯è®¿é®æ°æ®éå并å°æ°æ®ç»å®å°æ£ç¡®çViewä¸ãè¿å°±éè¦æ们å®ç°ä»¥ä¸ä¸¤ä¸ªå½æ°ï¼
å¦å¤æ们è¿éè¦éåå¦ä¸ä¸ªæ¹æ³ï¼åListView-Adapteré£æ ·ï¼åæ ·å°åè¯RecyclerView-Adapterå表Itemsçæ»æ°ï¼
ViewHolderæè¿°RecylerViewä¸æ个ä½ç½®çitemViewåå æ°æ®ä¿¡æ¯ï¼å±äºAdapterçä¸é¨åï¼å ¶å®ç°ç±»é常ç¨äºä¿åfindViewByIdçç»æã 主è¦å ç´ ç»ææï¼
å ³äºViewHolderï¼è¿é主è¦ä»ç»mFlagsï¼
FLAG_BOUNDââViewHolderå·²ç»ç»å®å°æ个ä½ç½®ï¼mPositionãmItemIdãmItemViewTypeé½ææ
FLAG_UPDATEââViewHolderç»å®çView对åºçæ°æ®è¿æ¶éè¦éæ°ç»å®ï¼mPositionãmItemIdè¿æ¯ä¸è´ç
FLAG_INVALIDââViewHolderç»å®çView对åºçæ°æ®æ æï¼éè¦å®å ¨éæ°ç»å®ä¸åçæ°æ®
FLAG_REMOVEDââViewHolder对åºçæ°æ®å·²ç»ä»æ°æ®é移é¤
FLAG_NOT_RECYCLABLEââViewHolderä¸è½å¤ç¨
FLAG_RETURNED_FROM_SCRAPââè¿ä¸ªç¶æçViewHolderä¼å å°scrap list被å¤ç¨ã
FLAG_CHANGEDââViewHolderå 容åçååï¼é常ç¨äºè¡¨ææItemAnimatorå¨ç»
FLAG_IGNOREââViewHolderå®å ¨ç±LayoutManager管çï¼ä¸è½å¤ç¨
FLAG_TMP_DETACHEDââViewHolderä»ç¶RecyclerView临æ¶å离çæ å¿ï¼ä¾¿äºåç»ç§»é¤ææ·»å åæ¥
FLAG_ADAPTER_POSITION_UNKNOWNââViewHolderä¸ç¥é对åºçAdapterçä½ç½®ï¼ç´å°ç»å®å°ä¸ä¸ªæ°ä½ç½®
FLAG_ADAPTER_FULLUPDATEââæ¹æ³addChangePayload(null)è°ç¨æ¶è®¾ç½®
LayoutManager主è¦ä½ç¨æ¯ï¼æµéåææ¾RecyclerViewä¸itemViewï¼ä»¥åå½itemView对ç¨æ·ä¸å¯è§æ¶å¾ªç¯å¤ç¨å¤çã
å½æ们æ³å¨æäºitemä¸å ä¸äºç¹æ®çUIæ¶ï¼å¾å¾é½æ¯å¨itemViewä¸å å¸å±å¥½ï¼ç¶åéè¿è®¾ç½®å¯è§æ§æ¥å³å®åªäºä½ç½®æ¾ç¤ºä¸æ¾ç¤ºãRecyclerViewå°itemViewåè£ é¥°UIåéå¼æ¥ï¼è£ 饰UIå³ItemDecorationï¼ä¸»è¦ç¨äºç»å¶itemé´çåå²çº¿ãé«äº®æè marginçãå ¶æºç å¦ä¸ï¼
è¿å»AdapterViewçitem项æä½å¾å¾æ¯æ²¡æå¨ç»çãç°å¨RecyclerViewçItemAnimator使å¾itemçå¨ç»å®ç°åå¾ç®åèæ ·å¼ä¸°å¯ï¼æ们å¯ä»¥èªå®ä¹item项ä¸åæä½ï¼å¦æ·»å ï¼å é¤ï¼çå¨ç»ææã
Recyclerç¨äºç®¡çå·²ç»åºå¼æä¸RecyclerViewå离çï¼scrapped or detachedï¼item viewï¼ä¾¿äºéç¨ãScrapped viewæä¾éäºRecyclerViewï¼ä½è¢«æ 记为å¯ç§»é¤æå¯å¤ç¨çviewã
LayoutManagerè·åAdapteræä¸é¡¹çViewæ¶ä¼ä½¿ç¨Recyclerãå½å¤ç¨çViewææï¼cleanï¼æ¶ï¼Viewè½ç´æ¥è¢«å¤ç¨ï¼åä¹è¥View失æï¼dirtyï¼æ¶ï¼éè¦éæ°ç»å®Viewã对äºææçViewï¼å¦æä¸ä¸»å¨è°ç¨request layoutï¼åä¸éè¦éæ°æµé大å°å°±è½å¤ç¨ãå¨åæRecyclerçå¤ç¨åçä¹åï¼æ们å äºè§£ä¸å¦ä¸ä¸¤ä¸ªç±»ï¼
RecyclerViewPoolç¨äºå¤ä¸ªRecyclerViewä¹é´å ±äº«Viewãåªéè¦å建ä¸ä¸ªRecyclerViewPoolå®ä¾ï¼ç¶åè°ç¨RecyclerViewçsetRecycledViewPool(RecycledViewPool)æ¹æ³å³å¯ãRecyclerViewé»è®¤ä¼å建ä¸ä¸ªRecyclerViewPoolå®ä¾ã
éè¿æºç æ们å¯ä»¥çåºmScrapæ¯ä¸ä¸ª<viewType, List>çæ å°ï¼ mMaxScrap æ¯ä¸ä¸ª<viewType, maxNum>çæ å°ï¼è¿ä¸¤ä¸ªæååé代表å¯å¤ç¨Viewæ± çåºæ¬ä¿¡æ¯ãè°ç¨ setMaxRecycledViews(int viewType, int max) æ¶ï¼å½ç¨äºå¤ç¨ç mScrap ä¸viewType对åºçViewHolder个æ°è¶ è¿maxNumæ¶ï¼ä¼ä»å表æ«å°¾å¼å§ä¸¢å¼è¶ è¿çé¨åãè°ç¨ getRecycledView(int viewType) æ¹æ³æ¶ä» mScrap ä¸ç§»é¤å¹¶è¿åviewType对åºçListçæ«å°¾é¡¹ã
ViewCacheExtensionæ¯ä¸ä¸ªç±å¼åè æ§å¶çå¯ä»¥ä½ä¸ºViewç¼åç帮å©ç±»ãè°ç¨Recycler.getViewForPosition(int)æ¹æ³è·åViewæ¶ï¼Recyclerå æ£æ¥attached scrapåä¸çº§ç¼åï¼å¦æ没æåæ£æ¥ViewCacheExtension.getViewForPositionAndType(Recycler, int, int)ï¼å¦æ没æåæ£æ¥RecyclerViewPoolã注æï¼Recyclerä¸ä¼å¨è¿ä¸ªç±»ä¸åç¼åViewçæä½ï¼æ¯å¦ç¼åViewå®å ¨ç±å¼åè æ§å¶ã
ç°å¨å¤§å®¶çæäºRecyclerViewPoolåViewCacheExtensionçä½ç¨åï¼ä¸é¢å¼å§ä»ç»Recyclerã å¦ä¸æ¯Recyclerçå ä¸ªå ³é®æååéåæ¹æ³ï¼
è·åæ个ä½ç½®éè¦å±ç¤ºçViewï¼å æ£æ¥æ¯å¦æå¯å¤ç¨çViewï¼æ²¡æåå建æ°View并è¿åãå ·ä½è¿ç¨ä¸ºï¼
注ï¼ä»¥ä¸æ¯æ¥å¹é è¿ç¨é½å¯ä»¥å¹é positionæitemIdï¼å¦ææstableIdï¼ã
è°è°RecyclerViewä¸çç¼å
Androidæ·±å ¥ç解RecyclerViewçç¼åæºå¶
RecyclerViewå¨é¡¹ç®ä¸ç使ç¨å·²ç»å¾æ®éäºï¼å¯ä»¥è¯´æ¯é¡¹ç®ä¸æé«é¢ä½¿ç¨çä¸ä¸ªæ§ä»¶äºãé¤äºå¸å±çµæ´»æ§ã丰å¯çå¨ç»ï¼RecyclerViewè¿æä¼ç§çç¼åæºå¶ï¼æ¬æå°è¯éè¿æºç æ·±å ¥äºè§£ä¸ä¸RecyclerViewä¸çç¼åæºå¶ã
RecyclerViewåæ§è½ä¼åè¦è¯´å¤æä¹å¤æï¼æ¯å¦è¯´å¸å±ä¼åï¼ç¼åï¼é¢å è½½ççãå ¶ä¼åçç¹å¾å¤ï¼å¨è¿äºçä¼¼ç¬ç«çç¹ä¹é´ï¼å ¶å®åå¨ä¸ä¸ªæ¢çº½ï¼Adapterãå 为ææçViewHolderçå建åå 容çç»å®é½éè¦ç»è¿Adaperç两个å½æ°onCreateViewHolderåonBindViewHolderã
å æ¤æ们æ§è½ä¼åçæ¬è´¨å°±æ¯è¦åå°è¿ä¸¤ä¸ªå½æ°çè°ç¨æ¶é´åè°ç¨ç次æ°ãå¦ææ们æ³å¯¹RecyclerViewåæ§è½ä¼åï¼å¿ é¡»æ¸ æ¥çäºè§£å°æ们çæ¯ä¸æ¥æä½èåï¼onCreateViewHolderåonBindViewHolderè°ç¨äºå¤å°æ¬¡ãå æ¤ï¼äºè§£RecyclerViewçç¼åæºå¶æ¯RecyclerViewæ§è½ä¼åçåºç¡ã
为äºç解ç¼åçåºç¨åºæ¯ï¼æ¬æé¦å ä¼ç®åä»ç»ä¸ä¸RecyclerViewçç»å¶åçï¼ç¶åååæå ¶ç¼åå®ç°åçã
RecyclerViewæ»å¨æ¶ä¼è§¦åonTouchEvent#onMoveï¼åæ¶åå¤ç¨ViewHolderå¨è¿éå°±ä¼å¼å§ãæ们ç¥é设置RecyclerViewæ¶éè¦è®¾ç½®LayoutManager,LayoutManagerè´è´£RecyclerViewçå¸å±ï¼å å«å¯¹ItemViewçè·åä¸å¤ç¨ã以LinearLayoutManager为ä¾ï¼å½RecyclerViewéæ°å¸å±æ¶ä¼ä¾æ¬¡æ§è¡ä¸é¢å 个æ¹æ³ï¼
ä¸è¿°çæ´ä¸ªè°ç¨é¾ï¼onLayoutChildren()->fill()->layoutChunk()->next()->getViewForPosition(),getViewForPosition()å³æ¯æ¯ä»RecyclerViewçåæ¶æºå¶å®ç°ç±»Recyclerä¸è·ååéçViewï¼ä¸é¢ä¸»è¦å°±æ¥ä»çè¿ä¸ªRecycler#getViewForPosition()çå®ç°ã
ä¸è¿°é»è¾ç¨æµç¨å¾è¡¨ç¤ºï¼
RecyclerViewå¨Recyleréé¢å®ç°ViewHolderçç¼åï¼Recycleréé¢çå®ç°ç¼åç主è¦å å«ä»¥ä¸5个对象ï¼
public final class Recycler {
final ArrayList mAttachedScrap = new ArrayList<>();
ArrayList mChangedScrap = null;
RecyclerViewå¨è®¾è®¡çæ¶å讲ä¸è¿°5个ç¼å对象å为äº3级ãæ¯æ¬¡å建ViewHolderçæ¶åï¼ä¼æç §ä¼å 级ä¾æ¬¡æ¥è¯¢ç¼åå建ViewHolderãæ¯æ¬¡è®²ViewHolderç¼åå°Recyclerç¼åçæ¶åï¼ä¹ä¼æç §ä¼å 级ä¾æ¬¡ç¼åè¿å»ãä¸çº§ç¼ååå«æ¯ï¼
使ç¨èªå®ä¹ViewCacheExtensionåï¼view离å±åååæ¥ä¸ä¼èµ°onBindViewHolder()æ¹æ³ã
holder.setIsRecyclable(false)ï¼è¿æ ·çè¯æ¯æ¬¡é½ä¼èµ°onCreateViewHolder()åonBindViewHolder()æ¹æ³
1.æååå§åviewHolderï¼æ¾å°ç¼åæ± ä¸
viewPool.putRecycledView(adapter.onCreateViewHolder(recyclerView, 1))
2.æååå§åviewï¼å¨onCreateViewHolderçæ¶åå»åview
3.èªå®ä¹ViewCacheExtension
4.éå½çå¢å cacheSize
4.å ¬ç¨ç¼åæ± ï¼æ¯å¦å¤ä¸ªviewPager+fragmentåºæ¯ä½¿ç¨ï¼æè å ¨å±åå©ç¼åæ± ï¼æè§ç¨æ·ä¸å¤§ã
æ2ä¸åæ³æå¼
第ä¸ç§
第äºç§
ä¸ä¼ï¼å 为prefetchï¼GapWorkerä¸çä¸ä¸ªæ¹æ³ï¼ä¹åmViewCacheMaxä¼åæmRequestedCacheMax + extraCache
æ2ç§æ¹å¼å¯ä»¥è®©ç¼å失æ
第ä¸ç§
recyclerView.setItemViewCacheSize(-1)
第äºç§
recyclerView.setItemViewCacheSize(0)
layoutManager.isItemPrefetchEnabled = false
设置ä¸ç¼ååï¼æ¥åæ»å¨è®©viewè¿å ¥å±å¹ç¦»å¼å±å¹ï¼viewHolderçitemæ¶ä¼å¤æ¬¡èµ°onBindViewHolder()æ¹æ³ã