当回收器视图项根为 LinearLayout 时,为什么 match_parent 不起作用?

Mee*_*Yom 5 xml android android-linearlayout android-recyclerview

我简化了代码来显示问题

主要活动.java

public class MainActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);

        ItemData itemsData[] = { new ItemData("test 1"),
                new ItemData("test 2"),
                new ItemData("test 3")};

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        MyAdapter mAdapter = new MyAdapter(itemsData, this);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    }
}
Run Code Online (Sandbox Code Playgroud)

MyAdapter.java

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    static Context mContext;
    private ItemData[] itemsData;

    public MyAdapter(ItemData[] itemsData, Context context) {
        this.itemsData = itemsData;
        mContext = context;
    }

    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemLayoutView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item_layout, null);

        ViewHolder viewHolder = new ViewHolder(itemLayoutView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        viewHolder.txtViewTitle.setText(itemsData[position].getTitle());
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView txtViewTitle;

        public ViewHolder(View itemLayoutView) {
            super(itemLayoutView);
            txtViewTitle = (TextView)     itemLayoutView.findViewById(R.id.item_title);
        }
    }

    @Override
    public int getItemCount() {
        return itemsData.length;
    }
}
Run Code Online (Sandbox Code Playgroud)

项目数据.java

public class ItemData {
    private String title;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public ItemData(String title){
        this.title = title;
    }
}
Run Code Online (Sandbox Code Playgroud)

活动主文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"
    android:layout_height="match_parent"     android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"     tools:context=".MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v7.widget.RecyclerView>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

item_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="80dp">

    <TextView
        android:id="@+id/item_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/background_dark"
        android:background="@android:color/holo_blue_bright"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:textSize="22dp" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

当我在item_layout.xml中将LinearLayout更改为RelativeLayout时,它工作正常,但是当我使用LinearLayout时,它会忽略match_parent。我需要在不将 LinearLayout 更改为relativelayout 的情况下使其工作。

Sky*_*net -2

当你使用RecyclerView时,你需要指定一个LayoutManager来负责布局视图中的每个项目。LinearLayoutManager 允许您指定方向,就像普通的 LinearLayout 一样。您可以尝试权重和权重总和属性吗?

代码:

LinearLayoutManager layoutManager
    = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);

RecyclerView myList = findViewById(R.id.my_recycler_view);
myList.setLayoutManager(layoutManager)
Run Code Online (Sandbox Code Playgroud)