我有一个包含ListView
的Android片段,我已将ArrayAdapter
附加到ListView
,但当我将项目插入ArrayAdapter
时,{{ 1}}仅显示第一项。
ListView
对我的技术进行迭代,如果它们已解锁,则会将它们添加到onCreateView
,但只会显示第一个项目
ArrayList shownTechs
}
当我谷歌搜索类似的问题时,人们似乎正在重新创建ArrayAdapter而不是编辑现有的ArrayAdapter,但我不这样做,并且无法弄清楚仍然存在什么问题。有什么建议吗?
fragment_technology_list.xml:
public class TabTechnologyList extends Fragment {
private static final String TAB_NAME = "tab_name";
private ListView techList;
private ArrayList<Technology> shownTechs = new ArrayList<>();
ArrayAdapter<Technology> techListAdapter;
public TabTechnologyList() { }
public static TabTechnologyList newInstance() {
TabTechnologyList fragment = new TabTechnologyList();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tabData = Globals.getTabData();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_technology_list, container, false);
techList = (ListView) view.findViewById(R.id.tabTechListList);
techListAdapter = new ArrayAdapter<>(getActivity().getApplicationContext(), R.layout.layout_techlist_element, shownTechs);
techList.setAdapter(techListAdapter);
for(Technology tech: tabData.getTechnologies()) {
shownTechs.add(tech);
}
//Only the first one is shown on the screen
return view;
}
}
layout_techlist_element.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.tbohne.kittens.front.TabTechnologyList"
>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tabTechListList" />
</RelativeLayout>
加成
我刚刚意识到这个片段不填充父高度,并且只有足够高才能显示一个条目,就像片段高度为<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TextView>
一样。 (我无法滚动它,所以看起来好像只有一个)。这让我觉得片段的嵌套可能是相关的,所以这里是创建片段的代码:
wrap_content
fragment_technology.xml
public class TabTechnology extends Fragment {
private TabTechnologyList table;
private OnFragmentInteractionListener mListener;
public TabTechnology() {}
public static TabTechnology newInstance(String tabName) {[SNIP]}
@Override public static onAttach(Activity activity) {[SNIP]}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_technology, container, false);
table = TabTechnologyList.newInstance(tabName);
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.view_table, table);
transaction.commit();
return view;
}
}
答案 0 :(得分:0)
将片段放在ScrollView
内是问题的根源。简单地用FrameLayout
替换那些,突然一切都神奇地起作用。很奇怪。
我怀疑这个问题与嵌套可滚动对象有关。