当前位置: 首页 > 知识库问答 >
问题:

只有在标签之间移动后,片段中RecyclView的内容才会显示

呼延渝
2023-03-14

我对fragment and recyclerView和翻新有问题
我的第一个片段显示了一个人员列表,当我开始片段活动时不显示内容,当我移动到第三个片段并返回到第一个片段时,内容出现。。。此外,当我使用编辑搜索文本和按搜索时,回收器视图必须刷新,但当我转到第三个片段并返回数据时,会出现<有什么问题吗?我正在使用改造2从服务器获取数据

我的片段:

public class FragmentPerson extends Fragment{
private  List<Person> persons = new ArrayList<>();
private RecyclerView recyclerView;
private Button Add;
//Datas
private PersonServise mTService;
public PersonAdapter adapter;
private Button search;
private EditText searchField;


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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_person, container, false);
    recyclerView = (RecyclerView)rootView.findViewById(R.id.ry_persons);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    adapter = new PersonAdapter(persons);
    recyclerView.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    Add = (Button)rootView.findViewById(R.id.FP_BT_AddPerson);
    Add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent goToAdd = new Intent(getActivity(), AddPerson.class);
            startActivity(goToAdd);
        }
    });


    search = (Button)rootView.findViewById(R.id.FP_BT_Search);
    searchField = (EditText)rootView.findViewById(R.id.FP_ET_SearchField);

    search.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try {
                PersonProvider personProvider = new PersonProvider();
                mTService = personProvider.getTService();

                String s = searchField.getText().toString();
                String[] words = s.split("\\s+");
                for (int i = 0; i < words.length; i++) {
                    words[i] = words[i].replaceAll("[^\\w]", "");
                }
                if(words[1] == null){
                    words[1] = new String("a");
                }
                Toast.makeText(getActivity(), words[0], Toast.LENGTH_SHORT).show();
                Toast.makeText(getActivity(), words[1], Toast.LENGTH_SHORT).show();

                Call<List<Person>> call = mTService.getAllPersons(words[0],words[1]);
                call.enqueue(new Callback<List<Person>>() {
                    @Override
                    public void onResponse(Call<List<Person>> call, Response<List<Person>> response) {
                        if(response.isSuccessful()){
                            if(response.body()!=null){
                                persons = response.body();
                            }
                            if(response.body()==null){
                                Toast.makeText(getActivity(), "No data", Toast.LENGTH_SHORT).show();
                            }
                        }
                        else {
                            Toast.makeText(getActivity(), "Response is NOT succesful", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<List<Person>> call, Throwable t) {
                        Toast.makeText(getActivity(), "Failure :" + t.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });
            }
            catch (IndexOutOfBoundsException e){
                Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }
    });


    return rootView;
}
private void setData(){
    try {
        PersonProvider personProvider = new PersonProvider();
        mTService = personProvider.getTService();
        Call<List<Person>> call = mTService.getAllPersons("a","a");
        call.enqueue(new Callback<List<Person>>() {
            @Override
            public void onResponse(Call<List<Person>> call, Response<List<Person>> response) {
                if(response.isSuccessful()){
                    if(response.body()!=null){
                        persons = response.body();
                    }
                    if(response.body()==null){
                        Toast.makeText(getActivity(), "No data", Toast.LENGTH_SHORT).show();
                    }
                }
                else {
                    Toast.makeText(getActivity(), "Response is NOT succesful", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<List<Person>> call, Throwable t) {
                Toast.makeText(getActivity(), "Failure :" + t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });

    }
    catch (NullPointerException e){
        Toast.makeText(getActivity(), "crash because: " + e, Toast.LENGTH_LONG).show();
    }
}

}

这是我的活动表和片段:

public class UserManageData extends AppCompatActivity {


//Declare of Tab
TabLayout tabLayout;
ViewPager viewPager;
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.user_manage_data);



    //Tabs Functions
    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);
    tabLayout = (TabLayout) findViewById(R.id.UMD_TabLayout) ;
    tabLayout.setupWithViewPager(viewPager);
}


void setupViewPager(ViewPager viewPager){
    ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
    viewPagerAdapter.addFragment(new FragmentPerson(), "بارشمار"); // `new FragmentPerson()` should be inside `FragmentPagerAdapter.getItem()`
    viewPagerAdapter.addFragment(new FragmentServise(), "سرویس");  // `new FragmentServise()` should be inside `FragmentPagerAdapter.getItem()`
    viewPagerAdapter.addFragment(new FragmentShip(), "کشتی");  // `new FragmentShip()` should be inside `FragmentPagerAdapter.getItem()`
    viewPagerAdapter.addFragment(new FragmentTransite(), "ترانزیت");  // `new FragmentTransite()` should be inside `FragmentPagerAdapter.getItem()`
    viewPager.setAdapter(viewPagerAdapter);

//寻呼机。setOffscreenPageLimit(4);}

public class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>(); // this line can cause crashes
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

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

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

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment); // this line can cause crashes
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

}

和回收器视图适配器:

    public class PersonAdapter extends RecyclerView.Adapter<PersonAdapter.PersonViewHolder>
{
    List<Person> persons;

    public PersonAdapter(List<Person> persons) {
        this.persons = persons;
    }

    @Override
    public PersonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.manage_data_item,parent,false);
        return new PersonViewHolder(view);
    }

    @Override
    public void onBindViewHolder(PersonViewHolder holder, final int position) {
        Person person = persons.get(position);
        holder.name.setText(person.getPersonFirstName());
        holder.mobile.setText(person.getPersonMobile());
        holder.delete.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {

            }
        });
    }

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

    public class PersonViewHolder extends RecyclerView.ViewHolder {
        private Button delete;
        private Button edit;
        private TextView name;
        private TextView mobile;
        public PersonViewHolder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.MDI_TV_1);
            mobile = (TextView) itemView.findViewById(R.id.MDI_TV_mobile);
            delete = (Button) itemView.findViewById(R.id.MDI_BT_Delete);
            edit = (Button) itemView.findViewById(R.id.MDI_BT_Edit);
        }
    }

}

共有2个答案

潘宸
2023-03-14
  1. setData()方法从onCreate()移动到onCreateView()的末尾

段干茂实
2023-03-14

你需要用这个。

viewPager.setOffscreenPageLimit(3);
 类似资料: