我正在设计一个具有自定义布局的自定义列表视图。因为在完成从Web服务加载数据的列表视图后,我需要在列表视图的末尾和@+id/progressBar1上方添加一个Imageview。

在这种情况下,不能使用listview.addFooterView(View v),因为我使用了来自第三方库Pull to refresh listview的自定义PullToRefreshListview

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/lay_top"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:orientation="vertical" >

        <com.rb.library.ScrollingTextView
            android:id="@+id/txtTicker"
            style="@style/style_bottom_marque"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:visibility="gone"
            app:typeface="roboto_condensed" />

        <include
            android:id="@+id/pan_breaking_new"
            layout="@layout/breaking_news" />
    </LinearLayout>

    <View
        android:id="@+id/view1"
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_below="@+id/lay_top"
        android:background="#ccc"
        android:paddingTop="2dip" />

    <com.devspark.robototextview.widget.RobotoTextView
        android:id="@+id/txt_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/view1"
        android:layout_gravity="fill_horizontal"
        android:drawableRight="@drawable/ic_title_arrow"
        android:gravity="left|center_vertical"
        android:inputType="textCapWords"
        android:padding="5dip"
        android:text="@string/TITLE_TOP_NEWS"
        app:typeface="roboto_black" android:visibility="gone"/>

    <View
        android:id="@+id/view2"
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_below="@+id/txt_title"
        android:background="#ccc" />

    <com.handmark.pulltorefresh.library.PullToRefreshListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:layout_below="@+id/view2"
        android:layout_margin="2dip"
        android:layout_marginBottom="0dp"
        android:layout_marginTop="0dp"
        android:divider="#cccccc"
        android:dividerHeight="1dp" />







    <com.devspark.robototextview.widget.RobotoTextView
        android:id="@+id/main_error"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_gravity="fill_horizontal"
        android:gravity="center_horizontal"
        android:padding="20dip"
        android:text="@string/INTERNET_ERR"
        android:visibility="gone"
        app:typeface="roboto_black" />

    <ImageView
        android:id="@+id/img_error"
        android:layout_below="@+id/main_error"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:gravity="center_horizontal"
        android:contentDescription="Error"
        android:layout_centerInParent="true"
        android:visibility="gone"
        android:src="@drawable/ic_error" />

    <LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/progressBar1"
        android:layout_below="@+id/listView">

        <ImageView
            android:id="@+id/list_footer_image"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:scaleType="centerCrop"
            android:src="@drawable/app_logo"

            android:visibility="visible"/>

    </LinearLayout>

    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="25dip"
        android:layout_height="25dip"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />



</RelativeLayout>


HomFragment.java

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.listview_layout, container,false);
        View footer_image=inflater.inflate(R.layout.listview_image_footer, container,false);



            /* get theme webservice*/

        StrictMode.ThreadPolicy policy1 = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy1);


        userFunction=new UserFunctions();

            //Breaking News Section
            slide = AnimationUtils.loadAnimation(getActivity(), R.anim.slidedown);
            fadeOut = AnimationUtils.loadAnimation(getActivity(), R.anim.fadeout);
            pan_breaking_new = (LinearLayout)rootView.findViewById(R.id.pan_breaking_new);
            //pan_breaking_new.setOnClickListener(this);
            pan_breaking_new.setVisibility(8);

            btn_close= (ImageView) rootView.findViewById(R.id.btn_close);
            btn_close.setOnClickListener(this);
            img_share= (ImageView) rootView.findViewById(R.id.img_share);
            img_share.setOnClickListener(this);
            //END Breaking News Section

            //Ticker
            marque = (ScrollingTextView)rootView.findViewById(R.id.txtTicker);
            marque.setVisibility(8);
            //END ticker


            main_error = (TextView) rootView.findViewById(R.id.main_error);
            main_error.setText("Loading...");
            img_error = (ImageView) rootView.findViewById(R.id.img_error);
            img_error.setOnClickListener(this);


            progressBar1 = (ProgressBar) rootView.findViewById(R.id.progressBar1);
            progressBar1.setVisibility(8);



            //karthik
            refreshable_listView = (PullToRefreshListView) rootView.findViewById(R.id.listView);
            listView=refreshable_listView.getRefreshableView();
            listView.addFooterView(footer_image);
}

最佳答案

尝试更改此代码:

 View footer_image=inflater.inflate(R.layout.listview_image_footer, container,false);


有了这个 :

 View footer_image=inflater.inflate(R.layout.listview_image_footer,null);


注意:这里的容器是引用片段xml,尝试将自定义页脚添加到包含在容器中的ListView中,因此将页脚的根值设置为null而不是容器

07-26 08:43