gpt4 book ai didi

java - RecyclerView 根本不显示。我找不到错误。我该如何解决?

转载 作者:行者123 更新时间:2023-12-01 18:31:26 25 4
gpt4 key购买 nike

我正在尝试使用 ViewPageAdapter 将我的 recyclerview 构建为多个 fragment 。我对 Android Studio 还很陌生,我不知道如何处理我遇到的错误。我的回收 View 没有显示,在运行时,我遇到的唯一问题似乎是“跳帧”类型的问题。这通常小于 100 帧(而且我的模拟器很慢,所以我不知道应用程序是否真的占用了这么多时间)。

我尝试修复它的事情:我尝试使用 AsyncTask 类,但我仍然不确定如果它减慢了应用程序的速度,如何正确实现它。该平台上的一些人建议您需要翻转回收器 View 适配器和布局管理器的设置顺序,但这根本没有帮助。

注意:我还没有时间为 ViewPageAdapter 创建新的 fragment 类。目前只有一个 fragment 类,但我打算在 ViewPager 上将多个 fragment 表示为多个页面。 (另外,我不想分享我团队项目背后的总体想法。我想保持匿名,但如果您发现有必要了解该项目的主要想法,我绝对可以发送消息更多详细信息。)

我设置 RecyclerView 适配器的 fragment


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

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

public class Fragment_new extends Fragment {

View v;
private RecyclerView myRecyclerView;
private List<new_prescription> list_prescriptions;

public View onCreateNewView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.new_fragment, container, false);
myRecyclerView = v.findViewById(R.id.recyclerview_obj);
recyclerViewAdapter recycleAdapter = new recyclerViewAdapter(getContext(), list_prescriptions);
myRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
myRecyclerView.setAdapter(recycleAdapter);
return v;

}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

list_prescriptions = new ArrayList<>();
list_prescriptions.add(new new_prescription("Some Medication #1", "6:30-9:30", 5));
list_prescriptions.add(new new_prescription("Some Medication #2", "1:30-4:30", 5));
}
}

ViewPageAdapter 为不同的 fragment 创建 View


import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

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

public class ViewPageAdapter extends FragmentPagerAdapter {


private final List<Fragment> pager_list = new ArrayList<>();
private final List<String> string_List = new ArrayList<>();

public ViewPageAdapter(FragmentManager manager) {
super(manager);

}

@Override
public Fragment getItem(int position) {
return pager_list.get(position);
}

@Override
public int getCount() {
return string_List.size();
}

public CharSequence elementString(int position) {
return string_List.get(position);
}

public void addFragment(Fragment fragment, String title) {
pager_list.add(fragment);
string_List.add(title);

}


}

我的 RecyclerView 的普通 RecyclerViewAdapter


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

public class recyclerViewAdapter extends RecyclerView.Adapter<recyclerViewAdapter.MyViewHolder> {

Context data;
List<new_prescription> new_list;

public recyclerViewAdapter(Context data, List<new_prescription> new_list) {
this.data = data;
this.new_list = new_list;
}

public recyclerViewAdapter() {
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(data).inflate(R.layout.new_fragment_items, parent, false);
MyViewHolder view = new MyViewHolder(v);
return view;
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

holder.new_name.setText(new_list.get(position).getName());
holder.new_quantity.setText(new_list.get(position).getTime());

}

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

public static class MyViewHolder extends RecyclerView.ViewHolder {

private TextView new_name;
private TextView new_quantity;
private ImageView new_image;
private TextView new_time;

public MyViewHolder(@NonNull View itemView) {
super(itemView);
new_name = itemView.findViewById(R.id.prescription_name);
new_image = itemView.findViewById(R.id.new_image);
new_quantity = itemView.findViewById(R.id.quantity_items);
new_time = itemView.findViewById(R.id.new_time);

}

}
}

主要 Activity 类


import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;

public class Main_Activity_Frags extends AppCompatActivity {

private TabLayout layout;
private ViewPager viewpager;
private ViewPageAdapter adapter;


public Main_Activity_Frags() {
}

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_2);

layout = findViewById(R.id.tab_layout_id);
viewpager = findViewById(R.id.viewpager_id);
adapter = new ViewPageAdapter(getSupportFragmentManager());


adapter.addFragment(new Fragment_new(),"Prescriptions For Today");
adapter.addFragment(new Secondary_Fragment(),"Quantity");
adapter.addFragment(new Fragment_final(), "Time");


viewpager.setAdapter(adapter);
layout.setupWithViewPager(viewpager);
layout.getTabAt(0).setIcon(R.drawable.baseline_today_black_18dp);
layout.getTabAt(1).setIcon(R.drawable.baseline_history_black_18dp);


}



}

这是我用来实例化添加到 RecyclerView Arraylist 的 new_prescription 对象的类。

public class new_prescription {

private String Name;
private String Time;
private int Frequency;

public new_prescription() {
}

public new_prescription(String name, String time, int frequency) {
Name = name;
Time = time;
Frequency = frequency;
}

public String getTime() {
return Time;
}

public String getName() {
return Name;
}

public int getFrequency() {
return Frequency;
}
}

最佳答案

我认为这是可行的,试试这个:

public class Fragment_new extends Fragment {

private RecyclerView myRecyclerView;
private List<new_prescription> list_prescriptions;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.new_fragment, container, false);
myRecyclerView = v.findViewById(R.id.recyclerview_obj);
list_prescriptions = new ArrayList<>();
list_prescriptions.add(new new_prescription("Some Medication #1", "6:30-9:30", 5));
list_prescriptions.add(new new_prescription("Some Medication #2", "1:30-4:30", 5));

recyclerViewAdapter recycleAdapter = new recyclerViewAdapter(getContext(), list_prescriptions);
myRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
myRecyclerView.setAdapter(recycleAdapter);
return v;

}


}

关于java - RecyclerView 根本不显示。我找不到错误。我该如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60161505/

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