android listview footerview中心位置不起作用

时间:2015-12-15 12:53:08

标签: android android-layout android-listview android-scrollview

我有customlistview.i在我的listview中成功添加了footerView。这是一个来源

void loadDataLastTransactions(int limit) {
    footerView = getActivity().getLayoutInflater().
            inflate(R.layout.listview_footer_layout, null);
    if (cashTransactionlist.size() <= limit) {
        transactionAdapter = new TransactionAdapter(getActivity(), cashTransactionlist);
    } else {
        List<Transaction> sixTramsactions = cashTransactionlist.subList(0, limit);
        transactionAdapter = new TransactionAdapter(getActivity(), sixTramsactions);

    }
    transactionsList.addFooterView(footerView, null, false);
    transactionsList.setAdapter(transactionAdapter);

}

这是我的footerView的xml代码

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingTop="7dip"
    android:paddingBottom="7dip"
    android:orientation="vertical"
    android:layout_gravity="center_horizontal"
    android:gravity="center_vertical|left"
    >

    <LinearLayout
        android:id="@+id/footer_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >

        <TextView
            android:id="@+id/u_transaction_view_all"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textColor="@color/u_blue"
            android:textSize="@dimen/u_common_text_size"
            android:text="@string/u_transaction_view_all"
            android:padding="8dp"
            />
    </LinearLayout>
</LinearLayout> 

my footerView's xml looks like a pic

这是我的mainactivity.xml代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f2f2f2"
    android:id="@+id/dashboard_main_layout"
    android:clickable="true"
    android:orientation="vertical"
    android:layout_gravity="center_horizontal"
    android:gravity="center_vertical|left"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:orientation="vertical">

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


                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="#d9d9d9"/>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                <GridView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:numColumns="3"
                android:stretchMode="columnWidth"
                android:id="@+id/dashboard_header_gridview" />
                    <View
                        android:layout_width="match_parent"
                        android:background="#ffffff"
                        android:layout_height="match_parent"
                        android:layout_marginLeft="-1dp"/>
                    </LinearLayout>
        </LinearLayout>
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#d9d9d9"/>
        <ScrollView
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:fillViewport="true">
           <LinearLayout
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:orientation="vertical"
               android:layout_gravity="center_horizontal"
               android:gravity="center_vertical|left">
               <View
                   android:layout_width="match_parent"
                   android:layout_height="1dp"
                   android:background="#d9d9d9"/>
               <LinearLayout
                   android:id="@+id/fragments_transaction_list_layout"
                   android:layout_width="match_parent"
                   android:layout_height="match_parent"
                   android:layout_below="@+id/fragments_cardview"
                   android:orientation="vertical"
                   android:layout_gravity="center_horizontal"
                   android:gravity="center_vertical|left"
                   >
                   <android.custom.CustomListview
                       android:id="@+id/u_transaction_list"
                       android:layout_width="match_parent"
                       android:layout_height="wrap_content"
                       android:animationCache="false"
                       android:cacheColorHint="#00000000"
                       android:clipToPadding="false"
                       android:divider="@null"
                       android:dividerHeight="0dp"
                       android:listSelector="#00000000"
                       android:scrollbars="none"
                       android:scrollingCache="false"
                       android:smoothScrollbar="true" >
                   </android.custom.CustomListview>
           </LinearLayout>
       </LinearLayout>
   </ScrollView>
</LinearLayout>




public class CustomListview extends ListView implements View.OnTouchListener, OnScrollListener {

private int listViewTouchAction;
private static final int MAXIMUM_LIST_ITEMS_VIEWABLE = 99;

public CustomListview(Context context, AttributeSet attrs) {
    super(context, attrs);
    listViewTouchAction = -1;
    setOnScrollListener(this);
    setOnTouchListener(this);
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
        int visibleItemCount, int totalItemCount) {
    if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
        if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
            scrollBy(0, -1);
        }
    }
}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int newHeight = 0;
    final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);
    if (heightMode != MeasureSpec.EXACTLY) {
        ListAdapter listAdapter = getAdapter();
        if (listAdapter != null && !listAdapter.isEmpty()) {
            int listPosition = 0;
            for (listPosition = 0; listPosition < listAdapter.getCount()
                    && listPosition < MAXIMUM_LIST_ITEMS_VIEWABLE; listPosition++) {
                View listItem = listAdapter.getView(listPosition, null, this);

                if (listItem instanceof ViewGroup) {
                    listItem.setLayoutParams(new LayoutParams(
                            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                }
                listItem.measure(widthMeasureSpec, heightMeasureSpec);
                newHeight += listItem.getMeasuredHeight();
            }
            newHeight += getDividerHeight() * listPosition;
        }
        if ((heightMode == MeasureSpec.AT_MOST) && (newHeight > heightSize)) {
            if (newHeight > heightSize) {
                newHeight = heightSize;
            }
        }
    } else {
        newHeight = getMeasuredHeight();
    }
    setMeasuredDimension(getMeasuredWidth(), newHeight);
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
        if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
            scrollBy(0, 1);
        }
    }
    return false;
}
}

当我运行我的应用程序时,我的页脚视图仍然是possition.in我的选项我在activity_main.xml文件中有问题,因为footerView的xml是正确的。如果有人知道解决方案,请帮助我 谢谢大家

1 个答案:

答案 0 :(得分:0)

属性layout_gravity会影响与其父级对齐的元素,而gravity属性会对齐该视图的子视图。例如:

如果你申请

android:gravity="center"
android:layout_gravity="left"

TextView表示TextView容器将对齐 left ,内部文本将居中

抓住22:

将页脚布局添加到ListView时,它可能有RelativeLayout个父级,RelativeLayout不支持layout_gravity属性。在这种情况下,修复将是添加:

layout_centerHorizontal="true"
layout_alignParentBottom="true"

到页脚布局的根目录。