recycerview不显示arraylist数据

时间:2019-09-02 20:11:07

标签: android api android-recyclerview recycler-adapter

嗨,我正在尝试使用MVVM制作片段,并尝试从我的API进行调用,以列出故事以及用户帖子/图片,但是目前这些故事运行良好,并且我可以看到它也从API获取了数据将数据保存到userpost / picture中,但是我没有将其插入视图中,希望这里有人可以帮助我:)

片段中的代码:

private ArrayList<StoryList> storyArrayList;
private ArrayList<FeedData> feedArrayList;
private FeedViewModel feedViewModel;
private StoryAdapter storyAdapter;
private FeedAdapter feedAdapter;
private LinearLayoutManager newsFeedLayoutManager;

private void setupView() {
    mBinding.storyRecyler.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
    storyArrayList = new ArrayList<>();
    storyAdapter = new StoryAdapter(getActivity(), storyArrayList);
    mBinding.storyRecyler.setAdapter(storyAdapter);

    newsFeedLayoutManager = new LinearLayoutManager(getContext());
    mBinding.recylerFeed.setLayoutManager(newsFeedLayoutManager);
    feedArrayList = new ArrayList<>();
    feedAdapter = new FeedAdapter(getActivity(), feedArrayList);
    mBinding.recylerFeed.setAdapter(feedAdapter);
    mBinding.recylerFeed.hasFixedSize();
    mBinding.recylerFeed.setItemViewCacheSize(10);
}

@Override
public void storyresponse(Object object) {
    if (object instanceof StoryResponse) {
        StoryResponse response = (StoryResponse) object;
        storyArrayList.addAll(response.getData());
        storyAdapter.notifyDataSetChanged();
    }

    if (object instanceof FeedResponse) {
        FeedResponse feedresponse = (FeedResponse) object;
        feedArrayList.addAll(feedresponse.getFeeddata());
        feedAdapter.notifyDataSetChanged();
    }
}

以及适配器代码:

public class FeedAdapter extends RecyclerView.Adapter<FeedAdapter.ViewHolder> {
    private final List<FeedData> feedList;
    private Activity context;
    private View.OnClickListener onClickListener;

    public FeedAdapter(Activity context, List<FeedData> feedList) {
        this.context = context;
        this.feedList = feedList;
        this.onClickListener = onClickListener;
    }

    public List<FeedData> getFeedData(){
        return feedList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_feed_photo, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.mBinding.setFeed(feedList.get(position));
        Boolean isLiked = holder.mBinding.getFeed().getIsLiked();
        String username = holder.mBinding.getFeed().getUsername();

        holder.mBinding.username.setText(username);
    }

    @Override
    public int getItemCount() {
        return feedList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        ListItemFeedPhotoBinding mBinding;

        public ViewHolder(View itemView) {
            super(itemView);
            mBinding = DataBindingUtil.bind(itemView);
        }

    }

    public interface ClickListener {
        void onClick(View view, int position);

        void onLongClick(View view, int position);
    }

    public static class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {

        private GestureDetector gestureDetector;
        private FeedAdapter.ClickListener clickListener;

        public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final FeedAdapter.ClickListener clickListener) {
            this.clickListener = clickListener;
            gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
                @Override
                public boolean onSingleTapUp(MotionEvent e) {
                    return true;
                }

                @Override
                public void onLongPress(MotionEvent e) {
                    View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
                    if (child != null && clickListener != null) {
                        clickListener.onLongClick(child, recyclerView.getChildPosition(child));
                    }
                }
            });
        }

        @Override
        public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {

            View child = rv.findChildViewUnder(e.getX(), e.getY());
            if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
                clickListener.onClick(child, rv.getChildPosition(child));
            }
            return false;
        }

        @Override
        public void onTouchEvent(RecyclerView rv, MotionEvent e) {
        }

        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

        }
    }
}

该片段的XML:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:bind="http://schemas.android.com/apk/res/android">
    <data>
    </data>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:background="#ffff"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.coordinatorlayout.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <com.google.android.material.appbar.AppBarLayout
                android:id="@+id/appBarLayout"
                android:layout_width="match_parent"
                android:layout_marginTop="0dp"
                android:background="#ffff"
                android:layout_marginBottom="-2dp"
                android:layout_height="wrap_content">

                <com.google.android.material.appbar.CollapsingToolbarLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:layout_scrollFlags="scroll|exitUntilCollapsed">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:orientation="vertical"
                        app:layout_collapseMode="parallax"
                        android:layout_marginTop="5dp"
                        android:layout_height="match_parent">
                        <View android:layout_width="match_parent"
                            android:layout_height="1dp"
                            android:background="#efefef"/>
                        <LinearLayout
                            android:id="@+id/storiesLayout"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="5dp"
                            android:layout_marginEnd="5dp"
                            android:layout_marginTop="5dp"
                            android:paddingBottom="6dp"
                            android:background="#ffff"
                            android:orientation="vertical">
                            <androidx.recyclerview.widget.RecyclerView
                                android:id="@+id/storyRecyler"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="0dp"
                                android:layout_alignParentTop="true"
                                android:layout_marginBottom="0dp"
                                android:scrollbars="vertical"
                                android:layout_marginEnd="0dp"
                                app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
                        </LinearLayout>

                    </LinearLayout>
                </com.google.android.material.appbar.CollapsingToolbarLayout>

            </com.google.android.material.appbar.AppBarLayout>

            <LinearLayout
                android:id="@+id/material_style_ptr_frame"
                android:layout_marginBottom="48dp"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/recylerFeed"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="-4dp"
                    android:layout_alignParentTop="true"
                    android:layout_marginBottom="48dp"
                    android:scrollbars="vertical"
                    android:background="#f3f3f3"
                    android:layout_marginEnd="-5dp" />

            </LinearLayout>

        </androidx.coordinatorlayout.widget.CoordinatorLayout>

        <ProgressBar
            android:layout_centerInParent="true"
            android:id="@+id/sectionProgress"
            android:layout_width="50dp"
            android:layout_height="50dp" >
        </ProgressBar>

        <com.hitomi.cmlibrary.CircleMenu
            android:id="@+id/circle_menu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:visibility="gone" />
    </RelativeLayout>
</layout>

0 个答案:

没有答案