我想在包裹在片段中的RecycleView中显示ArrayList,但是当我运行apk时,它没有显示RV。当我在logcat上看到时,注意到“没有连接适配器;正在跳过布局”。
我尝试了很多更改片段和适配器的方法,但是那根本没有用。由于本网站上还有其他基于API的问题,我无法理解,因为我是初学者。我试图找出一个基于Arraylist的问题,结果仍然相同,没有一个解决方案是基于我的问题。
我将显示我的Adapter,fragment和mainActivity中的代码,它很长,但是我想知道导致我迷路和无知的问题。
片段:
public class FilmFragment extends Fragment {
View view;
ArrayList<Film> list = new ArrayList<>();
RecyclerView rvCategory;
FilmDatas filmDatas;
public FilmFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_movie,container,false);
return view;}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState){
rvCategory = view.findViewById(R.id.rvCategory);
rvCategory.setHasFixedSize(true);
list.addAll(filmDatas.getListData());
showRecycleList();}
private void showRecycleList(){
rvCategory.setLayoutManager(new LinearLayoutManager(getActivity()));
FilmAdapter adapter = new FilmAdapter(getContext());
adapter.setListMovie(list);
rvCategory.setAdapter(adapter);}}
适配器:
public class FilmAdapter extends RecyclerView.Adapter<FilmAdapter.CategotyViewHolder> {
Context context;
private ArrayList<Film> listFilm;
public ArrayList<Film> getList(){
return listFilm; }
public void setListMovie(ArrayList<Film> list) {
this.listFilm = list;}
public FilmAdapter(Context context
) {this.context = context;}
@NonNull
@Override
public CategotyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row_film,parent,false);
return new CategotyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final CategotyViewHolder holder, int position) {
final Film film = listFilm.get(position);
Glide.with(holder.itemView.getContext()).load(film.getPhoto()).apply(new RequestOptions().override(100,100))
.into(holder.imgPhoto);
holder.tvName.setText(film.getName());
holder.tvFrom.setText(film.getFrom());
holder.tvDate.setText(film.getDate());
}
@Override
public int getItemCount() {
return getList().size();
}
public class CategotyViewHolder extends RecyclerView.ViewHolder {
ImageView imgPhoto;
TextView tvName,tvFrom,tvDate;
public CategotyViewHolder(@NonNull View itemView) {
super(itemView);
imgPhoto = itemView.findViewById(R.id.img_item_photo);
tvName = itemView.findViewById(R.id.tv_item_name);
tvFrom = itemView.findViewById(R.id.tv_item_from);
tvDate = itemView.findViewById(R.id.tv_date);}}
主要活动:
public class MainActivity extends AppCompatActivity {
TabLayout tabLayout;
FrameLayout frameLayout;
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
frameLayout = findViewById(R.id.Frame);
tabLayout = findViewById(R.id.simpelTab);
viewPager = findViewById(R.id.viewPager);
viewPager.setAdapter(new sliderAdapter(getSupportFragmentManager()));
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
tabLayout.addOnTabSelectedListener(new TabLayout.BaseOnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
private class sliderAdapter extends FragmentPagerAdapter {
String movie = "MOVIE";
String tvShow = "TV SHOW";
final String tabs[] = {movie, tvShow};
public sliderAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new FilmFragment();
case 1:
return new TvShowFragment();
}
return null;
}
@Override
public int getCount() {
return tabs.length; //count Tabs[] size: 2
}
@Override
public CharSequence getPageTitle(int position) {
return tabs[position];
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
if (item.getItemId() == R.id.action_settings) {
Intent mIntent = new Intent(Settings.ACTION_LOCALE_SETTINGS);
startActivity(mIntent);
}
}
return super.onOptionsItemSelected(item);
}
}
电影数据:
public class FilmDatas { public static String [][] data = new String[][]{
{
"Venom (2018)","Investigative journalist Eddie Brock attempts a comeback following a scandal, but accidentally becomes the host of Venom, a violent, super powerful alien symbiote. Soon, he must rely on his newfound powers to protect the world from a shadowy organization looking for a symbiote of their own.","https://image.tmdb.org/t/p/w220_and_h330_face/2uNW4WbgBXL25BAbXGLnLqX71Sw.jpg","https://image.tmdb.org/t/p/w533_and_h300_bestv2/VuukZLgaCrho2Ar8Scl9HtV3yD.jpg","October 5, 2018"
}
};
public ArrayList<Film> getListData() {
ArrayList<Film> list = new ArrayList<>();
for (String[] aData : data) {
Film film = new Film();
film.setName(aData[0]);
film.setFrom(aData[1]);
film.setBackground(aData[2]);
film.setPhoto(aData[3]);
film.setDate(aData[4]);
list.add(film);
}
return list;
}
}
电影:(其中包含可包裹的文件和已设置/获取的文件)
public class Film implements Parcelable {
private String name,from,background,photo,date;
public Film() {
}
public String getName() {return name;}
public void setName (String name) {this.name = name;}
public String getFrom() {return from;}
public void setFrom (String from) {this.from = from;}
public String getBackground() {return background;}
public void setBackground (String background) {this.background = background;}
public String getPhoto() {return photo;}
public void setPhoto (String photo) {this.photo = photo;}
public String getDate() {return date;}
public void setDate (String date) {this.date = date;}
protected Film(Parcel in) {
name = in.readString();
from = in.readString();
background = in.readString();
photo = in.readString();
date = in.readString();
}
public static final Creator<Film> CREATOR = new Creator<Film>() {
@Override
public Film createFromParcel(Parcel in) {
return new Film(in);
}
@Override
public Film[] newArray(int size) {
return new Film[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(name);
parcel.writeString(from);
parcel.writeString(background);
parcel.writeString(photo);
parcel.writeString(date);
}
}
答案 0 :(得分:0)
您的问题取决于您的片段。
您已声明
FilmDatas filmDatas;
但是您从未实例化它,甚至没有在此处填充它的任何对象
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState){
rvCategory = view.findViewById(R.id.rvCategory);
rvCategory.setHasFixedSize(true);
list.addAll(filmDatas.getListData());
showRecycleList();
}
因此,没有数据可填充您的列表,并且recyclerview将不会显示任何内容
尝试将像这样的元素添加到列表中进行测试
list.add(new FilmDatas("param1","param2"...))
此外,您将列表声明为Film类型,但使用FilmDatas类型填充列表
但是我假设filmData.getListData()
返回要填充的类型为Film的对象。
问题不是“No adapter attached; skipping layout”
,问题是您没有在列表中添加任何元素,并且在将列表设置为适配器时,它没有在每个recyclerview中绑定任何该数据行,而getItemCount()
返回0,因此您的recyclerview不显示任何内容。
答案 1 :(得分:0)
您的问题似乎与以下代码有关:
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState){
rvCategory = view.findViewById(R.id.rvCategory);
rvCategory.setHasFixedSize(true);
list.addAll(filmDatas.getListData());
showRecycleList();}
private void showRecycleList(){
rvCategory.setLayoutManager(new LinearLayoutManager(getActivity()));
FilmAdapter adapter = new FilmAdapter(getContext());
adapter.setListMovie(list);
rvCategory.setAdapter(adapter);}}
您正在创建RecyclerView
之后添加列表,而不会刷新视图。您可以通过以下两种方式之一进行处理:更新方法以使用数据更新视图;或者,在创建适配器时传递数据。
如果您只想刷新列表(这对于视图膨胀后可能会接收到的数据很有帮助(想从API请求中获取数据)),则只需添加notifyDataSetChanged()
:
适配器:
public void setListMovie(ArrayList<Film> list) {
this.listFilm = list;
notifyDataSetChanged();
}
否则,您只需要在创建适配器时传递数据即可:
适配器:
public FilmAdapter(Context context, ArrayList<Film> list) {
this.context = context;
this.listFilm = list;
}
然后在您的Fragment
中,您将执行以下操作:
private void showRecycleList(){
rvCategory.setLayoutManager(new LinearLayoutManager(getActivity()));
if(list != null){
FilmAdapter adapter = new FilmAdapter(getContext(), list);
rvCategory.setAdapter(adapter);
} else {
//Handle error
}
}