listfragment中的页脚(listview)

时间:2014-11-19 18:54:48

标签: java android listview

我正在尝试将静态页脚添加到android的列表视图中。我使用列表片段,填充父级的列表视图,并从主活动中加载它。我想在列表视图中添加页脚,因此我创建了一个页脚xml文件,但我无法从列表片段中加载它。它要么崩溃要么没有任何反应。

在listfragment中的listview中加载静态页脚的正确代码是什么?在listfragment扩展类中粘贴时,我引用的任何代码片段都不起作用。

我遇到这个错误,当它崩溃时,viewpager布局参数无法转换为abslistviewlayoutparams'或类似的东西,viewpager是主要的父级,每个标签都是一个片段,其中一个片段是一个列表视图,我试图添加一个页脚。

<!--This is the fragment with the list in it-->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

    <ProgressBar
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:visibility="gone" 
        android:layout_centerInParent="true"
        />

     <include layout="@android:layout/list_content" />
     <ListView
          android:id="@+id/list"
          android:layout_height="wrap_content"
          android:layout_width="match_parent" />

</RelativeLayout>

感谢。下面的代码我只是尝试,编译和运行,但什么也没做。而且它在里面 onCreateView方法,它是一个扩展ListFragment的类。

     lview = (ListView)view.findViewById(R.id.list);            

     //nothing happens using this    
     lview.addFooterView(
             inflater.inflate(R.layout.standalone_footer, null)
      );

      //crashes with cast exception using this
      lview.addFooterView(
             inflater.inflate(R.layout.standalone_footer, container, false)      
     );

//onCreate method, either crashes or runs but nothing happens when tweaking code
    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)        {

     View view = inflater.inflate(R.layout.fragment_tab1, container, false);

     lv = (ListView)view.findViewById(R.id.list);

     mItems = new ArrayList<ListViewCustom>();

     //add static fixed footer
     lv.addFooterView(
             inflater.inflate(R.layout.footer, null, false)     //also tried passing lv instead of null 
     );

     //initialize and set the list adapter
     setListAdapter(new ListViewAdapterCustom(getActivity(), mItems));

     return view;
}

独立页脚xml(带有xmlns标签的TableLayout不会显示为此处的代码......)             机器人:stretchColumns =&#34; *&#34;       机器人:背景=&#34;#000&#34;&GT;

<TableRow
     android:layout_height="wrap_content"
     android:layout_width="fill_parent"
     android:gravity="center_horizontal">

     <TextView
         android:layout_width="match_parent" 
         android:layout_height="wrap_content"
         android:textAppearance="?android:attr/textAppearanceMedium"  
         android:layout_span="1" 
         android:layout_weight="1" 
         android:gravity="center" />

     <TextView
         android:layout_width="match_parent" 
         android:layout_height="wrap_content"
         android:textAppearance="?android:attr/textAppearanceMedium" 
         android:layout_span="1" 
         android:layout_weight="1" 
         android:gravity="center" />

     <TextView
         android:layout_width="match_parent" 
         android:layout_height="wrap_content"
         android:textAppearance="?android:attr/textAppearanceMedium" 
         android:layout_span="1" 
         android:layout_weight="1" 
         android:gravity="center" />                         
</TableRow>

<TableRow
     android:layout_height="wrap_content"
     android:layout_width="fill_parent"
     android:gravity="center_horizontal">

     <TextView
         android:layout_width="match_parent" 
         android:layout_height="wrap_content"
         android:textAppearance="?android:attr/textAppearanceSmall"  
         android:text="0"  
         android:layout_span="1" 
         android:layout_weight="1" 
         android:gravity="center" />

     <TextView
         android:layout_width="match_parent" 
         android:layout_height="wrap_content"
         android:textAppearance="?android:attr/textAppearanceSmall" 
         android:text="0"  
         android:layout_span="1" 
         android:layout_weight="1" 
         android:gravity="center" />

     <TextView
         android:layout_width="match_parent" 
         android:layout_height="wrap_content"
         android:textAppearance="?android:attr/textAppearanceSmall"  
         android:text="0"  
         android:layout_span="1" 
         android:layout_weight="1" 
         android:gravity="center" />
</TableRow>

</TableLayout>

PS Listfragment和实际列表加载并呈现正常,一切正常。如果重要的话我也在使用自定义适配器吗?

1 个答案:

答案 0 :(得分:1)

view = (ListView)view.findViewById(R.id.list);
我不理解这部分内容 - 您的基本视图称为view(当您致电view.findViewById时看到,但您也称ListViewview也是。

onCreateView()方法中尝试此操作:

ListView lv = (ListView) view.findViewById(R.id.list);
lv.addFooterView(inflater.inflate(R.layout.standalone_footer, null));

或尝试getListView().addFooterView(inflater.inflate(R.layout.standalone_footer, null))