RecyclerView 项目不填充宽度 [英] RecyclerView items don't fill width

查看:35
本文介绍了RecyclerView 项目不填充宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have designed a layout with map fragment and recyclerView. Each recyclerView item is cardview (I have specified give the xml layout).

问题是 RecyclerView 项目没有填满屏幕宽度.这里的img

The problem is RecyclerView item doesn't fill screen width. img here

我试图将 layout_width 更改为 fill_parentmatch_parent ...但它无济于事

I tried to change layout_width to fill_parent, match_parent ... but it can't help

这是每个项目的布局

<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>

ma​​in_layout

<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天.谢谢.

Hope anyone can help me. I am stuck with it for 3 days. Thank you.

====================================19/11/15 编辑我不认为问题是我的 itemView 布局,因为当我将其更改为 GridLayout 时,它仍然没有填充宽度.这是我用于 RecyclerView 的 CustomAdapter

==================================== Edit on 19/11/15 I don't thing the problem is my itemView layout because when I change it to GridLayout it still doesn't fill the width. Here is my CustomAdapter for RecyclerView

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 中得到 RecylerView

and here I get RecylerView in main

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

推荐答案

当从 LayoutInflater 膨胀一个 View 时,你需要传递一个父参数以便 <要使用的 code>layout_* 属性.那是因为这些属性需要创建正确的 LayoutParams 类.这意味着您不能使用 inflate(R.layout.*, null),而必须为第二个参数传递一个 ViewGroup.在大多数情况下,您还希望使用该方法的三参数版本并将 false 作为第三个参数传递.如果省略或 true,则 View 会立即添加到父级,这会导致 onCreateViewHolder() 等地方出现问题,因为框架是设计用于稍后执行此操作.有关详细信息,请参阅此答案.

When inflating a View from a LayoutInflater, you need to pass a parent parameter in order for layout_* attributes to be used. That's because these attributes need to create the correct LayoutParams class. That means that you can't use inflate(R.layout.*, null), but must instead pass a ViewGroup for the second parameter. In most cases, you also want to use the three-parameter version of the method and pass false as the third parameter. If this is omitted or true then the View is immediately added to the parent, which causes problems in places like onCreateViewHolder() because the framework is designed to perform this operation later instead. For more details, see this answer.

就你而言,你有这条线

 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 );

这篇关于RecyclerView 项目不填充宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