RecyclerView项目左对齐

时间:2019-11-26 12:59:52

标签: android android-recyclerview

我在tags_frame中有一个FoodCardFragment(可以找到其布局here),该RecyclerView用于在TagsFragment中显示TagsFragment<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="start" /> 的布局很简单:

RecyclerView

StaggeredGridLayoutManager使用LinearLayoutManagerRecyclerView。但是,从您的卡片视图中间可以看到,这个StaggeredGridLayoutManager(由圆角矩形组成)使用LinearLayoutManager并且居中。实际上,在另一项活动中,我使用了(“autoAcceptAlerts”,true);,它也居中。

如何将其对准卡的左侧(开始)?

Image

2 个答案:

答案 0 :(得分:0)

您可以在public class JwtTokenHeaderHandler : DelegatingHandler { private readonly IAppLocalStorageService _localStorage; private readonly ILogger<JwtTokenHeaderHandler> _logger; private readonly HttpMessageHandler _innerHandler; private readonly MethodInfo _method; public JwtTokenHeaderHandler(IAppLocalStorageService localStorage, HttpMessageHandler innerHandler, ILogger<JwtTokenHeaderHandler> logger) { _localStorage = localStorage; _logger = logger; _innerHandler = innerHandler; var type = innerHandler.GetType(); _method = type.GetMethod("SendAsync", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod) ?? throw new InvalidOperationException("Cannot get SendAsync method"); WebAssemblyHttpMessageHandlerOptions.DefaultCredentials = FetchCredentialsOption.Include; } protected override async Task<HttpResponseMessage> SendAsync( HttpRequestMessage request, CancellationToken cancellationToken) { _logger.LogDebug("Adding Jwt to Header from JwtTokenHeaderHandler: SendAsync"); _logger.LogDebug($"Does header contain 'bearer' token: {!request.Headers.Contains("bearer")}"); if (!request.Headers.Contains("bearer")) { _logger.LogDebug("Adding Bearer Token to Header"); var savedToken = await _localStorage.GetTokenAsync(); _logger.LogDebug($"Saved Token is : {savedToken}"); if (!string.IsNullOrWhiteSpace(savedToken)) { request.Headers.Authorization = new AuthenticationHeaderValue("bearer", savedToken); } } //return await base.SendAsync(request, cancellationToken); return await (_method.Invoke(_innerHandler, new object[] { request, cancellationToken }) as Task<HttpResponseMessage>); } } 中进行尝试:

fragment_tags.xml

<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/recycler_view" android:layout_width="wrap_parent" android:layout_height="match_parent" android:layout_gravity="start" /> 用于wrap_content

答案 1 :(得分:0)

之所以会这样,是因为您将FrameLayout约束为两侧的父级,并将width设置为match_parent。您有更多选择,一种是将引力起点设置为框架布局,或者将框架布局的宽度设置为wrap_content并将约束设置为仅开始。