我有一个基本活动和一个包含项目列表的片段。当用户点击列表中的项目时,片段事务开始,并且带有回收器视图的当前片段被添加到后台堆栈并替换为其他片段。但是,在返回之后,它会破坏回收者视图并重新加载它。我如何防止回收者的观点被破坏?
修改
我有循环器视图子类 -
public class AppListView extends RecyclerView {
private LinearLayoutManager linearLayoutManager;
public boolean isParent() {
return isParent;
}
public void setParent(boolean isParent) {
this.isParent = isParent;
}
private boolean isParent = false;
public AppListView(Context context) {
super(context);
}
public AppListView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public AppListView(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent e) {
int startScrollState = getScrollState();
if (e.getAction() == MotionEvent.ACTION_DOWN && startScrollState == SCROLL_STATE_SETTLING) {
stopScroll();
}
boolean result = super.onInterceptTouchEvent(e);
return result;
}
@Override
public boolean onTouchEvent(MotionEvent e) {
boolean result = super.onTouchEvent(e);
return result;
}
public LinearLayoutManager getLinearLayoutManager() {
return linearLayoutManager;
}
public void setLinearLayoutManager(LinearLayoutManager linearLayoutManager) {
setLayoutManager(linearLayoutManager);
this.linearLayoutManager = linearLayoutManager;
}
}
并将其添加到片段:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(isRetainInstance());
if (!isSignedUpEventBus) {
getEventBus().register(this);
isSignedUpEventBus = true;
}
setViews();
setActions();
}
像这样添加:
@Override
protected void setViews() {
listH = 0;
list = (AppListView) getView().findViewById(R.id.list);
layoutManager = new LinearLayoutManager(getBaseActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
list.setLinearLayoutManager(layoutManager);
list.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
listH = list.getHeight();
listW = list.getWidth();
initData();
if (Build.VERSION.SDK_INT < 16) {
list.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
list.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
}
});
list.setParent(true);
}