FrameLayout 无法在 Fragment 的 RecyclerView 中显示

FrameLayout can't show in RecyclerView of Fragment

我使用 ListViewaddFooterViewFragment 中插入一个 FrameLayout,成功了!

但现在我想使用 RecyclerView 并在 FragmentRecyclerView 的页脚添加 FrameLayout

但是列表数据显示时,FrameLayout没有显示!

如何解决,谢谢!

这是我的 ListView 代码(可以显示FrameLayout):

public class NewFragment extends Fragment {

    private ListView mListView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.new_layout,container,false);
        mListView = (ListView) view.findViewById(R.id.listView);

        // AD FrameLayout.
        FrameLayout footerLayout = (FrameLayout) getLayoutInflater().inflate(R.layout.footerview, null);

        // ListView.
        ArrayList<Card> list = new ArrayList<>();
        list.add(new Card("drawable://" + R.drawable.icon1, "Batman","1 min","drawable://" + R.drawable.img_pic1, "list1 post1.", "10000 likes", "10000 message"));
        list.add(new Card("drawable://" + R.drawable.icon2, "Superman","10 min","drawable://" + R.drawable.img_pic2, "list1 post2.", "1234 likes", "123 message"));
        list.add(new Card("drawable://" + R.drawable.icon3, "Ironman","30 min","drawable://" + R.drawable.img_pic3, "list1 post3.", "10 likes", "10 message"));

        CustomListAdapter adapter = new CustomListAdapter(getActivity(), R.layout.card_layout_main, list);
        mListView.addFooterView(footerLayout); // add FrameLayout in the footer of listview.
        mListView.setAdapter(adapter);

        return view;
    }

这是我的 RecyclerView 代码:

public class HotFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {

private RecyclerView postcardRecycler;
private ArrayList<PostCard> post_data = new ArrayList<>();
private ArrayList<PostCard> refresh_data = new ArrayList<>();
private PostCardImageAdapter adapter;
private Handler handler = new Handler();
private SwipeRefreshLayout swipeRefreshLayout;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.hot_layout, container, false);

    postcardRecycler = view.findViewById(R.id.postcard_recycler);
    swipeRefreshLayout = view.findViewById(R.id.postcard_swipe_refresh);
    swipeRefreshLayout.setOnRefreshListener(this);

    LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
    postcardRecycler.setLayoutManager(layoutManager);

    adapter = new PostCardImageAdapter(getActivity(), post_data);
    postcardRecycler.setAdapter(adapter);

    init();

    return postcardRecycler;
}

private void init() {
    initPostData();
    initRefreshData();
}

private void initPostData() {
    post_data.add(new PostCard("http://clipart-library.com/images/8T65akk8c.png", "Batman", "1min", "Original RecyclerView.", "https://pmcvariety.files.wordpress.com/2013/07/dark_knight_rises.jpg", "100 likes", "100 messages"));
    post_data.add(new PostCard("https://cdn.iconscout.com/icon/free/png-256/superman-6-282336.png", "Superman", "10min", "Original RecyclerView.", "https://ichef.bbci.co.uk/news/660/cpsprodpb/FA79/production/_102512146_d8e385de-4e0c-45c0-b265-e105d0b47294.jpg", "66likes", "66messages"));
}

private void initRefreshData() {
    refresh_data.add(new PostCard("http://clipart-library.com/images/8T65akk8c.png", "Batman", "1min", "Refresh RecyclerView.", "https://pmcvariety.files.wordpress.com/2013/07/dark_knight_rises.jpg", "100 likes", "100 messages"));
    refresh_data.add(new PostCard("https://cdn.iconscout.com/icon/free/png-256/superman-6-282336.png", "Superman", "10min", "Refresh RecyclerView.", "https://ichef.bbci.co.uk/news/660/cpsprodpb/FA79/production/_102512146_d8e385de-4e0c-45c0-b265-e105d0b47294.jpg", "66likes", "66messages"));
}

@Override
public void onRefresh() {

    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            Log.e("refresh", "Enter Refresh function!");
            post_data.addAll(0, refresh_data);
            adapter.notifyDataSetChanged();
            swipeRefreshLayout.setRefreshing(false);
        }
    }, 1000);
}

}

这是我的适配器:

package playground.com.pgapp;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

