2017-09-23 126 views
1

我的數據集有一個元素userId。我想檢查頭寸userId是否等於userId之前的頭寸,然後TextView設置爲GONE如何根據recyclerview Android的位置動態地更改佈局中的元素?

我在onBindViewHolder到目前爲止已經試過:

final Item item = itemsArray.get(position); 

if(position - 1 > -1){ 
    if(items.get(position-1).getUserId() == item.getUserId()) { 
     holder.username.setVisibility(View.GONE); 
    } 
} 

,但最終,一些具有相同userId他們username該項目是gone,但他們中的一些,其userId不相同與前位置的userIdusername也是gone

爲了說清楚,我附上了一些圖片,清楚地顯示出我想要的。

這就是我想要的東西:

enter image description here

但最終,它變成像下面的圖片,你看到item不同userId,該username也`不見了。

enter image description here

所以,我的問題是,我怎麼能檢查前一個項目的userId是一樣的當前位置的userId,所以它不會落得我附着在第二圖像。

如果有其他解決方案,請讓我知道。提前感謝。

+0

是您的userId是字符串類型? –

+0

是'int'類bro – ken

+0

嘗試添加其他條件邏輯爲「holder.username」 –

回答

1

使用此代碼

final Item item = itemsArray.get(position); 
holder.username.setVisibility(View.VISIBLE); 
if(position - 1 > -1){ 
    if(items.get(position-1).getUserId() == item.getUserId()) { 
    holder.username.setVisibility(View.GONE); 
    }else{ 
    holder.username.setVisibility(View.VISIBLE); 
    } 
} 

當您使用RecyclerView使一些人認爲這是「水漲船高」 holder.username狀態被重用。

+0

它的工作原理你可以更新答案?因爲你沒有添加其他邏輯的答案 – ken

+0

此代碼也將工作。覈實。如果我的回答是正確的,請接受它 –

+0

它與我所附的相同。 – ken

0

在適配器中掃描持有者並且稍後改變它是錯誤的。 在設置適配器中的數據之前,您應該準備好數據。更改您的數據模型(例如,添加字段boolean isShowNameItem類)