FrameLayout не может отображаться в RecyclerView фрагмента - PullRequest
0 голосов
/ 15 апреля 2019

Я использую ListView и addFooterView, чтобы вставить FrameLayout в Fragment, это работает!

Но теперь я хочу использовать RecyclerView и добавить FrameLayout в нижний колонтитул RecyclerView в Fragment.

Но когда показываются данные списка, 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);
}

}

Вот мой adpter:

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>

1 Ответ

1 голос
/ 15 апреля 2019

Вам необходимо создать два вида держателя класса.Одним из них вы уже создали, который внутри PostCardImageAdapter.

И вы создаете FooterViewHolder внутри PostCardImageAdapter

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

Переопределите getItemViewType внутри PostCardImageAdapter и подготовьте последний элемент FooterViewHolder.

@Override
public int getItemViewType(int position) {
  if(position < data.size())
     return ITEM; 
  else
     return FOOTER;
}
1008 * Создайте свой видоискатель внутри onCreateViewHolder с параметром метода viewType
if (viewType == ITEM) 
  return new ItemViewHolder();
else
  return new FooterViewHolder();

Вкратце, в RecyclerView не было никакого нижнего колонтитула или вида заголовка, который можно разделить для каждого отдельного вида с помощью виджета, а затем использовать с положением или состоянием данных.

...