RecyclerView 项目不填充宽度

2024-02-17

我设计了一个带有地图片段和recyclerView的布局。 每个recyclerView项目都是cardview(我已经指定给出xml布局)。

问题是 RecyclerView 项目不填充屏幕宽度。img here https://i.stack.imgur.com/fz0px.png

我尝试将layout_width更改为fill_parent, match_parent......但这无济于事

这是布局对于每个项目

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:id="@+id/cardView">

    <LinearLayout
        android:id="@+id/locationItemView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="120px"
            android:layout_height="120px"
            android:id="@+id/imgIcon"
            android:background="@drawable/image_bg"
            android:layout_margin="5dp"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true" />

        <LinearLayout
            android:id="@+id/layoutInfo"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/txtName"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Location Name"
                android:textColor="#d5c645"
                android:textStyle="bold"
                android:textSize="20dp"
                android:padding="3dp" />

            <TextView
                android:id="@+id/txtAddress"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Location Address"
                android:textSize="16dp"
                android:padding="3dp" />

            <TextView
                android:id="@+id/txtDistance"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:text="Location Distance"
                android:textSize="14dp"
                android:textStyle="italic"
                android:padding="2dp"
                android:textAlignment="viewEnd" />
        </LinearLayout>
    </LinearLayout>
</android.support.v7.widget.CardView>

and the 主布局

<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:map="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
        android:layout_height="0px" android:id="@+id/map" tools:context=".Main"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_weight=".6"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/locationList"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight=".4"
        android:divider="#FEFFCC"
        android:dividerHeight="1dp" />

</LinearLayout>

<ListView
    android:id="@+id/navdrawer"
    android:layout_width="250px"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="?attr/colorPrimaryDark"
    android:choiceMode="singleChoice"
    android:divider="@android:color/white"
    android:dividerHeight="1dp"
    android:drawSelectorOnTop="false"/>
</android.support.v4.widget.DrawerLayout>

希望任何人都可以帮助我。我坚持了3天。 谢谢。

===================================== 编辑于 2015 年 11 月 19 日 我不认为问题出在我的 itemView 布局上,因为当我将其更改为 GridLayout 时,它仍然无法填充宽度。 这是我的 RecyclerView 的 CustomAdapter

public class LocationDetailsAdapter extends RecyclerView.Adapter<LocationDetailsViewHolder> {
    Context _context;
    ArrayList<LocationDetails> _data;

    public LocationDetailsAdapter(Context _context, ArrayList<LocationDetails> _object) {
        this._context = _context;
        _data = _object;
    }

    @Override
    public LocationDetailsViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View _v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_location,null );
        LocationDetailsViewHolder _viewHolder = new LocationDetailsViewHolder(_v);

        return _viewHolder;
    }

    @Override
    public void onBindViewHolder(LocationDetailsViewHolder locationDetailsViewHolder, int i) {
        LocationDetails _location = _data.get(i);
        locationDetailsViewHolder._imgType.setImageResource(R.drawable.repair_img);
        locationDetailsViewHolder._locationName.setText(_location.get_locationName());
        locationDetailsViewHolder._locationAddress.setText(_location.get_locationAddress() + ", " + _location.get_district() + ", " + _location.get_province());
        locationDetailsViewHolder._distance.setText(String.valueOf(_location.get_distance()) + " km");

        locationDetailsViewHolder._locationName.setOnClickListener(clickListener);
        locationDetailsViewHolder._imgType.setOnClickListener(clickListener);
        locationDetailsViewHolder._locationAddress.setOnClickListener(clickListener);

locationDetailsViewHolder._locationName.setTag(locationDetailsViewHolder);
        locationDetailsViewHolder._imgType.setTag(locationDetailsViewHolder);
        locationDetailsViewHolder._locationAddress.setTag(locationDetailsViewHolder);
}

    @Override
    public int getItemCount() {
        return (null != _data ? _data.size() : 0);
    }

    View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LocationDetailsViewHolder _holder = (LocationDetailsViewHolder)v.getTag();
            int _pos = _holder.getPosition();

            int _id = _data.get(_pos).get_id();

            Intent _intent = new Intent(CommonFields._context, ItemView.class);
            _intent.putExtra("LocationID",_id);
            _intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            CommonFields._context.startActivity(_intent);
        }
    };
}

在这里我在 main 中得到了 RecyclerView

_locationView = (RecyclerView)findViewById(R.id.locationList);
_locationView.setLayoutManager(new LinearLayoutManager(this));
_adapter = new LocationDetailsAdapter(Main.this, CommonFields._locationData);
_locationView.setAdapter(_adapter);

当给一个View from a LayoutInflater,您需要传递一个父参数才能layout_*要使用的属性。那是因为这些属性需要创建正确的LayoutParams班级。这意味着你不能使用inflate(R.layout.*, null),但必须传递一个ViewGroup对于第二个参数。在大多数情况下,您还想使用该方法的三参数版本并传递false作为第三个参数。如果省略或true那么View立即添加到父级,这会导致以下地方出现问题:onCreateViewHolder()因为该框架被设计为稍后执行此操作。有关更多详细信息,请参阅这个答案 https://stackoverflow.com/a/5027921/2565340.

就你而言,你有这条线

 View _v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_location,null );

你应该将其更改为

 View _v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_location, viewGroup, false );
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

RecyclerView 项目不填充宽度 的相关文章

