gpt4 book ai didi

android - ViewPager 不显示第一个 fragment ,如果它的内容是动态加载的

转载 作者:行者123 更新时间:2023-11-30 01:42:32 24 4
gpt4 key购买 nike

我在 android 应用程序中构建带有 fragment 的 viewPager 时遇到了一个非常奇怪的问题。我有一个构建布局和两个 fragment 的 Activity :每个 fragment 都包含 RecyclerView 并从服务器动态加载其内容。当我用 fragment 的静态内容测试它时,一切都很好,但是当我切换到预先测试和工作的动态内容 fragment 时,ViewPager 从不加载第一个,只加载第二个。如果我交换它们,viewPager 仍然只加载第二个。在第一个选项卡中,我总是只看到一个白色的空白字段。任何帮助将不胜感激。

Activity 部分:

    tabLayout = (TabLayout) findViewById(R.id.contacts_tabs_layout);
adapter = new ContactsPagerAdapter(getSupportFragmentManager());

viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);

Activity 布局:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_parent"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" />

<android.support.design.widget.TabLayout
android:id="@+id/contacts_tabs_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMaxWidth="0dp"
app:tabGravity="fill"
app:tabMode="scrollable">

</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
android:id="@+id/view_pager_contacts_list"
android:layout_width="match_parent"
android:layout_height="fill_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

fragment 布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/contacts_list"
android:layout_width="fill_parent"
android:layout_height="match_parent" />

fragment 相关代码:

 contactsList = (RecyclerView) v.findViewById(R.id.contacts_list);
contactsList.setLayoutManager(new LinearLayoutManager(getActivity()));
adapter = new ContactsListAdapter(getActivity());
contactsList.setAdapter(adapter);


controller.getContacts(new CommunicationListener() {
@Override
public void onFileReceived(FilePath filePath, byte[] data) {
System.out.println("RECEIVED CONTACTS DATA FILE WITH SIZE OF " + data.length);
final Contact c = Utils.deserializeContact(data);

new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
adapter.add(c);
}
});
}
});
return v;

适配器部分:

 public ContactsPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);

}


@Override
public Fragment getItem(int position)
{
switch(position){
case 0:
return new ContactsListFragment();
case 1:
return new TestFragment();
default:
return null;
}
}

@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}

@Override
public int getCount() {
return 2;
}

最佳答案

@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}

来自 PagerAdapter 的 javadoc POSITION_NONE 表示该项目不再存在。

/** * Called when the host view is attempting to determine if an item's position * has changed. Returns {@link #POSITION_UNCHANGED} if the position of the given * item has not changed or {@link #POSITION_NONE} if the item is no longer present * in the adapter. * *

The default implementation assumes that items will never * change position and always returns {@link #POSITION_UNCHANGED}. * * @param object Object representing an item, previously returned by a call to * {@link #instantiateItem(View, int)}. * @return object's new position index from [0, {@link #getCount()}), * {@link #POSITION_UNCHANGED} if the object's position has not changed, * or {@link #POSITION_NONE} if the item is no longer present. */

您应该返回项目的位置或POSITION_UNCHANGED

还有:

`PagerAdapter` supports data set changes. Data set changes must occur on the * `main thread` and must end with a call to {@link #notifyDataSetChanged()} similar * to AdapterView adapters derived from {@link android.widget.BaseAdapter}. A data * set change may involve pages being added, removed, or changing position. The * ViewPager will keep the current page active provided the adapter implements * the method {@link #getItemPosition(Object)}.

添加 fragment 后需要调用notifyDatasetChanged()

关于android - ViewPager 不显示第一个 fragment ,如果它的内容是动态加载的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34252191/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com