我简化了代码以显示问题
Mainactivity.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());
}
}
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;
}
}
ItemData.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;
}
}
activity_main.xml中
<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>
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>
当我在item_layout.xml中将LinearLayout更改为RelativeLayout时,它可以正常工作,但是当我使用LinearLayout时,它会忽略match_parent。 我需要在不将LinearLayout更改为RelativeLayout的情况下使其工作。
答案 0 :(得分:2)
只需将项目视图扩展为:
即可View itemLayoutView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_layout, parent, false);
将父级传递给inflater让inflater知道哪些layoutParams应该使用。第3个参数表示视图是否应附加到父级。
答案 1 :(得分:-2)
使用RecyclerView时,需要指定一个LayoutManager,负责布置视图中的每个项目。 LinearLayoutManager允许您指定方向,就像普通的LinearLayout一样。您可以尝试权重和权重总和属性吗?
代码:
LinearLayoutManager layoutManager
= new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
RecyclerView myList = findViewById(R.id.my_recycler_view);
myList.setLayoutManager(layoutManager)