我是初学者,我参与了一些我正在研究的项目。我找不到任何可以帮助我解决问题的答案。这就是我在做的事情。 我有主要的Activity(GitHub登录活动),在那里我将转到第二个Activity并加载4个碎片(用户碎片,存储库碎片,追随者碎片和碎片)。问题是,当我使用View寻呼机时,它不会加载片段。当我使用Fragment事务加载任何片段时,一切都按预期正常工作。我真的不知道我的代码在哪里弄错了。
public class DetailPageAdapter extends FragmentPagerAdapter {
public static final String[] fragments = {"Profile", "Repository"}; //, "Followers", "Following"};
public DetailPageAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new UserProfileFragment();
case 1:
return new UserRepositoryFragment();
// case 2:
// return new UserFollowersFragment();
// case 3:
// return new UserFollowingFragment();
}
return null;
}
@Override
public int getCount() {
return fragments.length;
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return fragments[position];
}
}
public class DetailUserActivity extends AppCompatActivity {
private ViewPager viewPager;
private Toolbar toolbar;
private TabLayout tabs;
private DetailPageAdapter adapter;
private LinearLayout fragmentContainer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
fragmentContainer = findViewById(R.id.fragmentContainer);
toolbar = new Toolbar(this);
setSupportActionBar(toolbar);
tabs = findViewById(R.id.tabs);
viewPager = findViewById(R.id.viewPager);
adapter = new DetailPageAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
tabs.setupWithViewPager(viewPager);
}
}
public class UserProfileFragment extends Fragment {
public static final String NAME_ORIGINAL_STATE = "NAME_ORIGINAL_STATE";
public static final String LOGIN_ORIGINAL_STATE = "LOGIN_ORIGINAL_STATE";
public static final String LOCATION_ORIGINAL_STATE = "LOCATION_ORIGINAL_STATE";
public static final String AVATAR_ORIGINAL_URL = "AVATAR_ORIGINAL_URL";
private ImageView avatar;
private TextView name, login, location;
private String imgUrl;
public UserProfileFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_user_profile, container, false);
avatar = view.findViewById(R.id.avatar);
name = view.findViewById(R.id.name);
location = view.findViewById(R.id.location);
login = view.findViewById(R.id.login);
if(savedInstanceState == null) {
displayContent();
} else {
restorePreviousValues(savedInstanceState);
}
return view;
}
private void restorePreviousValues(Bundle savedInstanceState) {
name.setText(savedInstanceState.getString(NAME_ORIGINAL_STATE));
location.setText(savedInstanceState.getString(LOCATION_ORIGINAL_STATE));
login.setText(savedInstanceState.getString(LOGIN_ORIGINAL_STATE));
GlideApp
.with(getContext())
.load(savedInstanceState.getString(AVATAR_ORIGINAL_URL))
.into(avatar);
}
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
outState.putString(NAME_ORIGINAL_STATE, name.getText().toString());
outState.putString(LOCATION_ORIGINAL_STATE, location.getText().toString());
outState.putString(LOGIN_ORIGINAL_STATE, login.getText().toString());
outState.putString(AVATAR_ORIGINAL_URL, imgUrl);
}
private void displayContent() {
GitHubUserEndPoint apiService = APIClient.getClient().create(GitHubUserEndPoint.class);
Call<GitHubUser> call = apiService.getUser(user);
call.enqueue(new Callback<GitHubUser>() {
@Override
public void onResponse(Call<GitHubUser> call, Response<GitHubUser> response) {
if(response.body() != null){
try {
name.setText(response.body().getName());
login.setText(response.body().getLogin());
location.setText(response.body().getLocation());
imgUrl = response.body().getAvatar();
GlideApp
.with(getContext())
.load(imgUrl)
.into(avatar);
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public void onFailure(Call<GitHubUser> call, Throwable t) {
}
});
}
}
答案 0 :(得分:1)
ViewPager与普通片段有点不同,你需要覆盖 setUserVisibleHint 函数:
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (!isVisibleToUser) return;
loadUI();
}