我在这个应用里面用了三个片段作为导航菜单。我想把回收者视图放在**故事片段里面。**但是我没有安装回收者视图。我在下面分享代码。你会给予相关的解决方案。
主页活动类.Java
软件包com. quotecollector;
导入androidx。注解。非空;
导入Android操作系统。
导入androidx.appcompat.app.AppCompatActivity;
导入androidx.appcompat.widget工具栏;
导入androidx.内核.视图.重力兼容;
导入安卓系统.
导入androidx.fragment.app片段;
导入Android片段管理器;
导入Android片段应用程序片段事务;
导入androidx.回收机视图.小部件.回收机视图;
导入android.os.捆绑包;
导入android.视图.菜单项;
导入谷歌安卓材料导航视图;
导入com. quotecollector. fragment.fragmentAbout;
导入com. quotecollector. fragment.fragmentStories;
导入com. quotecollector. fragment.FragmentSettings;
公共类HomeActivity扩展了AppCompatActivity {
DrawerLayout drawerLayout;
NavigationView navigationView;
Toolbar toolbar;
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// Now we will find the toolbar here//
drawerLayout = findViewById(R.id.drawerLayout);
navigationView = findViewById(R.id.navView);
toolbar = findViewById(R.id.toolBar);
recyclerView = findViewById(R.id.recyclerviewStories);
//Now we will set the toolBar//
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Quotes Collectors");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//Now we set here the String arrays//
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout,
工具栏,R.字符串.打开抽屉,R.字符串.关闭抽屉);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
//Here we implement one method//
navigationView.setNavigationItemSelectedListener(new
导航视图.OnNavigationItemSelectedListener(){
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.About) {
loadFragment(new FragmentAbout());
} else if (id == R.id.Stories) {
loadFragment(new FragmentStories());
} else {
loadFragment(new FragmentSettings());
}
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
});
}
@Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
super.onBackPressed();
}
private void loadFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.Content, fragment);
fragmentTransaction.commit();
}
}
这是片段存储类.java
package com.quotescollector.Fragments;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.quotescollector.Models.StoriesModel;
import com.quotescollector.R;
import java.util.ArrayList;
public class FragmentStories extends Fragment {
RecyclerView.Adapter adapter;
RecyclerView recyclerView;
ArrayList<StoriesModel> storiesModelList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_stories, container, false);
storiesModelList = new ArrayList<>();
recyclerView = view.findViewById(R.id.recyclerviewStories);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(adapter);
displayStories();
return view;
}
private void displayStories() {
storiesModelList.add(new StoriesModel(R.drawable.stories, "Stories"));
storiesModelList.add(new StoriesModel(R.drawable.asia, "asia"));
storiesModelList.add(new StoriesModel(R.drawable.stories, "Stories"));
storiesModelList.add(new StoriesModel(R.drawable.asia, "asia"));
storiesModelList.add(new StoriesModel(R.drawable.stories, "Stories"));
storiesModelList.add(new StoriesModel(R.drawable.asia, "asia"));
}
}
这是故事Adapterclass.java
package com.quotescollector.Adapters;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.quotescollector.Models.StoriesModel;
import com.quotescollector.R;
import java.lang.reflect.Array;
import java.util.ArrayList;
public class StroiesAdapter extends RecyclerView.Adapter<StroiesAdapter.viewHolder> {
ArrayList<StoriesModel> storiesModelArrayList;
Context context;
//Here we need a constructor//
public StroiesAdapter(ArrayList<StoriesModel> storiesModelArrayList, Context context) {
this.storiesModelArrayList = storiesModelArrayList;
this.context = context;
}
@NonNull
@Override
public viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.fragment_stories, parent, false);
return new viewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull viewHolder holder, int position) {
StoriesModel storiesModel = storiesModelArrayList.get(position);
holder.image.setImageResource(storiesModel.getImage());
holder.text.setText(storiesModel.getText());
}
@Override
public int getItemCount() {
return storiesModelArrayList.size();
}
public class viewHolder extends RecyclerView.ViewHolder {
ImageView image;
TextView text;
public viewHolder(@NonNull View itemView) {
super(itemView);
image = itemView.findViewById(R.id.ImageFeather);
text = itemView.findViewById(R.id.storiesHandler);
}
}
}
Stroies片段XML文件
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".Fragments.FragmentStories">
<!-- TODO: Update blank fragment layout -->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerviewStories"
android:layout_width="match_parent"
tools:listitem="@layout/stories_fragment_sample_layout"
android:layout_height="match_parent"/>
</LinearLayout>
这是包含XML文件和Java类的故事片段数据。我无法修复它。请指导我如何在导航菜单中显示回收视图。
1条答案
按热度按时间nwo49xxi1#
您没有创建适配器对象,也创建了一个名为
StroiesAdapter
的适配器,但您使用的是错误的RecyclerView.Adapter
,以下是更新后的代码,请尝试以下操作: