我对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);
}
}
}
setData()
方法从onCreate()
移动到onCreateView()的末尾
你需要用这个。
viewPager.setOffscreenPageLimit(3);
我怎么能那么做?我也不知道。我在等你的帮助。谢谢。 我的代码:
为什么我的标签内容没有显示在我的网格窗格中,这很奇怪,其他所有内容都在显示。 我已经尽力了。请帮忙 这里是我的代码片段和附加的结果照片
动态绑定src后,img标签不显示图片,点击右键加载后才显示,这是为什么? 如下图 我在vue中动态绑定的img标签的src属性,其中src是一个服务端返回的网络地址 注:该src地址是我阿里云OSS中保存的图片URL地址,已经绑定了自定义域名,并且允许跨域访问,且没有设置防盗链。浏览器是edge 我在img标签中直接把url写在src中倒是可以显示图片
有没有什么内容只能显示插槽的方法 例如,我正在构建一个简单的<code>卡。vue组件,并且我只希望在页脚槽包含内容时显示页脚: 在视图中: 由于上面的组件不包含页脚,因此不应该呈现它,但它是。 我尝试过使用,但这返回未定义。 有人有什么提示吗?
我正在用Java FX开发应用程序。在这个特定的窗口中,我使用了BorderPane。当按下按钮时,下面提供的代码应该显示在BorderPane的中心区域。 代码正在运行。按下按钮时,代码中的详细信息将显示在中心区域。但问题是两个标签内容都没有完全显示。我将提供一个屏幕截图。 那么,为什么标签的内容没有完全显示?如何使其完全可见?