0

먼저 ListViewScrollView 안에 넣으면 눈살을 찌푸리게됩니다. 그러나 Activity 클래스를 사용하여 레이아웃이 완벽하게 작동했습니다. 나는 이제 Fragments를 사용하여 동일한 작업을 시도했지만 거기에 도달 할 수 없습니다.findViewByID (android.R.id.list)는 ListFragment로 null을 반환합니다.

조각 간의 상호 작용이 잘 작동하고 listView가 데이터 입력 화면에서 채워집니다. 그러나 ListView는 몇 줄로 축소되었습니다. 보완하기 위해 arrayList 및 divider의 높이를 갖도록 ListView의 크기를 다시 조정하려고합니다. 이를 달성하기 위해 필자는 실패한 목록보기에 대한 참조를 가져와야합니다.

ListView에 대한 참조를 얻으려고하면 null이 반환되는 (ListView)findViewById(android.R.id.list) 또는 MySkeletonFragmentActivity.this.resultsFragment.getListView()으로 인해 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을

<?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) 왜 목록보기에 대한 참조를 가져 오지 않고 resultsList에 null이 표시되지 않습니까? 2) getList()를 호출하는 위치는 resultsListAlt에서와 같이 참조를 가져오고 listView의 크기에 영향을 줄 수 있습니까? 나는 onResume()에서 시도했지만 listView의 크기는 변경되지 않았습니다. 3) 같은 레이아웃을 만드는 더 좋은 방법이 있습니까? 데이터 항목에서 결과로 스크롤 할 수 있습니까?

프리젠 테이션을 사용하여 가로 및/또는 대형 장치에 다른 레이아웃을 제공 할 수있었습니다.

앱 설명을 위해 webView 래퍼 앱의 스크린 샷을 포함합니다. 맨 위 섹션에는 데이터 항목 (DataEntryFragment)이 있고 아래쪽에는 결과 목록 (ResultFragment)이 있습니다.

enter image description here

+0

감사합니다. 나는 이미지를 게시하기에 충분한 '포인트'가 없었다. – petrfaitl

답변

3

commit() 비동기입니다. ListViewcommit()에 전화 한 후 오랫동안 생성되지 않습니다. onCreateView() 메서드에서 조각 자체에 조각 논리를 이동하면 ListView이 생성되었음을 알 수있을뿐만 아니라 findViewById()resultsLayout에 호출하여 검색 할 수 있습니다.

+0

그 제안에 감사드립니다. 해결 방법을 수정하고 onViewCreated (View 뷰, Bundle savedInstanceState) 메소드에서 뷰에 대한 참조를 가져올 수있었습니다. 이제 경고의 한 단어, ArrayAdapter를 설정하기위한 컨텍스트를 얻으려고 할 때, 먼저 GetApplicationContext()를 사용하여 listView의 흰색 텍스트를 흰색으로 표시했습니다. container.GetContext()가 더 나은 장소였습니다. – petrfaitl

관련 문제