安卓RecyclerView固定页脚

3
过去几天,我一直在为我的RecyclerView制作固定的页脚,但到目前为止,我没有得到我期望的结果。我已经查看了现有的问题,但得到的结果与之前相同。似乎我还是缺少一点,但我想不出来……因此,我正在等待您的建议。谢谢!这是当前布局的视图,我希望页脚部分粘在RecyclerView底部。 DrawerAdapter.java
public class DrawerAdapter extends RecyclerView.Adapter<DrawerAdapter.DrawerViewHolder> {
// Class for view types.
private class VIEW_TYPES {
    public static final int Header = 0;
    public static final int Normal = 1;
    public static final int Footer = 2;
}

private ArrayList<DrawerItem> drawerMenuList;
private String userProfileName;
private String userProfileMail;
private Drawable userProfilePic;
private OnItemSelecteListener mListener;

public DrawerAdapter(ArrayList<DrawerItem> drawerMenuList, String name, String mail, Drawable profileImage) {
    this.drawerMenuList = drawerMenuList;
    this.userProfileMail = mail;
    this.userProfileName = name;
    this.userProfilePic = profileImage;
}

@Override
public DrawerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view;
    if (viewType == VIEW_TYPES.Header) {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.header, parent, false);
    } else if (viewType == VIEW_TYPES.Footer) {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer, parent, false);
    } else {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row, parent, false);
    }
    return new DrawerViewHolder(view, viewType);
}

@Override
public void onBindViewHolder(DrawerViewHolder holder, int position) {
    // If item's position 0 it's a header.
    if (position == 0) {
        holder.headerName.setText(userProfileName);
        holder.headerMail.setText(userProfileMail);
        holder.headerProfile.setImageDrawable(userProfilePic);
    } else if (position == 5) { // If position is 5 then it's footer.
        holder.headerName.setText("FOOTER");
    } else { // it's a menu item.
        holder.title.setText(drawerMenuList.get(position - 1).getTitle());
        holder.icon.setImageResource(drawerMenuList.get(position - 1).getIcon());
    }

}

@Override
public int getItemCount() {
    return drawerMenuList.size() + 1;
}

@Override
public int getItemViewType(int position) {
    if (position == 0) {
        return VIEW_TYPES.Header;
    } else if (position == 5) {
        return VIEW_TYPES.Footer;
    } else {
        return VIEW_TYPES.Normal;
    }
}


class DrawerViewHolder extends RecyclerView.ViewHolder {
    TextView title;
    TextView headerName;
    ImageView icon;
    TextView headerMail;
    ImageView headerProfile;

    public DrawerViewHolder(View itemView, int viewType) {
        super(itemView);

        if (viewType == VIEW_TYPES.Header) {
            headerName = (TextView) itemView.findViewById(R.id.header_name);
            headerMail = (TextView) itemView.findViewById(R.id.header_email);
            headerProfile = (ImageView) itemView.findViewById(R.id.circleView);
        } else if (viewType == VIEW_TYPES.Footer) {
            headerName = (TextView) itemView.findViewById(R.id.textView3);
        } else {
            title = (TextView) itemView.findViewById(R.id.title);
            icon = (ImageView) itemView.findViewById(R.id.icon);
        }
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mListener.onItemSelected(view, getAdapterPosition());
            }
        });

    }
}
// To set click listener.
public interface OnItemSelecteListener {
    void onItemSelected(View v, int position);
}

public void setOnItemClickLister(OnItemSelecteListener mListener) {
    this.mListener = mListener;
}

}

