我有片段和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();
}
}
}
这是我的tablayout和片段的活动:
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(), "بارشمار");
viewPagerAdapter.addFragment(new FragmentServise(), "سرویس");
viewPagerAdapter.addFragment(new FragmentShip(), "کشتی");
viewPagerAdapter.addFragment(new FragmentTransite(), "ترانزیت");
viewPager.setAdapter(viewPagerAdapter);
// viewPager.setOffscreenPageLimit(4); }
public class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
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);
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);
}
}
}
答案 0 :(得分:0)
你需要使用它。
viewPager.setOffscreenPageLimit(3);
答案 1 :(得分:0)
setData()
方法从onCreate()
移至onCreateView()
adapter.notifyDataSetChanged()
之后添加persons = response.body()