錯(cuò)亂對(duì)稱的瀑布流

現(xiàn)在的界面大同小異一點(diǎn)新意也沒有,最近看到一種布局界面還比較好看舵抹,我試著用自定義view但是太麻煩了肪虎,最終選用recycle來實(shí)現(xiàn)的,先上一波圖大伙看看

說起來實(shí)現(xiàn)起來難度不是很大惧蛹,就是adapter里面計(jì)算圖片的大小尺寸以及每個(gè)圖片對(duì)應(yīng)的位置比較麻煩笋轨,當(dāng)然也怕計(jì)算過多滑動(dòng)會(huì)卡頓,不過還好都是復(fù)用影響不到赊淑,對(duì)每張圖片我還特地自定義ImageView畫了圓角,廢話不多說上代碼

public class StaggerAdapterextends RecyclerView.Adapter {

private Listlists;

? ? private ContextmContext;

//將圖片布局類型一共分為了三類當(dāng)然你們可以接著加喔

? private int type_one =0x001;

? ? private int type_two =0x002;

? ? private int type_three =0x003;

? ? private int mWidth;

? ? private int eachValue;

? ? private int padding =15;

? ? public StaggerAdapter(Context mContext, int mWidth, List lists) {

? ? ? ? this.mContext = mContext;

? ? ? ? this.mWidth = mWidth;

? ? ? ? this.lists = lists;

? ? ? ? int padding_px = DipUtils.dip2px(mContext, padding);

? ? ? ? eachValue = (mWidth - padding_px *3) /3;

? ? ? ? Log.d("===", eachValue +"eachValue");

? ? }

@NonNull

@Override

? ? public RecyclerView.ViewHolderonCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

if (viewType ==type_one) {

View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_one, parent, false);

? ? ? ? ? ? return new ViewHolderOne(inflate);

? ? ? ? }else if (viewType ==type_two) {

View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_two, parent, false);

? ? ? ? ? ? return new ViewHolderTwo(inflate);

? ? ? ? }else {

View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_three, parent, false);

? ? ? ? ? ? return new ViewHolderThree(inflate);

? ? ? ? }

}

@Override

? ? public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

Integer pic1 =0;

? ? ? ? Integer pic2 =0;

? ? ? ? Integer pic3 =0;

? ? ? ? int num=(position+1)/3;

? ? ? ? int currentPosition=num*7;

? ? ? ? if (holderinstanceof ViewHolderOne) {

if (currentPosition <=lists.size()-1) {

pic1 =lists.get(currentPosition);

? ? ? ? ? ? }

if (currentPosition+1 <=lists.size()-1) {

pic2 =lists.get(currentPosition+1);

? ? ? ? ? ? }

if (currentPosition +2 <=lists.size()-1) {

pic3 =lists.get(currentPosition +2);

? ? ? ? ? ? }

ViewHolderOne viewHolderOne = (ViewHolderOne) holder;

? ? ? ? ? ? ViewGroup.LayoutParams layoutParams = viewHolderOne.oneLeft.getLayoutParams();

? ? ? ? ? ? layoutParams.width =eachValue *2;

? ? ? ? ? ? layoutParams.height =eachValue *2;

? ? ? ? ? ? viewHolderOne.oneLeft.setLayoutParams(layoutParams);

? ? ? ? ? ? viewHolderOne.oneLeft.requestLayout();

? ? ? ? ? ? if (pic1 !=0) {

viewHolderOne.oneLeft.setImageResource(pic1);

? ? ? ? ? ? }else {

viewHolderOne.oneLeft.setImageResource(0);

? ? ? ? ? ? }

ViewGroup.LayoutParams right_layout_p = viewHolderOne.oneRightLayout.getLayoutParams();

? ? ? ? ? ? right_layout_p.height =eachValue *2;

? ? ? ? ? ? viewHolderOne.oneRightLayout.setLayoutParams(right_layout_p);

? ? ? ? ? ? viewHolderOne.oneRightLayout.requestLayout();

? ? ? ? ? ? if (pic2 !=0) {

viewHolderOne.oneRight1.setImageResource(pic2);

? ? ? ? ? ? }else {

viewHolderOne.oneRight1.setImageResource(0);

? ? ? ? ? ? }

if (pic3 !=0) {

viewHolderOne.oneRight2.setImageResource(pic3);

? ? ? ? ? ? }else {

viewHolderOne.oneRight2.setImageResource(0);

? ? ? ? ? ? }

}else if (holderinstanceof ViewHolderTwo){

if (currentPosition+3<=lists.size()-1) {

pic1 =lists.get(currentPosition+3);

? ? ? ? ? ? }

if (currentPosition+4 <=lists.size()-1) {

pic2 =lists.get(currentPosition+4);

? ? ? ? ? ? }

if (currentPosition +5 <=lists.size()-1) {

pic3 =lists.get(currentPosition +5);

? ? ? ? ? ? }

ViewHolderTwo viewHolderTwo = (ViewHolderTwo) holder;

? ? ? ? ? ? ViewGroup.LayoutParams layoutParams = viewHolderTwo.twoLeftLayout.getLayoutParams();

? ? ? ? ? ? layoutParams.width =eachValue;

? ? ? ? ? ? layoutParams.height =eachValue *2;

? ? ? ? ? ? viewHolderTwo.twoLeftLayout.setLayoutParams(layoutParams);

? ? ? ? ? ? viewHolderTwo.twoLeftLayout.requestLayout();

? ? ? ? ? ? viewHolderTwo.twoLeft1.setImageResource(pic1);

? ? ? ? ? ? viewHolderTwo.twoLeft2.setImageResource(pic2);

? ? ? ? ? ? ViewGroup.LayoutParams two_right_lp = viewHolderTwo.twoRight.getLayoutParams();

? ? ? ? ? ? two_right_lp.height =eachValue *2;

? ? ? ? ? ? viewHolderTwo.twoRight.setLayoutParams(two_right_lp);

? ? ? ? ? ? viewHolderTwo.twoRight.requestLayout();

? ? ? ? ? ? viewHolderTwo.twoRight.setImageResource(pic3);

? ? ? ? }else {

ViewHolderThree viewHolderThree= (ViewHolderThree) holder;

? ? ? ? ? ? ViewGroup.LayoutParams item_three_lp = viewHolderThree.itemTwoIv.getLayoutParams();

? ? ? ? ? ? item_three_lp.height=eachValue;

? ? ? ? ? ? viewHolderThree.itemTwoIv.setLayoutParams(item_three_lp);

? ? ? ? ? ? viewHolderThree.itemTwoIv.requestLayout();

? ? ? ? ? ? viewHolderThree.itemTwoIv.setImageResource(lists.get(currentPosition-1));

? ? ? ? }

}

@Override

? ? public int getItemCount() {

int out_num =lists.size() /7;

? ? ? ? int last_num =lists.size() %7;

? ? ? ? int int_num = (int) Math.ceil((double) last_num /3);

? ? ? ? //? Log.d("===",(int) Math.ceil((double)lists.size()/3)+"count");

? ? ? ? return out_num *3 + int_num;

? ? }

@Override

? ? public int getItemViewType(int position) {

int i = position %3;

? ? ? ? if (i ==0) {

return type_one;

? ? ? ? }else if (i ==1) {

return type_two;

? ? ? ? }else {

return type_three;

? ? ? ? }

}

static class ViewHolderOneextends RecyclerView.ViewHolder {

@Bind(R.id.one_left)

ImageViewoneLeft;

? ? ? ? @Bind(R.id.one_right1)

ImageViewoneRight1;

? ? ? ? @Bind(R.id.one_right2)

ImageViewoneRight2;

? ? ? ? @Bind(R.id.one_rightLayout)

LinearLayoutoneRightLayout;

? ? ? ? ViewHolderOne(View view) {

super(view);

? ? ? ? ? ? ButterKnife.bind(this, view);

? ? ? ? }

}

