我想使用RecyclerView显示记录列表。
但是当我运行应用程序时,它只显示一条记录。
我验证了我的代码,一切似乎都很好。
适配器代码:
public class AdapterData extends RecyclerView.Adapter<AdapterData.DummyHolder> {
private LayoutInflater layoutInflater;
private ArrayList<String> mItems = new ArrayList<>();
public Context ThisContext;
public AdapterData(Context context)
{
layoutInflater = LayoutInflater.from(context);
mItems = generateValues();
ThisContext = context;
}
public static ArrayList<String> generateValues(){
ArrayList<String> Dummy = new ArrayList<>();
for(int i=1; i<100; i++)
{
Dummy.add("Item"+i);
Log.d("MTS", String.valueOf(i));
}
return Dummy;
}
@Override
public DummyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= layoutInflater.inflate(R.layout.row_layout,parent,false);
DummyHolder holder=new DummyHolder(view);
return holder;
}
@Override
public void onBindViewHolder(DummyHolder holder, int position) {
holder.txt_name.setText(mItems.get(position));
Log.d("MAN=",mItems.get(position));
}
@Override
public int getItemCount() {
return 100;
}
public static class DummyHolder extends RecyclerView.ViewHolder{
TextView txt_name;
public DummyHolder(View itemView) {
super(itemView);
txt_name = (TextView) itemView.findViewById(R.id.tx_name);
}
}
}
行代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tx_name"
android:hint="Hello"/>
</LinearLayout>
主要活动XML:
<?xml version="1.0" encoding="utf-8"?>
<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="com.hogwarts.harrypotter.recyclerdemo.MainActivity">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rv_list">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
MainActivity Code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.rv_list);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(new AdapterData(this));
}
答案 0 :(得分:26)
看一下项目布局。根布局有android:layout_height =“match_parent”。 这意味着项目采取全屏高度。改为wrap_content并让派对。说哦,是啊