随机推荐

  • 如何在特定时间间隔后运行方法?

    很清楚 例如 想象一下我的表单中的一个按钮 当用户单击按钮时 某些 void 方法应在 30 秒后运行 会有一个 void 方法DoAfterDelay这需要两个输入参数 第一个是执行的方法 使用委托 另一个是时间间隔 所以我会有 publ
  • 有人比较过 WCF 和 ZeroC ICE 吗?

    ZeroC 的 ICE www zeroc com 看起来很有趣 我有兴趣查看它并将其与我们使用 WCF 的现有软件进行比较 特别是 我们的 WCF 应用程序使用服务器回调 通过 HTTP 有谁对比过吗 进展如何 我对性能方面特别感兴趣 因
  • ASP.NET MVC RenderAction 中的以下方法或属性之间的调用不明确

    在我安装 ASP NET MVC 1 0 RTM 之前 该调用工作正常 Error CS0121 The call is ambiguous between the following methods or properties 代码片段
  • 将 Fetch 与授权标头和 CORS 结合使用

    我正在尝试让我的请求通过一个在线游戏 API 但我似乎无法正常工作 我正在使用FetchAPI 以及一些请求需要授权承载令牌 但请求永远不会与授权标头一起发送 我努力了 mode no cors credentials include 显然
  • Keras model.predict() 在第一次迭代时较慢,然后变得更快

    我正在尝试跑步model predict 在 for 循环中多次并计时在同一图像上花费的时间 该数据将用于计算运行预测所需时间的平均值 如果我在单独的脚本中运行预测 它将在我的 MacBook 上运行大约 300 毫秒 如果我随后在 for
  • Buildozer 构建在 NDK 构建时失败,给出 SDL_JAVA_PACKAGE_PATH 错误

    使用 Kivy 的 VM 2 0 设置时 Buildozer 0 34 会失败并出现此错误 我能够在 Ubuntu 16 04 中的不相关环境 无虚拟机 和相同的规范文件中复制它 日志输出 信息 标准输出 build app name an
  • 如何扩展操作栏中的导航列表?

    在 android 中 您可以通过传递微调器适配器和 OnNavigationListener 在操作栏中设置导航列表 问题是导航列表没有填充大部分操作栏 如何使其像 gmail 应用程序一样展开 Gmail 应用程序示例 My app 这
  • 如何在C#中应用多个.Tag属性?

    当我想存储 传递值时 我总是使用 Tag 属性 例如 当我存储值时 Form prosesEdit new FormProsesChemicalRawWbEdit prosesEdit Tag int this proses chemica
  • 目录级别 web.config 中的 Asp.net HttpModule

    我创建了一个自定义 http 模块 并希望将此模块添加到 Web 配置中 Web应用程序是一个包含多个 子应用程序 的项目 子应用程序只是一个文件夹 在该文件夹中它有自己的 web config 我这样做是为了让每个应用程序都有自己的应用程
  • 如何计算文件中字符串的出现次数?

    仅以这段代码为例 假装它是一个 HTML 文本文件 如果我想知道该文件的总次数echo出现了 我该如何使用 bash 来做到这一点 new user echo Preparing to add a new user sleep 2 addu
  • Facebook Php SDK - getUser() 始终返回 0

    我阅读了该论坛上的所有主题 但找不到解决方案 我使用 facebook 的最后一个 Php SDK v3 2 2 所以我用了这个 start fb facebook new Facebook array appId gt fb app id
  • Laravel:高级搜索表单查询

    我有一个高级搜索表单 可以使用 Laravel 从数据库中过滤结果 数据已正确过滤 但我要求用户能够使用相同的文本框 以高级形式 按名字或姓氏进行过滤 我尝试 orWhere 确保它用名字或姓氏过滤名称字段 但 orWhere 不考虑其他过
  • AngularJS 在 HTML 中留下注释:是否可以删除它们?

    有谁知道是否可以删除 html 代码中留下的角度注释 例如 如果我使用 ngRepeat 并且没有可重复的项目 AngularJS 会留下以下内容 此评论的结果是element嵌入执行者ngRepeat 看起来好像是一直在发生 https
  • 默认共享首选项使用的文件名是什么?

    安卓备份服务requires http developer android com guide topics data backup html SharedPreferences用于备份共享首选项的文件名 public static fin
  • 如何在数据框列表上使用 devtools::use_data?

    我有一系列数据框 我想将它们保存为包中的单独 rda 文件 我可以用devtools use data my df1 my df2 但我没有每个数据帧的命名对象 它们都存储在一个大列表中 我想要做的是为每个列表元素调用 use data 并
  • 如何从我的代码中删除重复项

    我有两个类似的方法 其中一个打印一些东西 其中一个保存一些东西 正如你所看到的 有很多重复的代码 我应该如何重构它并删除这种重复 public static void printSomething List
  • 在玩笑中模拟节点模块中的特定类

    我想嘲笑Socket类来自net节点模块 Docs https nodejs org api net html net class net socket 我有一堂课看起来像这样 import Socket from net class Fo
  • 在浏览器上通过php运行python脚本

    我有一个简单的 python 脚本 阿努巴哈 py usr bin env python coding UTF 8 enable debugging import cgitb import pandas cgitb enable print
  • 将新项目动态添加到 IQueryable 硬编码假存储库

    在使用真正的数据库之前构建应用程序 为了让事情正常工作 我可以首先使用硬编码列表作为假的内存存储库 public class FakeProductsRepository private static IQueryable
  • RecyclerView 项目不填充宽度

    我设计了一个带有地图片段和recyclerView的布局 每个recyclerView项目都是cardview 我已经指定给出xml布局 问题是 RecyclerView 项目不填充屏幕宽度 img here https i stack i