我试图在TabLayout中的片段中显示列表视图中的注册用户,但我的问题是当我从一个标签布局滑动到另一个标签布局然后返回到第一个它复制相同的用户列表并重复添加到该列表中每当我滑动到其他标签布局时,都会继续。我怎样才能解决这个问题? UserListFragment.java
public class UserlistFragment extends Fragment {
private static final String TAG = "UserList";
ListView usersList;
private DatabaseReference mDatabaseUser;
ArrayList<String> list=new ArrayList<>();
private DatabaseReference root = FirebaseDatabase.getInstance().getReference().getRoot();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.userlist_fragment,container,false);
mDatabaseUser = FirebaseDatabase.getInstance().getReference("users");
usersList = (ListView)view.findViewById(R.id.usersList);
final ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_dropdown_item_1line,list);
usersList.setAdapter(adapter);
usersList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String s = usersList.getItemAtPosition(i).toString();
/* Intent intent = new Intent(getApplicationContext(),Chat_Room.class);
intent.putExtra("user_name",s);
startActivity(intent);*/
}
});
mDatabaseUser.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
/*list.add((String) dataSnapshot.getValue(String.class));*/
User user = dataSnapshot.getValue(User.class);
// Check for null
if (user == null) {
// Log.e(TAG, "User data is null!");
return;
}
list.add(user.username);
adapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return view;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
private static final String TAG ="MainActivity";
private SectionPageAdapter mSectionPageAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionPageAdapter = new SectionPageAdapter(getSupportFragmentManager());
mViewPager = (ViewPager)findViewById(R.id.container);
setUpViewPager(mViewPager);
TabLayout tabLayout = (TabLayout)findViewById(R.id.tab);
tabLayout.setupWithViewPager(mViewPager);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
private void setUpViewPager(ViewPager viewPager){
SectionPageAdapter adapter = new SectionPageAdapter(getSupportFragmentManager());
adapter.addFragment(new UserlistFragment(), "Users");
adapter.addFragment(new GroupFragment(), "Group");
adapter.addFragment(new ProfileFragment(),"Profile");
viewPager.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_logout) {
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(this, SignActivity.class));
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:0)
我没有收到你的回复,所以无论如何我都会尝试回答。
问题可能是您的ChildEventListener正在被创建并一次又一次地重新添加而不会被删除。这可能是因为每次刷到它时都会不必要地重新创建片段(因为它在被刷掉时没有被保存在内存中)和/或因为firebase将mDatabaseUser对象以某种方式视为静态而需要你即使从内存中删除了包含该对象的片段,也要删除侦听器。
有两种可能的解决方案。你可以使用其中一个,或者两个都使用:
1)如果您使用的是ViewPager,请添加以下代码:
int temp = // the number of tabs in your layout - 1
viewPager.setOffscreenPageLimit(temp);
这将确保片段在屏幕外时保留在内存中,因此当您向后滑动时,onCreateView()方法将不会再次执行。
2)对您的片段进行以下更改:
- 更改声明侦听器的方式,以便您可以引用它:
ChildEventListener derp = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
/*list.add((String) dataSnapshot.getValue(String.class));*/
User user = dataSnapshot.getValue(User.class);
// Check for null
if (user == null) {
// Log.e(TAG, "User data is null!");
return;
}
list.add(user.username);
adapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
mDatabaseUser.addChildEventListener(derp);
- 添加以下方法:
@Override
public void onDetach() {
super.onDetach();
mDatabaseUser.removeEventListener(derp);
}
这将确保无论出于何种原因从内存中删除片段,都将删除侦听器。
希望有所帮助。