我在这个应用程序中使用了三个片段作为导航菜单。我想把回收器视图放在故事片段中但我未能安装回收器视图。我正在分享下面的编码。您将给出相关的解决方案。
主页Activityclass.Java
包com.quotescollector;
importandroidx.annotation.NonNull;
importandroidx.appcompat.app.ActionBarDrawerToggle;
导入androidx.appcompat.app.AppCompatActivity;
导入androidx.appcompat.widget.Toolbar;
导入androidx.core.view.GravityCompat;
importandroidx.draweryout.widget.drawerlayout;
导入androidx.fragment.app.fragment;
导入androidx.fragment.app.FragmentManager;
importandroidx.fragment.app.FragmentTransaction;
importandroidx.recyclerview.widget.RyclerView;
导入android.os.Bundle;
导入android.view.MenuItem;
导入com.google.android.material.navigation.NavigationView;
import com.quotescollector.Fragments.FragmentAbout;
导入com.quotescollector.Fragments.FragmentStories;
导入com.quotescollector.Fragments.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.string.OpenDrawer,R.strin.CloseDrawer(;
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
//Here we implement one method//
navigationView.setNavigationItemSelectedListener(new
NavigationView.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();
}
}
这是FragmentStoriesclass.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"));
}
}
这是Stories 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类对数据进行分段的故事。我无法修复它。请指导我如何在导航菜单中显示回收视图。
您没有创建适配器对象,还创建了一个名为StroiesAdapter
的适配器,但您使用的是RecyclerView.Adapter
,这是错误的,这是更新的代码,请尝试以下操作:
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 {
// here is the right type of adapter you made
StroiesAdapter 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);
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"));
// setting up your recyclerView and adapter after adding
// elements to the list.
recyclerView.setLayoutManager(new
LinearLayoutManager(getContext()));
// making object for adpter
adapter = new StroiesAdapter(storiesModelList, getContext());
// now setting up the adpter
recyclerView.setAdapter(adapter);
}
}