Firebase 无适配器连接;跳过布局,仅第一次显示每个片段(Tablayout)
我正在使用带有RecyclerView的片段的选项卡布局。这里有一个问题,如果我只使用一个或两个标签,它的工作都很好但是如果我超过了2个标签,在第一次加载布局时,但如果我开始在标签之间切换,则内容将消失,并在logcat中显示一条消息“未连接适配器;跳过布局”,您可以看到,在tab1和tab3消失后,它开始工作正常。。 我已经搜索了很长一段时间,关于这个问题有很多问题,但我认为我的情况不同,如果我的代码中有问题,那么它不应该第一次加载,我还想在这里提到,Firebase 无适配器连接;跳过布局,仅第一次显示每个片段(Tablayout),firebase,android-fragments,android-recyclerview,adapter,Firebase,Android Fragments,Android Recyclerview,Adapter,我正在使用带有RecyclerView的片段的选项卡布局。这里有一个问题,如果我只使用一个或两个标签,它的工作都很好但是如果我超过了2个标签,在第一次加载布局时,但如果我开始在标签之间切换,则内容将消失,并在logcat中显示一条消息“未连接适配器;跳过布局”,您可以看到,在tab1和tab3消失后,它开始工作正常。。 我已经搜索了很长一段时间,关于这个问题有很多问题,但我认为我的情况不同,如果我的代码中有问题,那么它不应该第一次加载,我还想在这里提到,posType是我的可验证的,它对于所有选
posType
是我的可验证的,它对于所有选项卡都是不同的,rest代码是相同的。适配器连接到firebase,并为不同的变量选择不同的内容。对不起,我是初学者,请指导我的错误或拙劣的解释。这是我的密码
public class Tab4Fragment extends android.support.v4.app.Fragment {
@Nullable
@Override
public View onCreateView( @NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab4_fragment, container, false);
posType = "Quiz";
profileManager = ProfileManager.getInstance(getActivity());
postManager = PostManager.getInstance(getActivity());
floatingActionButton = (FloatingActionButton) view.findViewById(R.id.addNewPostFab);
if (recyclerView == null) {
if (floatingActionButton != null) {
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick( View v ) {
if (hasInternetConnection()) {
addPostClickAction();
} else {
showFloatButtonRelatedSnackBar(R.string.internet_connection_failed);
}
}
});
}
newPostsCounterTextView = (TextView) view.findViewById(R.id.newPostsCounterTextView);
newPostsCounterTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick( View v ) {
refreshPostList((posType));
}
});
final ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
SwipeRefreshLayout swipeContainer = (SwipeRefreshLayout) view.findViewById(R.id.swipeContainer);
recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
postsAdapter = new PostsAdapter((MainActivity) getActivity(), swipeContainer, "Quiz");
postsAdapter.setCallback(new PostsAdapter.Callback() {
@Override
public void onItemClick( final Post post, final View view ) {
PostManager.getInstance(getActivity()).isPostExistSingleValue(post.getId(), new OnObjectExistListener<Post>() {
@Override
public void onDataChanged( boolean exist ) {
if (exist) {
openPostDetailsActivity(post, view);
} else {
showFloatButtonRelatedSnackBar(R.string.error_post_was_removed);
}
}
},posType);
}
@Override
public void onListLoadingFinished() {
progressBar.setVisibility(View.GONE);
}
@Override
public void onAuthorClick( String authorId, View view ) {
openProfileActivity(authorId, view);
}
@Override
public void onCanceled( String message ) {
progressBar.setVisibility(View.GONE);
Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
}
});
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
recyclerView.setAdapter(postsAdapter);
postsAdapter.loadFirstPage((posType));
updateNewPostCounter();
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled( RecyclerView recyclerView, int dx, int dy ) {
hideCounterView();
super.onScrolled(recyclerView, dx, dy);
}
});
}
postCounterWatcher = new PostManager.PostCounterWatcher() {
@Override
public void onPostCounterChanged( int newValue ) {
updateNewPostCounter();
}
};
postManager.setPostCounterWatcher(postCounterWatcher);
return view;
}
@Override
public void onActivityResult( int requestCode, int resultCode, Intent data ) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case ProfileActivity.CREATE_POST_FROM_PROFILE_REQUEST:
refreshPostList((posType));
break;
case CreatePostActivity.CREATE_NEW_POST_REQUEST:
refreshPostList((posType));
showFloatButtonRelatedSnackBar(R.string.message_post_was_created);
break;
case PostDetailsActivity.UPDATE_POST_REQUEST:
if (data != null) {
PostStatus postStatus = (PostStatus) data.getSerializableExtra(PostDetailsActivity.POST_STATUS_EXTRA_KEY);
if (postStatus.equals(PostStatus.REMOVED)) {
postsAdapter.removeSelectedPost();
showFloatButtonRelatedSnackBar(R.string.message_post_was_removed);
} else if (postStatus.equals(PostStatus.UPDATED)) {
postsAdapter.updateSelectedPost((posType));
}
}
break;
}
}
}
@Override
public void onResume() {
super.onResume();
updateNewPostCounter();
}
private void updateNewPostCounter() {
Handler mainHandler = new Handler(getActivity().getMainLooper());
mainHandler.post(new Runnable() {
@Override
public void run() {
int newPostsQuantity = postManager.getNewPostsCounter();
if (newPostsCounterTextView != null) {
if (newPostsQuantity > 0) {
showCounterView();
String counterFormat = getResources().getQuantityString(R.plurals.new_posts_counter_format, newPostsQuantity, newPostsQuantity);
newPostsCounterTextView.setText(String.format(counterFormat, newPostsQuantity));
} else {
hideCounterView();
}
}
}
});
}
public void doAuthorization(ProfileStatus status) {
if (status.equals(ProfileStatus.NOT_AUTHORIZED) || status.equals(ProfileStatus.NO_PROFILE)) {
startLoginActivity();
}
}
private void startLoginActivity() {
Intent intent = new Intent(getActivity(), LoginActivity.class);
startActivity(intent);
}
private void openCreatePostActivity() {
Intent intent = new Intent(getActivity(), CreatePostActivity.class);
intent.putExtra("posType", posType);
startActivityForResult(intent, CreatePostActivity.CREATE_NEW_POST_REQUEST);
}
private void addPostClickAction() {
ProfileStatus profileStatus = profileManager.checkProfile();
if (profileStatus.equals(ProfileStatus.PROFILE_CREATED)) {
openCreatePostActivity();
} else {
doAuthorization(profileStatus);
}
}
public void showSnackBar(View view, int messageId) {
Snackbar.make(getView().findViewById(R.id.main_content),
messageId, Snackbar.LENGTH_LONG).show();
}
public void showSnackBar(String message) {
Snackbar.make(getView().findViewById(R.id.main_content),
message, Snackbar.LENGTH_LONG).show();
}
public void showSnackBar(int messageId) {
Snackbar.make(getView().findViewById(R.id.main_content),
messageId, Snackbar.LENGTH_LONG).show();
}
public void showFloatButtonRelatedSnackBar( int messageId ) {
showSnackBar(floatingActionButton, messageId);
}
private void hideCounterView() {
if (!counterAnimationInProgress && newPostsCounterTextView.getVisibility() == View.VISIBLE) {
counterAnimationInProgress = true;
AlphaAnimation alphaAnimation = AnimationUtils.hideViewByAlpha(newPostsCounterTextView);
alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart( Animation animation ) {
}
@Override
public void onAnimationEnd( Animation animation ) {
counterAnimationInProgress = false;
newPostsCounterTextView.setVisibility(View.GONE);
}
@Override
public void onAnimationRepeat( Animation animation ) {
}
});
alphaAnimation.start();
}
}
private void openPostDetailsActivity( Post post, View v ) {
Intent intent = new Intent(getActivity(), PostDetailsActivity.class);
intent.putExtra("posType", posType);
intent.putExtra(PostDetailsActivity.POST_ID_EXTRA_KEY, post.getId());
if ((new String(posType).equals("Question")) || (new String(posType).equals("Quiz"))){
startActivityForResult(intent, PostDetailsActivity.UPDATE_POST_REQUEST);
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
View imageView = v.findViewById(R.id.postImageView);
View authorImageView = v.findViewById(R.id.authorImageView);
ActivityOptions options = ActivityOptions.
makeSceneTransitionAnimation(getActivity(),
new android.util.Pair<>(imageView, getString(R.string.post_image_transition_name)),
new android.util.Pair<>(authorImageView, getString(R.string.post_author_image_transition_name))
);
startActivityForResult(intent, PostDetailsActivity.UPDATE_POST_REQUEST, options.toBundle());
} else {
startActivityForResult(intent, PostDetailsActivity.UPDATE_POST_REQUEST);
}
}
}
public boolean hasInternetConnection() {
ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
}
@SuppressLint("RestrictedApi")
private void openProfileActivity( String userId, View view ) {
Intent intent = new Intent(getActivity(), ProfileActivity.class);
intent.putExtra(ProfileActivity.USER_ID_EXTRA_KEY, userId);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && view != null) {
View authorImageView = view.findViewById(R.id.authorImageView);
ActivityOptions options = ActivityOptions.
makeSceneTransitionAnimation(getActivity(),
new android.util.Pair<>(authorImageView, getString(R.string.post_author_image_transition_name)));
startActivityForResult(intent, ProfileActivity.CREATE_POST_FROM_PROFILE_REQUEST, options.toBundle());
} else {
startActivityForResult(intent, ProfileActivity.CREATE_POST_FROM_PROFILE_REQUEST);
}
}
private void showCounterView() {
AnimationUtils.showViewByScaleAndVisibility(newPostsCounterTextView);
}
private void refreshPostList(String posType) {
postsAdapter.loadFirstPage((posType));
if (postsAdapter.getItemCount() > 0) {
recyclerView.scrollToPosition(0);
}
}
}
public类Tab4Fragment扩展了android.support.v4.app.Fragment{
@可空
@凌驾
创建视图时的公共视图(@NonNull LayoutInflater inflater、@Nullable ViewGroup container、@Nullable Bundle savedInstanceState){
视图=充气机。充气(R.layout.tab4_碎片,容器,错误);
posType=“测验”;
profileManager=profileManager.getInstance(getActivity());
postManager=postManager.getInstance(getActivity());
floatingActionButton=(floatingActionButton)view.findViewById(R.id.addNewPostFab);
如果(recyclerView==null){
if(floatingActionButton!=null){
floatingActionButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
如果(hasInternetConnection()){
addPostClickAction();
}否则{
showFloatButtonRelatedSnackBar(R.string.internet\u连接\u失败);
}
}
});
}
NewPostSconterTextView=(TextView)view.findViewById(R.id.NewPostSconterTextView);
NewPostsUserTextView.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
刷新postlist((posType));
}
});
final ProgressBar ProgressBar=(ProgressBar)view.findviewbyd(R.id.ProgressBar);
SwipeRefreshLayout swipeContainer=(SwipeRefreshLayout)view.findViewById(R.id.swipeContainer);
recyclerView=(recyclerView)视图.findViewById(R.id.recycler\u视图);
postsAdapter=新的postsAdapter((MainActivity)getActivity(),swipContainer,“测验”);
setCallback(新的postsAdapter.Callback(){
@凌驾
公共虚空onItemClick(最终张贴,最终视图){
PostManager.getInstance(getActivity()).isPostExistSingleValue(post.getId(),新OnObject ExistListener(){
@凌驾
公共无效onDataChanged(布尔值存在){
如果(存在){
openPostDetailsActivity(发布、查看);
}否则{
showFloatButtonRelatedSnackBar(R.string.error\u post\u已删除);
}
}
},后型);
}
@凌驾
public void onListLoadingFinished(){
progressBar.setVisibility(View.GONE);
}
@凌驾
AuthorClick上的公共void(字符串authorId,视图){
openProfileActivity(编写、查看);
}
@凌驾
已取消的公共void(字符串消息){
progressBar.setVisibility(View.GONE);
Toast.makeText(getActivity(),message,Toast.LENGTH_LONG).show();
}
});
setLayoutManager(新的LinearLayoutManager(getActivity());
((SimpleItemImator)recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
recyclerView.setAdapter(postsAdapter);
postsAdapter.loadFirstPage((posType));
updateNewPostCounter();
recyclerView.addOnScrollListener(新的recyclerView.OnScrollListener(){
@凌驾
已填空的公共空间(RecyclerView RecyclerView、int dx、int dy){
hideCounterView();
super.onScrolled(recyclerView、dx、dy);
}
});
}
postCounterWatcher=新的邮局管理员。postCounterWatcher(){
@凌驾
PostCounterChanged上的公共无效(int newValue){
updateNewPostCounter();
}
};
邮政管理人员setPostCounterWatcher(postCounterWatcher);
返回视图;
}
@凌驾
ActivityResult上的公共void(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
if(resultCode==RESULT\u OK){
开关(请求代码){
案例档案活动。根据档案申请创建档案发布:
刷新postlist((posType));
打破
案例CreatePostActivity.CREATE_NEW_POST_请求:
刷新postlist((posType));
showFloatButtonRelatedSnackBar(R.string.message\u post\u已创建);
打破
案例PostDetailsActivity.UPDATE\u POST\u请求:
如果(数据!=null){
PostStatus PostStatus=(PostStatus)data.getSerializableExtra(PostDetailsActivity.POST_STATUS_EXTR)