findViewByID(android.R.id.list)使用ListFragment返回null

时间:2014-04-18 00:22:08

标签: android listview android-fragments android-listview android-listfragment

首先我要说明的是,ListViewScrollView括在(ListView)findViewById(android.R.id.list)内是不礼貌的。但是,通过使用Activity类,我可以很好地完成布局。我现在试图通过使用Fragments来做同样的事情,但却无法实现目标。

片段之间的交互工作正常,listView从数据输入屏幕填充。但是,ListView显示为折叠为几行。为了弥补我试图重新调整ListView的大小以使arrayList加上分隔符的高度。为了实现这一目标,我需要获得对列表视图的引用,我没有这样做。

问题似乎在于尝试获取对ListView的引用,要么MySkeletonFragmentActivity.this.resultsFragment.getListView()返回null,要么view not yet created返回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 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>

结果片段XML

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;
}

}

结果片段

onResume()

问题: 1)为什么我没有引用List视图并在resultsList上获取null? 2)在resultListAlt中调用getView(),获取引用并能够影响listView的大小?我尝试了{{1}},但listView的大小保持不变。 3)是否有更好的方法来创建相同的布局,您可以从数据条目滚动到结果?

我使用片段为景观和/或更大的设备提供不同的布局。

为了说明该应用,我包含了我的webView包装应用的屏幕截图。在顶部,您有一个数据条目(DataEntryFragment),在结果列表的下半部分(ResultFragment)。

enter image description here

1 个答案:

答案 0 :(得分:3)

commit()是异步的。在您致电ListView之后很久,您的commit()才会被创建。使用onCreateView()方法将您的populate-the-fragment逻辑移动到片段本身,因为您不仅知道ListView已创建,而且您可以通过调用findViewById()来检索它resultsLayout

相关问题