2012-04-22 98 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