2016-09-29 108 views
2

我正在使用RecyclerView其中我有RelativeLayout。在那RelativeLayout,我有一個2 TextView。如果第一個TextView具有某個值,該值決定第二個TextView應該向左還是向右對齊。以編程方式設置佈局參數recyclerview的項目

我曾嘗試使用下面的代碼設置LayoutParamsRecyclerView適配器:

if (listItem.getmLikeCount() > 0) { 
    RelativeLayout.LayoutParams feedCommentParams = new RelativeLayout.LayoutParams(
     RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
    feedCommentParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); 
    listItemHolder.mCommentCount.setLayoutParams(feedCommentParams); 
} else { 
    listItemHolder.mLikeCount.setVisibility(View.GONE); 
    RelativeLayout.LayoutParams feedCommentParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
    feedCommentParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0); 
    listItemHolder.mCommentCount.setLayoutParams(feedCommentParams); 
} 

但項目是越來越如果有任何RecyclerView項目是沒有任何count,那麼所有其他項目也對準即回收左邊。

我想顯示評論數到右如果像count > 0其他顯示評論數左。

+0

如果要求這個小傢伙,那麼爲什麼你正在嘗試做它在代碼中,使用Android:layout_alignWithParentIfMissing =在XML「真」,它將從XML只 – Vickyexpert

+0

管理或偷懶,只是創建兩個不同的「TextViews '分開對齊。 – Alpha

回答

2

這個問題很容易解決,兩個TextView分開對齊。因此,根據評論數量,您可以將其可見性設置爲GONEVISIBLE

無論如何,在你的情況下,你可能會考慮在這樣的if和else語句中調用requestLayout

if (listItem.getmLikeCount() > 0) { 
    RelativeLayout.LayoutParams feedCommentParams = new RelativeLayout.LayoutParams(
     RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
    feedCommentParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); 
    listItemHolder.mCommentCount.setLayoutParams(feedCommentParams); 

    // Call requestLayout here 

} else { 
    listItemHolder.mLikeCount.setVisibility(View.GONE); 
    RelativeLayout.LayoutParams feedCommentParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
    feedCommentParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0); 
    listItemHolder.mCommentCount.setLayoutParams(feedCommentParams); 

    // Call requestLayout here for the else part. 

} 
+0

謝謝你的回答,你能告訴我我應該寫些什麼來代替//在上面代碼中的else部分調用requestLayout。 –

相關問題