public class PostCardImageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static final int Post_Layout = 0;
    public static final int AD_Layout = 1;

    private Context mContext;
    private ArrayList<PostCard> mData;

    private LayoutInflater mLayoutInflater;
    private int mBottomCount = 1;

    public PostCardImageAdapter(Context context, ArrayList<PostCard> data) {
        this.mContext = context;
        this.mData = data;
        mLayoutInflater = LayoutInflater.from(context);
    }

    public int getContentItemCount(){
        return mData.size();
    }

    public boolean isBootomView(int position){
        return mBottomCount != 0 && position >= getContentItemCount();
    }

    @Override
    public int getItemViewType(int position) {
        int dataItemCount = getContentItemCount();

        if(mBottomCount != 0 && position >= dataItemCount){
            return AD_Layout;
        }

        else{
            return Post_Layout;
        }
    }

    // post ViewHolder.
    public static class ItemViewHolder extends RecyclerView.ViewHolder {
        private ImageView postcard_userIcon;
        private TextView postcard_name;
        private TextView postcard_time;
        private TextView postcard_postText;
        private ImageView postcard_postPic;
        private TextView postcard_likes;
        private TextView postcard_comments;

        public ItemViewHolder(View itemView) {
            super(itemView);
            postcard_userIcon = (ImageView) itemView.findViewById(R.id.postcard_userIcon);
            postcard_name = (TextView) itemView.findViewById(R.id.postcard_name);
            postcard_time = (TextView) itemView.findViewById(R.id.postcard_time);
            postcard_postText = (TextView) itemView.findViewById(R.id.postcard_postText);
            postcard_postPic = (ImageView) itemView.findViewById(R.id.postcard_postPic);
            postcard_likes = (TextView) itemView.findViewById(R.id.postcard_likes);
            postcard_comments = (TextView) itemView.findViewById(R.id.postcard_comments);
        }
    }

    // ad ViewHolder.
    public static class FooterViewHolder extends RecyclerView.ViewHolder {

        public FooterViewHolder(View itemView) {
            super(itemView);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if(viewType == Post_Layout){
            return new ItemViewHolder(mLayoutInflater.inflate(R.layout.postcard_layout, parent, false));
        }

        else if(viewType == AD_Layout){
            return new FooterViewHolder(mLayoutInflater.inflate(R.layout.footerview, parent, false));
        }

        return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {

        if(holder instanceof ItemViewHolder){
            PostCard post = mData.get(position);

            ((ItemViewHolder) holder).postcard_name.setText(post.name);
            ((ItemViewHolder) holder).postcard_time.setText(post.time);
            ((ItemViewHolder) holder).postcard_postText.setText(post.postText);
            ((ItemViewHolder) holder).postcard_likes.setText(post.likes);
            ((ItemViewHolder) holder).postcard_comments.setText(post.comments);
            Glide.with(mContext)
                    .load(post.userIconUrl)
                    .into(((ItemViewHolder) holder).postcard_userIcon);
            Glide.with(mContext)
                    .load(post.postPicUrl)
                    .into(((ItemViewHolder) holder).postcard_postPic);
        }

        else if(holder instanceof FooterViewHolder){

        }
    }

    @Override
    public int getItemCount() {
        return getContentItemCount() + mBottomCount;
    }
}

这是我的 footerview.xml:

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

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/ad_scrollview">

        <com.facebook.ads.NativeAdLayout
            android:id="@+id/native_ad_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" />
    </ScrollView>

</FrameLayout>

您需要创建两个视图持有者class。您已经在 PostCardImageAdapter 中创建了其中一个。

然后在 PostCardImageAdapter 中创建 FooterViewHolder

class FooterViewHolder extends RecyclerView.ViewHolder {
FooterViewHolder(View itemView) {
        super(itemView);
    }
}

覆盖 PostCardImageAdapter 中的 getItemViewType 并准备最后一项将是 FooterViewHolder。

@Override
public int getItemViewType(int position) {
  if(position < data.size())
     return ITEM; 
  else
     return FOOTER;
}

使用方法的 viewType 参数在 onCreateViewHolder 中创建您的 viewholder

if (viewType == ITEM) 
  return new ItemViewHolder();
else
  return new FooterViewHolder();

简而言之,RecyclerView 不存在任何页脚或页眉视图,您可以使用 viewholder 分隔每个不同的视图,然后使用位置或数据状态。