【问题标题】:Can't set a Custom Listview on ListFragment无法在 ListFragment 上设置自定义列表视图
【发布时间】:2014-01-27 06:36:30
【问题描述】:

我正在尝试在 ListFragment 上设置自定义 Listview 布局,我一直在到处搜索,但仍然无法正常工作。我是 android 编程新手,非常感谢您的帮助。

这是我的 listFragment 类

public class ListCins  extends ListFragment {
public static ArrayList<Cin> cins;
private ListView listView;
private pListAdapter mAdapter;

public static ListCins newInstance() {
ListCins f = new ListCins();
return f;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) { 
super.onActivityCreated(savedInstanceState);
//Los fragments soportan presentar un msj cuando la lista esta vacia.
setEmptyText("Nothing yet, but working");    

}


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);




cins = new ArrayList<Cin>();
cins.add(new Cin("Agenter", 1));
cins.add(new Cin("Ora", 2));
cins.add(new Cin("Vistia", 2));
cins.add(new Cin("Bluel", 2));


mAdapter = new pListAdapter(this.getActivity(), cins);
setListAdapter(mAdapter);


}



@Override
public void onViewCreated(View view, Bundle savedInstanceState) {


super.onViewCreated(view, savedInstanceState);      
setEmptyText("Nothing yet 2, but working");
registerForContextMenu(getListView());

setHasOptionsMenu(true);
}






@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);

Toast mTosat = Toast.makeText(this.getActivity(),"clicked:"+String.valueOf(position)+" "+cins.get(position).nombre , 200);
mTosat.show();


}



}

这是我要应用于 listfragment 的 list_fragment.xml。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"


android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp">


<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:drawSelectorOnTop="false" />


<TextView
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="No data"        />
</LinearLayout>  

我也有一个自定义的arrayAdapter,但我不知道它是否与自定义列表视图布局有关。

提前致谢...

【问题讨论】:

  • 对不起,我不明白你的问题是什么。你自己试过吗?您面临什么具体问题?

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


【解决方案1】:

您需要覆盖 onCreateView 并膨胀/返回您的自定义布局。 ListFragment 应该为您处理其余的事情。这在http://developer.android.com/reference/android/app/ListFragment.html的屏幕布局部分有描述

【讨论】:

    【解决方案2】:

    感谢 NasaGeek,我正试图像他们说的那样给它充气,就像这样:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
    View view = inflater.inflate(R.layout.list_fragment, container, false);
    return view;
    }
    

    但它不起作用,然后我发现了问题。由于某种原因,这种方法使代码变得疯狂。

    setEmptyText("Nothing yet, but working");   
    

    如果你能解释我为什么,那就太棒了。

    谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-10
      • 1970-01-01
      相关资源
      最近更新 更多