gpt4 book ai didi

android - 获取空的 RecyclerView

转载 作者:行者123 更新时间:2023-11-30 01:08:46 25 4
gpt4 key购买 nike

这是我第一次使用 RecyclerView,我遇到了一个问题。我试图让我的 View 成为 DrawerFragment。抽屉菜单工作正常,但我在那里看不到 RecyclerView。谁能帮帮我?

这是我的 fragment 的布局:

<LinearLayout 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:background="#DADADA"
tools:context="com.example.entwicklung1.designtestapp.NavigationDrawerFragment"
android:orientation="horizontal">

<LinearLayout
android:id="@+id/linid"
android:layout_width="match_parent"
android:layout_height="120dp"
android:background="#0064a7"
android:paddingLeft="20dp">

<ImageView
android:layout_width="240dp"
android:layout_height="120dp"
android:elevation="20dp"
android:gravity="center_vertical"
android:src="@drawable/zanderlogo" />
</LinearLayout>

<android.support.v7.widget.RecyclerView
android:id="@+id/drawerList"
android:layout_width="match_parent"
android:layout_height="wrap_content"

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

</LinearLayout>

这里是代码:

public class NavigationDrawerFragment extends Fragment {
private RecyclerView recyclerView;

public static final String PREF_FILE_NAME = "testpref";
public static final String KEY_USER_LEARNED_DRAWNER = "user_learned_drawer";
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private MyAdapter adapter;

private boolean mUserLearnedDrawer;
private boolean mFromSavedInstanceState;
private View containerView;

public NavigationDrawerFragment() {
// Required empty public constructor
}

public List<Information> getData(){
List<Information> data = new ArrayList<>();
int[] icons={R.drawable.ic_number1,R.drawable.ic_number2,R.drawable.ic_number3,R.drawable.ic_number4,R.drawable.ic_number5};
String[] titles ={"News", "Produkte","Homepage","Kontakt","Anfahrt"};

for(int i=0;i<titles.length; i++){
Information current=new Information();
current.iconId=icons[i];
current.title=titles[i];
data.add(current);
}
return data;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mUserLearnedDrawer = Boolean.valueOf(readFromPreferences(getActivity(), KEY_USER_LEARNED_DRAWNER, "false"));
mFromSavedInstanceState = savedInstanceState != null ? true : false;

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout=inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
recyclerView=(RecyclerView)layout.findViewById(R.id.drawerList);
adapter = new MyAdapter(getActivity(),getData());
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(adapter);
return layout;
}




public void setUp(int fragmentId, DrawerLayout drawerLayout, final Toolbar toolbar) {
containerView=getActivity().findViewById(fragmentId);
mDrawerLayout = drawerLayout;
mDrawerToggle = new ActionBarDrawerToggle(getActivity(),
drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {

@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
if(!mUserLearnedDrawer){
mUserLearnedDrawer=true;
saveToPreferences(getActivity(),KEY_USER_LEARNED_DRAWNER,mUserLearnedDrawer+"");
}
getActivity().supportInvalidateOptionsMenu();
}

@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
getActivity().supportInvalidateOptionsMenu();

}
};

mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerLayout.post(new Runnable() {
@Override
public void run() {
mDrawerToggle.syncState();
if(!mUserLearnedDrawer && !mFromSavedInstanceState){
mDrawerLayout.openDrawer(containerView);
}
}
});
}

public static void saveToPreferences(Context context, String preferenceName, String preferenceValue) {
SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();

editor.putString(preferenceName, preferenceValue);
editor.apply();
}

public static String readFromPreferences(Context context, String preferenceName, String defaultValue) {
SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);
return sharedPreferences.getString(preferenceName, defaultValue);
}
}

这里是 RecyclerView 适配器:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{
private LayoutInflater inflater;
List<Information> data = Collections.emptyList();

public MyAdapter(Context context,List<Information> data) {
inflater=LayoutInflater.from(context);
}

@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);
holder.icon.setImageResource(current.iconId);
}

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

class MyViewHolder extends RecyclerView.ViewHolder {
TextView title;
ImageView icon;

public MyViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.listText);
icon = (ImageView)itemView.findViewById(R.id.listIcon);
}
}
}

我做错了什么?

最佳答案

您不在适配器构造函数中设置数据。所以,你的数据是空的。

    List<Information> data = Collections.emptyList();

public MyAdapter(Context context,List<Information> data) {
inflater=LayoutInflater.from(context);
this.data = data;
^^^^^^^^^^^^^^^^ add this statement
}

更新:

需要将顶部 LinearLayout 的方向更改为“垂直”

关于android - 获取空的 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38632399/

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