如何在RecyclerView结束时添加页脚?

时间:2017-07-27 06:41:11

标签: android

我有一个应用程序,其中包含一些广告,如谷歌和Facebook的recyclerview。在recyclerview结束时,我想添加一个页脚视图。我该怎么做。

代码: -

 @Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    ViewHolder viewHolder = null;
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    switch (viewType) {
        case 1:
            View contentView = inflater.inflate(R.layout.ad_list_card, parent, false);
            viewHolder = new DealViewHolder(contentView);
            this.sessionManager = new CLoginSessionManager(this.m_Context);
            HashMap<String, String> user = this.sessionManager.getLoginData();
            this.m_szMobileNumber = user.get(CLoginSessionManager.LOGIN_MOBILE);// getting mobile from saved preferences..........
            this.m_szEncryptedPassword = user.get(CLoginSessionManager.LOGIN_PASSWORD);// getting password from shared preferences...
            mHelper = new PreferenceHelper(m_Context);
            break;
        case 2:
            View facebookAdView = inflater.inflate(R.layout.soopermo_fb_card, parent, false);
            viewHolder = new FacebookAdHolder(facebookAdView);
            break;
        case 3:
            View adView = inflater.inflate(R.layout.google_ad_card, parent, false);
            viewHolder = new GoogleAdViewHolder(adView);
            break;
    }
    return viewHolder;
}

 @Override
public int getItemViewType(int position) {
    return this.s_oDataset.get(position).getViewType();
}

2 个答案:

答案 0 :(得分:0)

在您注入 Recycleview 的活动中,juts添加另一个 LinearLayout 并在其中包含必要的视图, Recycleview 是juts内部系统,在那里你可以控制其外部的显示项目

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

    </LinearLayout>
    <!--Middle-->
    <LinearLayout
        android:background="@color/red_400"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="5">

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>

    </LinearLayout>
    <!--Footer-->
    <LinearLayout
        android:background="@color/green_500"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

    </LinearLayout>
</LinearLayout>

答案 1 :(得分:0)

您必须在ViewType中添加另一个adapter

@Override
public int getItemViewType(int position) {
    if (position == data.size()) {
        // footer_view to check in your switch/case
        return FOOTER_VIEW;
    }

    return super.getItemViewType(position);
}

这是你的代码:

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    ViewHolder viewHolder = null;
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    switch (viewType) {
        case 1:
            // your view
            break;
        case 2:
           // your fb viewholder
            break;
        case 3: // your google view holder
            break;
        case FOOTER_VIEW:
            View contentView = inflater.inflate(R.layout.yourFooterView, parent, false);
            viewHolder = new FooterViewHolder(contentView);
            break;
    }
    return viewHolder;
}

然后,您可以将数据设置为onBind中的FooterViewHolder

    @Override
    public void onBindViewHolder(SplitMemberViewHolder viewHolder, int position) {
           switch(getItemViewType(position)){
           case 1:
           break;
           case 2:
           case 2:

            break;
           case FOOTER_VIEW:   
             //bind footer data.
           break;
       }
    }