gpt4 book ai didi

android - RecyclerView 在 Android 中只显示一行

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:17:55 26 4
gpt4 key购买 nike

我想使用 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>

主要 Activity 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>

主要 Activity 代码:

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

最佳答案

看看项目布局。根布局有 android:layout_height="match_parent"。这意味着项目占据全屏高度。更改为 wrap_content 并开始派对。说哦是啊

关于android - RecyclerView 在 Android 中只显示一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36819329/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com