[英]How to add horizontal scroll card view in recyclerview in Bottom navigation drawer
[英]How to add horizontal scroll on vertical recyclerview?
將 HorizontalScrollView 與 LinearLayout 一起用作子項。 將它的 Orientation 設置為水平並將您的動態視圖添加到它。
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<LinearLayout
android:id="@+id/ll_main"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent">
// Add your dynamic views to this layout.
</LinearLayout>
</HorizontalScrollView>
您可以在行布局中使用它:
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
//Your layout item here
</HorizontalScrollView>
將垂直RecyclerView
放在HorizontalScrollView
如下所示。
<HorizontalScrollView
android:fillViewport="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/tasks_recycler_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>
</HorizontalScrollView>
此外, RecyclerView
的項目視圖的根元素的寬度必須為wrap_content
而不是match_parent
,通過將其寬度設置為wrap_content
它允許它擴展其父元素的寬度,在這種情況下將是RecyclerView
。
現在,在項目視圖中,為內部視圖提供wrap_content
或固定寬度,在您的情況下,這些視圖是顯示“No”、“Wonum”、“Item num”、“Quantity”和“UOM”的視圖。 通過提供固定的內部視圖或wrap_content
選項,它們將自動擴展或根據給定的寬度擴展,從而擴展其父級。
嘗試這個
val linearLayoutManager = LinearLayoutManager(this)
linearLayoutManager.orientation = LinearLayoutManager.HORIZONTAL
selected_recycler_view.layoutManager = linearLayoutManager
adapter = TAdapter(this, existing.selectedArrayList)
使用此布局管理器允許在垂直 LinearLayoutManager 中水平滾動長項目。
import android.content.Context;
import android.view.View;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
public class FullScrollLayoutManager extends LinearLayoutManager {
private int offset;
private int maxOffset;
public FullScrollLayoutManager(Context context) {
super(context);
}
@Override
public void onLayoutCompleted(RecyclerView.State state) {
super.onLayoutCompleted(state);
int n = getChildCount();
offset = 0;
maxOffset = 0;
int ownWidth = getWidth();
for(int i=0; i<n; ++i) {
View view = getChildAt(i);
int x = view.getRight();
if(x>ownWidth) maxOffset = Math.max(maxOffset,x-ownWidth);
}
}
@Override
public boolean canScrollHorizontally() {
return true;
}
@Override
public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
if(dx<0) {
if(-dx>offset) dx = -offset;
}
else
if(dx>0) {
if(dx+offset>maxOffset) dx = maxOffset-offset;
}
offsetChildrenHorizontal(-dx);
offset += dx;
return dx;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.