2012-04-22 95 views
1

通常使用ListView將圖像和文本添加到ListView項目很簡單。您可以使用您的自定義適配器創建和適配器並設置列表適配器(適配器)。如何將ImageView添加到ListFragment?

我想創建一個Fragment應用程序,它包含與ImageView相同的列表。我搜索了一個例子,但一直沒能找到任何有圖像的地方。有人可以請我指出一個例子,以便我可以看到它是如何完成的?

它是否在layout-land XML中完成?例如,你如何聲明使用下面的XML。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" android:layout_height="match_parent"> 

    <fragment class="com.demo.android.FragmentLayout$ArticleFragment" 
      android:id="@+id/article" android:layout_weight="1" 
      android:layout_width="0px" android:layout_height="match_parent" /> 

    <FrameLayout android:id="@+id/details" android:layout_weight="1" 
      android:layout_width="0px" android:layout_height="match_parent" 
      android:background="?android:attr/detailsElementBackground" /> 

</LinearLayout> 

回答

3

你只需要創建一個類的擴展FragmentListFragment,然後膨脹包含您ListView佈局和設置您的Adapter

ListFragment

public class Example extends ListFragment { 

// Adapter 
private YourListAdapter mAdapter; 

// ListView 
private ListView mListView; 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    // Adapter 
    mListView.setAdapter(mAdapter); 
    super.onActivityCreated(savedInstanceState); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.listview, container, false); 
    mListView = (ListView)view.findViewById(android.R.id.list); 
    return view; 
    } 
} 
+0

感謝@aneal,我覺得我在做的事情太複雜了我自己。我採用了與使用列表視圖相同的方法,併爲我工作。 – Byron 2012-04-27 16:32:41