static class ViewHolderTwoextends RecyclerView.ViewHolder {

@Bind(R.id.two_left1)

ImageViewtwoLeft1;

? ? ? ? @Bind(R.id.two_left2)

ImageViewtwoLeft2;

? ? ? ? @Bind(R.id.two_leftLayout)

LinearLayouttwoLeftLayout;

? ? ? ? @Bind(R.id.two_right)

ImageViewtwoRight;

? ? ? ? ViewHolderTwo(View view) {

super(view);

? ? ? ? ? ? ButterKnife.bind(this, view);

? ? ? ? }

}

static class ViewHolderThreeextends RecyclerView.ViewHolder {

@Bind(R.id.item_twoIv)

ImageViewitemTwoIv;

? ? ? ? ViewHolderThree(View view) {

super(view);

? ? ? ? ? ? ButterKnife.bind(this, view);

? ? ? ? }

}

好了今天的擼碼結(jié)束 覺得可以的老鐵點(diǎn)個(gè)贊唄

源碼下載 請(qǐng)點(diǎn)擊這里

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末仅讽,一起剝皮案震驚了整個(gè)濱河市陶缺,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌洁灵,老刑警劉巖饱岸,帶你破解...
    沈念sama閱讀 217,907評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異徽千,居然都是意外死亡苫费,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門双抽,熙熙樓的掌柜王于貴愁眉苦臉地迎上來百框,“玉大人,你說我怎么就攤上這事牍汹☆砦” “怎么了?”我有些...
    開封第一講書人閱讀 164,298評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵慎菲,是天一觀的道長嫁蛇。 經(jīng)常有香客問我,道長露该,這世上最難降的妖魔是什么睬棚? 我笑而不...
    開封第一講書人閱讀 58,586評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上抑党,老公的妹妹穿的比我還像新娘包警。我一直安慰自己,他們只是感情好新荤,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,633評(píng)論 6 392
  • 文/花漫 我一把揭開白布揽趾。 她就那樣靜靜地躺著,像睡著了一般苛骨。 火紅的嫁衣襯著肌膚如雪篱瞎。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,488評(píng)論 1 302
  • 那天痒芝,我揣著相機(jī)與錄音俐筋,去河邊找鬼。 笑死严衬,一個(gè)胖子當(dāng)著我的面吹牛澄者,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播请琳,決...
    沈念sama閱讀 40,275評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼粱挡,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼!你這毒婦竟也來了俄精?” 一聲冷哼從身側(cè)響起询筏,我...
    開封第一講書人閱讀 39,176評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎竖慧,沒想到半個(gè)月后嫌套,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,619評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡圾旨,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,819評(píng)論 3 336
  • 正文 我和宋清朗相戀三年踱讨,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片砍的。...
    茶點(diǎn)故事閱讀 39,932評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡痹筛,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出廓鞠,到底是詐尸還是另有隱情味混,我是刑警寧澤,帶...
    沈念sama閱讀 35,655評(píng)論 5 346
  • 正文 年R本政府宣布诫惭,位于F島的核電站翁锡,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏夕土。R本人自食惡果不足惜馆衔,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,265評(píng)論 3 329
  • 文/蒙蒙 一瘟判、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧角溃,春花似錦拷获、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,871評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至未蝌,卻和暖如春驮吱,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背萧吠。 一陣腳步聲響...
    開封第一講書人閱讀 32,994評(píng)論 1 269
  • 我被黑心中介騙來泰國打工左冬, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人纸型。 一個(gè)月前我還...
    沈念sama閱讀 48,095評(píng)論 3 370
  • 正文 我出身青樓拇砰,卻偏偏與公主長得像,于是被迫代替她去往敵國和親狰腌。 傳聞我的和親對(duì)象是個(gè)殘疾皇子除破,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,884評(píng)論 2 354