gpt4 book ai didi

android - RecyclerView 仅显示一项

转载 作者:搜寻专家 更新时间:2023-11-01 08:25:52 29 4
gpt4 key购买 nike

我正在使用 RecyclerView,但是当在 recyclerview 中查看项目时,只显示列表中的第一项这是 RecyclerView 适配器

 package com.example.abdelmagied.myapplication;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.ArrayList;

/**
* Created by AbdELMagied on 7/19/2017.
*/
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder>{
private ArrayList<items> myitems;
public LayoutInflater inflater;
public RecyclerAdapter(Context context, ArrayList<items> myitems) {
this.myitems = myitems;
this.inflater = LayoutInflater.from(context);
}

@Override
public RecyclerAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = inflater.inflate(R.layout.recyclerviewrow , parent , false);
return new MyViewHolder(v);
}

@Override
public void onBindViewHolder(RecyclerAdapter.MyViewHolder holder, int position) {
holder.name.setText(myitems.get(position).name);
holder.price.setText(myitems.get(position).price);
}

@Override
public int getItemCount() {
return myitems.size();
}

public static class MyViewHolder extends RecyclerView.ViewHolder{
TextView name;
TextView price;
public MyViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.recyclerviewname);
price = (TextView)itemView.findViewById(R.id.recyclerviewprice);
}
}
}

这是名为 recyclerviewrow.xml 的布局

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/recyclerviewname" />

<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/recyclerviewprice" />
</LinearLayout>

这是 Main3Activity.xml

 package com.example.abdelmagied.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import java.util.ArrayList;

public class Main3Activity extends AppCompatActivity {
public RecyclerView mRecyclerView;
public RecyclerView.LayoutManager mymanager;
public RecyclerView.Adapter recyclerViewAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Bundle bundle = getIntent().getExtras();
ArrayList<items> myitems = bundle.getParcelableArrayList("myarray");
ArrayList<items> go = new ArrayList<items>();
go.add(new items("mobile" , " blablabl" , "655"));
go.add(new items("labtop" , " blablabl" , "655"));
go.add(new items("sony" , "blablbl" , "655"));
go.add(new items("mobile" , " blablabla" , "655"));
go.add(new items("mobile" , "blablabla" , "655"));
mRecyclerView = (RecyclerView) findViewById(R.id.RecyclerId);
mymanager = new LinearLayoutManager(this);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(mymanager);
recyclerViewAdapter = new RecyclerAdapter(this , go);
mRecyclerView.setAdapter(recyclerViewAdapter);

}
}

最佳答案

问题是 match_parent,请将您的父项目 android:layout_height 更改为 wrap_content。

现在你也会有项目,但它会在底部,如果你滚动你可以看到第二个项目。

所以改成这样,

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>

关于android - RecyclerView 仅显示一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45194622/

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