gpt4 book ai didi

android - 如何在抽屉导航的第一个 fragment 中调用 RecyclerView?我遵循了 slidenerd 的教程,但遇到了一个错误

转载 作者:行者123 更新时间:2023-11-30 02:04:58 26 4
gpt4 key购买 nike

这是我希望插入回收器 View 的 fragment 的 Java 文件。请不要担心我出于某些原因删除的包名称。

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

public class Dashboard extends Fragment {
private RecyclerView recyclerView;
private RecyclerAdapter adapter;

/**
* Returns a new instance of this fragment for the given section number.
*/
public static Dashboard newInstance() {
Dashboard fragment = new Dashboard();
return fragment;
}

public Dashboard () {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_dashboard, container,
false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.drawerlist);
adapter=new RecyclerAdapter(getActivity().getData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return rootView;
}

public static List<Information> getData(){
List<Information> data=new ArrayList<>();
String[] titles={"Rooms Occupied","RoomsVacant","Check-In","Check-Out","Extensions","Confirmations","Cancellations"};
for(int i=0;i<titles.length;i++)
{
Information current =new Information();
current.title=titles[i];
data.add(current);
}
return data;
}

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}






}

这是我的适配器类

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 java.util.Collections;
import java.util.List;

/**
* Created by gowtham on 6/13/2015.
*/
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
private LayoutInflater inflater;
List<Information> data= Collections.emptyList();
public RecyclerAdapter(Context context, List<Information> data){
inflater=LayoutInflater.from(context);
this.data=data;
}

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

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Information current=data.get(position);
holder.title.setText(current.title);

}

@Override
public int getItemCount() {
return 0;
}

class MyViewHolder extends RecyclerView.ViewHolder{
TextView title;


public MyViewHolder(View itemView) {
super(itemView);
title= (TextView) itemView.findViewById(R.id.viewText);

}
}
}

这是我想在其中使用回收器 View 的 fragment 的布局文件

<?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">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/viewImage"
android:background="@drawable/ic_dashboard"
android:layout_gravity="center_vertical"
android:gravity="center"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/viewText"
android:text="Dummy Text"
android:textStyle="bold"
android:layout_gravity="left"
android:gravity="center" />
</LinearLayout>

由于我是 stackoverflow 的新手,所以我没有足够的声誉来添加图像。所以我会在下面的评论中附上截图。请看一下它以了解这里到底发生了什么。谢谢!

最佳答案

为什么要调用getActivity().getData(),调用getData()即可。

在你的 Adapter 类中:

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

它不能只返回零,你应该返回数据大小。

关于android - 如何在抽屉导航的第一个 fragment 中调用 RecyclerView?我遵循了 slidenerd 的教程,但遇到了一个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30816671/

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