MainActivity.java

                            for (int i = 0; i < navMenuTitles.length; i++) {
                                Log.e("I", String.valueOf(i));
                                mDrawerItemList.add(new DrawerItem(navMenuTitles[i], navMenuIcons.getResourceId(i, -1)));
                            }
                            mDrawerItemList.add(new DrawerItem());

                            mRecyclerView = (RecyclerView) findViewById(R.id.drawerRecyclerView);
                            mDrawerAdapter = new DrawerAdapter(mDrawerItemList, logged_user.name, logged_user.email, draw_userImage);
                            mLinearLayoutManager = new CustomGridLayoutManager(HomeScreenActivity.this){
                                @Override
                                public boolean canScrollVertically() {
                                    return false;
                                }
                            };

                            mRecyclerView.setLayoutManager(mLinearLayoutManager);
                            mRecyclerView.setAdapter(mDrawerAdapter);
                            mDrawerLayout = (DrawerLayout) findViewById(R.id.DrawerLayout);
                            mDrawerToggle = new ActionBarDrawerToggle(HomeScreenActivity.this, mDrawerLayout, mToolbar, R.string.openDrawer, R.string.closeDrawer) {
                                @Override
                                public void onDrawerOpened(View drawerView) {
                                    super.onDrawerOpened(drawerView);
                                    // code here will execute once the drawer is opened( As I dont want anything happened whe drawer is
                                    // open I am not going to put anything here)
                                }

                                @Override
                                public void onDrawerClosed(View drawerView) {
                                    super.onDrawerClosed(drawerView);
                                    // Code here will execute once drawer is closed
                                }
                            }; // Drawer Toggle Object Made
                            mDrawerLayout.addDrawerListener(mDrawerToggle); // Drawer Listener set to the Drawer toggle
                            mDrawerToggle.syncState();               // Finally we set the drawer toggle sync State
                            mDrawerAdapter.setOnItemClickLister(new DrawerAdapter.OnItemSelecteListener() {
                                @Override
                                public void onItemSelected(View v, int position) {
                                    Log.e("CLICK", "You clicked at position: " + position);
                                }
                            });

drawer_layout.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/DrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:elevation="7dp">

<FrameLayout
    android:id="@+id/containerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/drawerRecyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_gravity="left"
    android:background="#ffffff"
    app:reverseLayout="false"
    app:stackFromEnd="false">

</android.support.v7.widget.RecyclerView>

footer.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:layout_alignParentBottom="true"
android:background="#ffffff"
android:layout_alignBottom="@id/drawerRecyclerView"
android:foregroundGravity="bottom">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="false"
        android:layout_alignParentEnd="true"
        android:background="#f16666"
        android:foregroundGravity="bottom"
        android:gravity="center_horizontal|bottom"
        android:paddingTop="@dimen/_15sdp"
        android:paddingBottom="@dimen/_15sdp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="TEST"
                android:id="@+id/textView3"
                android:layout_gravity="center_horizontal"
                android:layout_alignParentBottom="true"
                android:gravity="center_horizontal|bottom" />
    </LinearLayout>
    </RelativeLayout>
2个回答

1
在你的drawer_layout.xml中,将RecyclerView放置在RelativeLayout内,并将alignParentTop设置为true。然后从RecyclerView中删除页脚视图,并将其放置在RelativeLayout下面,如下所示:
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/DrawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:elevation="7dp">

    <FrameLayout
        android:id="@+id/containerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
           android:id="@+id/drawerRecyclerView"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_alignParentTop="true"
           android:layout_gravity="left"
           android:background="#ffffff"
           app:reverseLayout="false"
           app:stackFromEnd="false" />

        <include layout="@layout/footer" />

    </RelativeLayout>

</android.support.v4.widget.DrawerLayout>

您还可以查看NavigationView,以便不必自定义抽屉列表。这里是有关如何使用它的教程。它很容易使用并且需要较少的代码。然后,您可以查看这个答案,了解如何在NavigationView中放置页脚。

谢谢!首先我尝试使用RecyclerView包含页脚布局,但是结果非常奇怪。抽屉布局立即出现在屏幕上等等。我认为问题是由于我用于选项卡的工具栏引起的。但是后来我使用了NavigationView,它像魔法一样运行良好!我还使用了那个教程使其与我的选项卡一起工作。https://androidbelieve.com/navigation-drawer-with-swipe-tabs-using-design-support-library/ - İlker Çakır

0

我正在使用带有权重的LinearLayout(创建了多个具有不同值的dimens文件)。对我来说,它的工作效果符合预期:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical"
<include layout="@layout/header" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycleView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.5"
    tools:layout_height="0dp"
    tools:listitem="@layout/row" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="@dimen/footer_weight"
    android:padding="@dimen/extra_padding"
    android:paddingEnd="@dimen/small_padding"
    android:paddingLeft="@dimen/small_padding"
    android:paddingRight="@dimen/small_padding"
    android:paddingStart="@dimen/small_padding"
    android:text="@string/contact"
    android:textColor="@color/grey" />

 </LinearLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接