我有一个问题,因为当我在 fragment 中调用它时,getTag() 方法返回 null, fragment 是我创建的 ViewPager 选项卡布局的一部分。
代码
import java.util.ArrayList;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;
public class HistoryFragment extends Fragment {
ListView listView;
HistoryAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View history = inflater.inflate(R.layout.fragment_history, container, false);
ArrayList<ToSave> arrayOfData = new ArrayList<ToSave>();
adapter = new HistoryAdapter(getActivity().getBaseContext(), arrayOfData);
listView = (ListView) history.findViewById (R.id.listView1);
listView.setAdapter(adapter);
String myTag = getTag();
((MainActivity)getActivity()).setHistoryFragment(myTag);
Toast.makeText(getActivity(), "HistoryFragment.onCreateView(): " + myTag, Toast.LENGTH_LONG).show();
return history;
}
}
我想用它在 Fragment 之间进行通信(通过单击另一个 Fragment 中的按钮添加 ListView 项目),但我无法使其工作。
编辑
TabPagerAdapter.java
@Override
public Fragment getItem(int i) {
switch (i) {
case 0:
if (mFragmentAtPos0 ==null) {
mFragmentAtPos0 = new PartyFragment(listener);
}
return mFragmentAtPos0;
case 1:
return new SummaryFragment();
case 2:
return new HistoryFragment();
}
return null;
}
最佳答案
Fragment
标签不会自动指定。你必须自己分配它们。根据您附加 fragment 的方式,有几个地方可以执行此操作:在 XML 中或动态地进行。
如果它是在 XML 中定义的,那么您可以这样设置它:
<fragment android:name="com.example.news.ArticleListFragment"
android:id="@+id/list"
android:tag="your_tag"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
如果你动态添加 fragment ,那么你可以这样做:
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment, "your_tag");
fragmentTransaction.commit();
在这两个例子中寻找“your_tag”。
然后,当您在 Fragment
上调用 getTag()
时,您将得到 "your_tag" 作为结果。
如果使用将您的 fragment 提供给 FragmentPagerAdapter
,标签将根据 getItemId(int)
自动分配,默认返回寻呼机位置。所以调用 getTag()
会返回 fragment 在 ViewPager
中的位置。
在使用 FragmentStatePagerAdapter
的情况下,NOT 指定了 fragment 标签。如果是这种情况,您必须切换到第一种适配器类型或使用其他方式来引用您的 fragment 。
从您的适配器实现中我知道您只有 3 个页面,因此 FragmentPagerAdapter
更适合您。
作为证明,这是来自 FragmentStatePagerAdapter
的部分描述:
This version of the pager is more useful when there are a large number of pages, working more like a list view.
FragmentPagerAdapter
的下一个描述:
This version of the pager is best for use when there are a handful of typically more static fragments to be paged through, such as a set of tabs.
如果您坚持使用 FragmentStatePagerAdapter,请继续阅读:
我假设您以后需要标签来找到您的 fragment 。相反,标签保留对 fragment 的引用。您的 Activity 方法 ((MainActivity)getActivity()).setHistoryFragment(myTag);
已经需要特定类型的 fragment 。所以你可以这样调用它 ((MainActivity)getActivity()).setHistoryFragment(this);
稍后,不是搜索 fragment ,而是检查它是否不为 null 并使用它。
关于android - 在 fragment 中调用 getTag() 时总是返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25313859/