RecyclerView加上弧形滾動條

2021-08-17 17:04:50 字數 3851 閱讀 8784

介面主要是通過gridlayoutmanager設定每行顯示兩個item;

mrecyclerview = (myrecyclerview) rootview.findviewbyid(r.id.id_recyclerview_grid);

mmanager = new gridlayoutmanager(getactivity().getbasecontext(), 2);

mrecyclerview.setlayoutmanager(mmanager);

mrecyclerview.setadapter(madapter);

madapter.setonitemclicklistener(this);

madapter.setonitemlongclicklistener(this);

此處就不做介紹了,本文主要是講在recyclerview介面右側加上弧形滾動條
原理:
自定義recyclerview

,繼承recyclerview

,重寫ondraw方法,在ondraw中畫出滾動條,通過adapter中getitemcount()方法獲得總的item數目來決定滾動條移動部分的長度,

優點:
借助recyclerview的重新整理

來重新整理滾動條位置,不用自己去做大量的重新整理,相比於單獨定義view可優化部分效能;

**實現:
public class myrecyclerview extends recyclerview 

public myrecyclerview(context context, attributeset attrs)

/**

*定義畫筆樣式
**/

private void init()

public void clear()

@override

public void draw(canvas arg0)

@override

public void ondraw(canvas canvas)

if (mvisibleitemcount == 0)

log.e(tag,"------mitemheight="+mitemheight+", width = "+width+",height="+height);

int totalheight = mitemheight * (madapter.getitemcount()/mlayoutmanager.getspancount()+madapter.getitemcount()%mlayoutmanager.getspancount())+ topbottompadding;

float offsetangle = 0;

float sweepangle = (height * sweep_angle / totalheight);

if (totalheight != 0) else

rectf rect = new rectf((width - diameter) / 2,

(height - diameter) / 2, (width + diameter) / 2,

(height + diameter) / 2);

canvas.drawcircle(

(float) (width / 2 + diameter/ 2* math.cos((start_angle) * math.pi/ 180)),

(float) (height / 2 + diameter/ 2* math.sin((start_angle) * math.pi/ 180)), mstrokewidth / 2, mbgcirclepaint);

canvas.drawarc(rect, start_angle, sweep_angle, false, mbgarcpaint);

canvas.drawcircle((float) (width / 2 + diameter/ 2* math.cos((start_angle + sweep_angle) * math.pi/ 180)),

(float) (height / 2 + diameter/ 2* math.sin((start_angle + sweep_angle) * math.pi/ 180)), 

mstrokewidth / 2, mbgcirclepaint);

rectf rect2 = new rectf((width - diameter) / 2,

(height - diameter) / 2, (width + diameter) / 2,

(height + diameter) / 2);

log.d(tag, "-------sweepangle=" + sweepangle + ",offsetangle="+ offsetangle);

canvas.drawcircle(

(float) (width / 2 + diameter

/ 2* math.cos((start_angle + offsetangle) * math.pi

/ 180)),

(float) (height / 2 + diameter

/ 2* math.sin((start_angle + offsetangle) * math.pi

/ 180)), mstrokewidth / 2, mcirclepaint);

canvas.drawarc(rect2, start_angle + offsetangle, sweepangle, false,

marcpaint);

canvas.drawcircle(

(float) (width / 2 + diameter

/ 2* math.cos((start_angle + offsetangle+sweepangle) * math.pi

/ 180)),

(float) (height / 2 + diameter

/ 2* math.sin((start_angle + offsetangle+sweepangle) * math.pi

/ 180)), mstrokewidth / 2, mcirclepaint);

} else 

}@override

public void setlayoutmanager(layoutmanager layout)

}

@override

protected void onattachedtowindow()

}runnable mrun = new runnable()

};@override

public boolean ontouchevent(motionevent arg0) else if (action == motionevent.action_up)

return super.ontouchevent(arg0);

}

@override

public void setadapter(adapter adapter)

private recyclerview.onscrolllistener monscrolllistener = new recyclerview.onscrolllistener()

@override

public void onscrolled(recyclerview recyclerview, int dx, int dy)

};}

給RecyclerView加上摺疊的效果

recyclerview有很高的自由度,可以說只有想不到沒有做不到,真是越用越喜歡。這次用超簡單的方法,讓recyclerview帶上摺疊的效果。效果是這樣的。總結一下這個列表的特點,就是以下三點 1.重疊效果 2.層次感 3.首項的差動。下面我們來乙個個解決。我們新建乙個parallaxrecyc...

給 RecyclerView 加上摺疊的效果

recyclerview 有很高的自由度,可以說只有想不到沒有做不到,真是越用越喜歡。這次用超簡單的方法,讓 recyclerview 帶上摺疊的效果。效果是這樣的。總結一下這個列表的特點,就是以下三點 重疊效果 層次感 首項的差動。下面我們來乙個個解決。我們新建乙個 parallaxrecycle...

網路流 加上了當前弧優化的Dinic演算法

測試題目 題目背景 滾粗了的hansbug在收拾舊語文書,然而他發現了什麼奇妙的東西。題目描述 蒟蒻hansbug在一本語文書裡面發現了一本答案,然而他卻明明記得這書應該還包含乙份練習題。然而出現在他眼前的書多得數不勝數,其中有書,有答案,有練習冊。已知乙個完整的書冊均應該包含且僅包含一本書 一本練...