gpt4 book ai didi

java - 如何在android中安排RecyclerView上的数据?

转载 作者:行者123 更新时间:2023-11-30 01:04:05 24 4
gpt4 key购买 nike

我想使用 RecycleView 来显示我的数据数组。

这是我的 Adapater 类,它正在 View 中设置数据。

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.MyViewHolder>{

private Context context;
private List<Pojo> dataList;

public DataAdapter(Context context, List<Pojo> dataList) {
this.context = context;
this.dataList = dataList;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_main,parent,false);
return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.categoryTV.setText(dataList.get(position).getGameName());
}

@Override
public int getItemCount() {

return dataList.size();
}

public class MyViewHolder extends RecyclerView.ViewHolder{

public TextView categoryTV;

public MyViewHolder(View itemView) {
super(itemView);
categoryTV = (TextView) itemView.findViewById(R.id.text);
}
}
}

这是我的activity_main.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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:id="@+id/rellayout"
tools:context="com.acknotech.kiran.retrofit_second.MainActivity">

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

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

</android.support.v7.widget.RecyclerView>
</RelativeLayout>

这是 MainActivity..

public class MainActivity extends AppCompatActivity {

private RecyclerView recyclerView;
private DataAdapter dataAdapter;
private List<Pojo> dataArrayList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}

private void initViews(){
recyclerView=(RecyclerView) findViewById(R.id.my_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
loadJSON();
}

private void loadJSON(){
dataArrayList = new ArrayList<>();
Retrofit retrofit=new Retrofit.Builder().baseUrl("my url").addConverterFactory(GsonConverterFactory.create()).build();
AllGamesAPI requestInterface=retrofit.create(AllGamesAPI.class);

Call<List<Pojo>> call= requestInterface.getJSON();
call.enqueue(new Callback<List<Pojo>>() {
@Override
public void onResponse(Call<List<Pojo>> call, Response<List<Pojo>> response) {
dataArrayList = response.body();
dataAdapter=new DataAdapter(getApplicationContext(),dataArrayList);
recyclerView.setAdapter(dataAdapter);
}

@Override
public void onFailure(Call<List<Pojo>> call, Throwable t) {
Log.e("Error",t.getMessage());
}

});
}
}

当我尝试执行它时总是一次只打印一个项目,但在 dataList 中我有 11 个项目。

如何使用RecyclerView获取一个页面中的所有item?

感谢和问候

最佳答案

您不应在 onCreateViewHolder 中扩充 Activity 布局,而应仅为一个项目扩充布局(例如 android.R.layout.simple_list_item_1,或自定义一)。此布局不应使用 match_parent 作为项目的高度。

关于java - 如何在android中安排RecyclerView上的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39050197/

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