我正在构建一个ListFragment。在肖像中它需要两个标题,但在横向上我需要左侧的这些标题和右侧的ListView。 xml处理不同的布局,java处理正确填充/更新它们。
除了在方向更改时,标题不会保留在列表中。 listView甚至可以保留滚动,但是通过删除标题来移动。
嵌套片段可能更好,但listview不会保持framelayout,因此这是一个有争议的点。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
loadFeeds();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_profile, container, false);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
setupList(savedInstanceState);
setupOrienation();
super.onActivityCreated(savedInstanceState);
}
private void setupList(Bundle savedInstanceState) {
if(getListAdapter() == null) {
listProfileButtons = (View)getLayoutInflater(savedInstanceState).inflate(R.layout.view_profile_buttons, null);
listProfile = (View)getLayoutInflater(savedInstanceState).inflate(R.layout.view_profile_header, null);
getListView().addHeaderView(listProfile);
getListView().addHeaderView(listProfileButtons);
}
}
private void setupOrienation(){
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
listProfile.setVisibility(View.GONE);
listProfileButtons.setVisibility(View.GONE);
setupProfile(getView());
setupButtons(getView());
getListView().setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
});
} else {
listProfile.setVisibility(View.VISIBLE);
listProfileButtons.setVisibility(View.VISIBLE);
//setupProfile(getListView());
//setupButtons(getListView());
setupButtons(getView().findViewById(R.id.include_profile_buttons));
getListView().setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if(firstVisibleItem > 0) {
getView().findViewById(R.id.include_profile_buttons).setVisibility(View.VISIBLE);
} else {
getView().findViewById(R.id.include_profile_buttons).setVisibility(View.GONE);
}
}
});
}
}
private void setupProfile(View parent) {
Bundle userBundle = getArguments();
TextView name = (TextView)parent.findViewById(R.id.text_view_profile_name);
TextView screenName = (TextView)parent.findViewById(R.id.text_view_profile_at_user);
ImageView image = (ImageView)parent.findViewById(R.id.image_view_profile_picture);
name.setText(userBundle.getString(User.USER_NAME));
screenName.setText("@" + userBundle.get(User.SCREEN_NAME));
ImageCacheManager.getInstance(getActivity()).bindDrawable(userBundle.getString(User.PROFILE_IMAGE), image);
}
private void setupButtons(View v) {
Bundle userBundle = getArguments();
Button buttonTweets = (Button)v.findViewById(R.id.button_number);
Button buttonFollowing = (Button)v.findViewById(R.id.button_follow);
Button buttonFollowers = (Button)v.findViewById(R.id.button_friends);
buttonTweets.setText(getResources().getString(R.string.profile_number, userBundle.getInt(User.NUMBER)));
buttonFollowing.setText(getResources().getString(R.string.profile_number_follow, userBundle.getInt(User.NUMBER_OF_FOLLOW)));
buttonFollowers.setText(getResources().getString(R.string.profile_number_friends, userBundle.getInt(User.NUMBER_OF_FRIENDS)));
buttonTweets.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switchListAdapter(adapter);
}
});
buttonFollowing.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switchListAdapter(followAdapter);
}
});
buttonFollowers.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switchListAdapter(friendsAdapter);
}
});
}
完成注释代码,以便在标头丢失后停止空指针异常。
答案 0 :(得分:2)
因此在轮换时,所有视图都会被销毁,这意味着标题。我相信当你setRatainInstance(true)时它应该处理ListFragment头中的头但不会。要使用带有使用retainInstance的ListFragment的Headers,您必须:
@Override
public void onDestroyView() {
setListAdapter(null);
super.onDestroyView();
}
然后我只在我需要的方向添加标题,这很好。关于它的负面部分是scroll isn't retained,我必须弄清楚应该使用哪个适配器。这些只是几种方法,虽然不是太糟糕。