我有一个Android活动,使用SectionsPagerAdapter显示3个标签,一个用于收到朋友请求,一个用于发送好友请求,一个用于当前好友。
当我打开活动时,它默认为选项卡0,即收到的请求,而是加载来自属于Friends片段的查询(使用parse.com)的结果。 Friends片段通常不显示任何内容,但它在后台运行Sent Request查询。转到“已发送”选项卡时,它会显示正确的选项卡。但是,当我返回其他任一选项卡时,有时它会显示其他选项卡下的“已发送请求”列表,或者根本没有显示任何内容,或者看似随机选择的三个选项卡中的一个。现在,这是我第一次使用选项卡式片段,所以我的问题可能就在那里,但是,我似乎无法找到它。
以下是包含标签的“好友”活动的代码:
public class FriendsActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_friends);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
switch(position){
case 0 : return ReceivedFragment.newInstance();
case 1 : return ConnectionsFragment.newInstance();
case 2 : return SentFragment.newInstance();
}
return null;
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getText(R.string.received);
case 1:
return getText(R.string.friends);
case 2:
return getText(R.string.sent);
}
return null;
}
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_friends, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
}
编辑:这是朋友列表片段的代码:
public class ConnectionsFragment extends Fragment{
public static ConnectionsFragment newInstance() {
ConnectionsFragment fragment = new ConnectionsFragment();
return fragment;
}
public ConnectionsFragment()
{
}
private SwipeMenuListView friendsListView;
private ParseUser currentUser;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_connections, container, false);
currentUser = ParseUser.getCurrentUser();
friendsListView = (SwipeMenuListView) rootView.findViewById(R.id.friendsList);
final ArrayList<SearchResults> searchResults = new ArrayList<SearchResults>();
final ListAdapter listAdapter = new ListAdapter(getActivity(), searchResults);
friendsListView.setAdapter(listAdapter);
ParseQuery<ParseObject> senderQuery = ParseQuery.getQuery("FriendRequest");
senderQuery.whereEqualTo("sender_id", currentUser.getObjectId());
senderQuery.whereEqualTo("status", "accepted");
ParseQuery<ParseObject> receiverQuery = ParseQuery.getQuery("FriendRequest");
receiverQuery.whereEqualTo("receiver_id", currentUser.getObjectId());
receiverQuery.whereEqualTo("status", "accepted");
List<ParseQuery<ParseObject>> queries = new ArrayList<ParseQuery<ParseObject>>();
queries.add(senderQuery);
queries.add(receiverQuery);
final ParseQuery<ParseObject> friendsQuery = ParseQuery.or(queries);
friendsQuery.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
Log.d("USERS", "Retrieved " + objects.size() + " Users");
if (objects.size() > 0) {
for (ParseObject dealsObject : objects) {
ParseUser foundUser = null;
ParseQuery<ParseUser> query = ParseUser.getQuery();
try {
if (dealsObject.get("receiver_id") == currentUser.getObjectId())
{
foundUser = query.get(dealsObject.getString("receiver_id"));
}
else
{
foundUser = query.get(dealsObject.getString("sender_id"));
}
Log.i("NAME IN FRIENDSLIST", foundUser.getString("name"));
} catch (ParseException err) {
Log.e("ERROR", err.getMessage());
}
Log.i("NAME", foundUser.getString("name"));
final SearchResults sr1 = new SearchResults();
sr1.setName(foundUser.getString("name"));
sr1.setSurname1(foundUser.getString("surname1"));
sr1.setSurname2(foundUser.getString("surname2"));
sr1.setUid(foundUser.getObjectId());
Log.d("UID", foundUser.getObjectId());
sr1.setPhone(foundUser.getString("username"));
sr1.setEmail(foundUser.getString("email"));
ParseFile fileObject = (ParseFile) foundUser.get("thumbnail");
try {
byte[] imageBytes = fileObject.getData();
sr1.setImage(BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length));
} catch (ParseException e1) {
Log.e("ERROR", e.getMessage());
}
searchResults.add(sr1);
}
listAdapter.notifyDataSetChanged();
} else if (objects.size() == 0) {
//Toast.makeText(getActivity(), R.string.no_search_results, Toast.LENGTH_LONG).show();
} else {
Log.e("USERS", "Error: " + e.getMessage());
}
}
}
});
friendsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object o = friendsListView.getItemAtPosition(position);
SearchResults fullObject = (SearchResults) o;
Intent i = new Intent(getActivity(),
UserDetailActivity.class);
i.putExtra("uid", fullObject.getUid());
startActivity(i);
}
});
return rootView;
}
}
答案 0 :(得分:0)
内部
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
switch(position){
case 0 : return ReceivedFragment.newInstance();
case 1 : return ConnectionsFragment.newInstance();
case 2 : return SentFragment.newInstance();
}
return null;
每次滑动时,您都在创建每个片段的新实例。我建议在每个片段中使用onResume(),并在那里解析你的信息,以避免不必要的活动创建。
public static class RecievedFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){}
public void onResume(){
super.onResume();
//Parse Info to this frag here
}
}
并删除getItem()中的.newInstance()
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
switch(position){
case 0 : return ReceivedFragment;
case 1 : return ConnectionsFragment;
case 2 : return SentFragment;
}
return null;
我怀疑您的问题可能在于如何将数据解析为每个片段。你在使用一个共享变量吗?您可以发布代码,显示您在解析中的处理方式吗?