【发布时间】:2014-04-18 00:22:08
【问题描述】:
首先让我声明,我知道将ListView 包含在ScrollView 中会令人不悦。但是,通过使用 Activity 类,我的布局工作得很好。我现在尝试通过使用 Fragments 来做同样的事情,但无法到达那里。
片段之间的交互工作正常,listView 从数据输入屏幕填充。然而,ListView 似乎折叠成几行。为了补偿,我试图重新调整 ListView 的大小,使其具有 arrayList 的高度加上分隔符。为此,我需要获取对列表视图的引用,但我没有这样做。
问题似乎在于试图通过返回 null 的(ListView)findViewById(android.R.id.list) 或返回 view not yet created 错误的MySkeletonFragmentActivity.this.resultsFragment.getListView() 获取对 ListView 的引用。 (寻找resultsListAlt)。
主要活动 这管理片段之间的交互
public class MySkeletonFragmentActivity extends Activity implements DataEntryFragment.OnDataSubmitListener
{
private ArrayAdapter<String> aa;
private ArrayList<String> results;
private String distance;
private ResultsFragment resultsFragment;
private DataEntryFragment dataEntryFragment;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
initialiseView();
//setContentView(R.layout.main);
FragmentManager fragmentManager = getFragmentManager();
resultsFragment = (ResultsFragment)fragmentManager.findFragmentById(R.id.results_container);
dataEntryFragment = (DataEntryFragment)fragmentManager.findFragmentById(R.layout.data_entry_fragment);
if(resultsFragment == null || dataEntryFragment == null)
{
FragmentTransaction fT = fragmentManager.beginTransaction();
fT.replace(R.id.data_entry_fragment_container, new DataEntryFragment());
resultsFragment = new ResultsFragment();
fT.replace(R.id.results_container, resultsFragment );
fT.addToBackStack(null);
fT.commit();
results = new ArrayList<String>();
aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results);
resultsFragment.setListAdapter(aa);
ListView resultsList = (ListView)findViewById(android.R.id.list);
ListView resultsListAlt = MySkeletonFragmentActivity.this.resultsFragment.getListView(); // view not yet created error
//set height of listView
int height = 0;
for (int i = 0; i<results.size();i++)
{
View arrayItem = aa.getView(i, null, resultsList);
arrayItem.measure(0,0);
height += arrayItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = resultsList.getLayoutParams(); // resultsList == null
params.height = height + (resultsList.getDividerHeight() * (results.size() -1));
resultsList.setLayoutParams(params);
resultsList.requestLayout();
}
结果片段 XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/results_fragment_layout"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/results"
android:textSize="@dimen/header_text_size"
android:paddingBottom="@dimen/activity_vertical_margin"/>
<ListView
android:id="@android:id/list"
android:tag="results_list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
结果片段
public class ResultsFragment extends ListFragment
{
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState )
{
super.onCreateView(inflater, container, savedInstanceState);
View resultsLayout = inflater.inflate(R.layout.results_fragment,container, false);
return resultsLayout;
}
}
问题:
1) 为什么我没有得到 List 视图的引用并且在 resultsList 上没有得到 null?
2) 在哪里调用 getView(),如在 resultsListAlt 中,以获取引用并能够影响 listView 的大小?我在onResume() 中尝试过,但listView 的大小保持不变。
3) 有没有更好的方法来创建相同的布局,您可以从数据输入滚动到结果?
我使用片段来为横向和/或更大的设备呈现不同的布局。
为了说明应用程序,我附上了我的 webView 包装器应用程序的屏幕截图。在顶部,您有一个数据条目 (DataEntryFragment),在下半部分是结果列表 (ResultFragment)。
【问题讨论】:
-
谢谢。我没有足够的“积分”来发布图片。
标签: android listview android-fragments android-listview android-listfragment