Skip to content
This repository has been archived by the owner on Feb 8, 2022. It is now read-only.

I read almost all the issues but still unable to resolve this issue of Scroll layout #340

Open
chnouman opened this issue Feb 9, 2018 · 2 comments

Comments

@chnouman
Copy link

chnouman commented Feb 9, 2018

i have tried lot of different ways but didn't resolve this issue.

This is my adapter.

public class PopularAdapter extends RecyclerView.Adapter<BaseViewHolder> {

    public static final int VIEW_TYPE_EMPTY = 0;
    public static final int VIEW_TYPE_NORMAL = 1;

    private PopularAdapter.Callback mCallback;
    private List<Image> mBlogResponseList;

    public PopularAdapter(List<Image> blogResponseList) {
        mBlogResponseList = blogResponseList;
    }

    public void setCallback(PopularAdapter.Callback callback) {
        mCallback = callback;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        holder.onBind(position);
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        switch (viewType) {
            case VIEW_TYPE_NORMAL:
                return new PopularAdapter.ViewHolder(
                        LayoutInflater.from(parent.getContext()).inflate(R.layout.item_latest_view, parent, false));
            case VIEW_TYPE_EMPTY:
            default:
                return new PopularAdapter.EmptyViewHolder(
                        LayoutInflater.from(parent.getContext()).inflate(R.layout.item_empty_view, parent, false));
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (mBlogResponseList != null && mBlogResponseList.size() > 0) {
            return VIEW_TYPE_NORMAL;
        } else {
            return VIEW_TYPE_EMPTY;
        }
    }

    @Override
    public int getItemCount() {
        if (mBlogResponseList != null && mBlogResponseList.size() > 0) {
            return mBlogResponseList.size();
        } else {
            return 1;
        }
    }

    public void addItems(List<Image> blogList) {
        mBlogResponseList.addAll(blogList);
        notifyDataSetChanged();
    }

    public interface Callback {
        void onBlogEmptyViewRetryClick();
    }

    public class ViewHolder extends BaseViewHolder {

        @BindView(R.id.cover_image_view)
        ImageView coverImageView;

        @BindView(R.id.title_text_view)
        TextView titleTextView;



        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        protected void clear() {
            coverImageView.setImageDrawable(null);
            titleTextView.setText("");

        }

        public void onBind(int position) {
            super.onBind(position);

            final Image  blog= mBlogResponseList.get(position);


            if (blog.getPath() != null) {
                Glide.with(itemView.getContext())
                        .load(blog.getPath())
                        //.asBitmap()
                        //.centerCrop()
                        .into(coverImageView);
            }

            if (blog.getId() != null) {
                titleTextView.setText(blog.getLikes()+"");
            }

           


            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //if (blog.getThumbail() != null) {
                    try {
                        Intent intent = ImageDetailActivity.getStartIntent(itemView.getContext());
                        intent.putExtra("object",blog);
                        intent.putExtra("comeFrom","popular");
                        itemView.getContext().startActivity(intent);

                    } catch (Exception e) {
                        AppLogger.d("url error");
                    }
                }
                // }
            });
        }
    }

    public class EmptyViewHolder extends BaseViewHolder {

        @BindView(R.id.btn_retry)
        Button retryButton;

        @BindView(R.id.tv_message)
        TextView messageTextView;

        public EmptyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @Override
        protected void clear() {

        }

        @OnClick(R.id.btn_retry)
        void onRetryClick() {
            if (mCallback != null)
                mCallback.onBlogEmptyViewRetryClick();
        }
    }
}

This is my item layout.

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

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        app:cardCornerRadius="2dp"
        app:cardElevation="2dp"
        android:layout_marginBottom="@dimen/cardMarginVertical"
        android:layout_marginLeft="@dimen/cardMarginHorizontal"
        android:layout_marginRight="@dimen/cardMarginHorizontal"
        android:layout_marginTop="@dimen/cardMarginVertical"
        app:cardPreventCornerOverlap="false"
        app:contentPadding="0dp">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:background="@color/purple"
     >
    <ImageView
        android:id="@+id/cover_image_view"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:scaleType="centerCrop"
        android:src="@drawable/splash_bg"
        />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_alignParentBottom="true"
            android:gravity="right"
            android:background="#66000000"
            >
            <ImageView
                android:layout_width="20dp"
                android:layout_height="20dp"
                app:srcCompat="@drawable/ic_views"
                android:layout_gravity="center_vertical"
                android:layout_marginRight="8dp"
                android:layout_marginLeft="8dp"
                android:tint="@color/white"

                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:textColor="@android:color/white"
                android:text="0"
                android:textSize="10sp"
                android:gravity="center_vertical"
                android:layout_gravity="center_vertical"
                android:id="@+id/title_text_view"
                android:layout_marginRight="8dp"
                />
        </LinearLayout>
    </RelativeLayout>
</RelativeLayout>
    </android.support.v7.widget.CardView>
</FrameLayout>
 

This is my Fragment.

public class PopularFragment extends BaseFragment implements PopularMVPView,
        PopularAdapter.Callback
{
    private static final boolean GRID_LAYOUT = true;
    private static final int ITEM_COUNT = 100;

    static String TAG = "PopularFragment";
    int pastVisiblesItems, visibleItemCount, totalItemCount;
    private boolean userScrolled = true;

    @Inject
    PopularMVPPresenter<PopularMVPView> mPresenter;
    @Inject
    PopularAdapter mPopularAdapter;
    @Inject
    GridLayoutManager mLayoutManager;
    @BindView(R.id.blog_recycler_view)
    RecyclerView mRecyclerView;




    private int pageNmr = 0;
    public static PopularFragment newInstance() {
        return new PopularFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_latest, container, false);
        ActivityComponent component = getActivityComponent();
        if (component != null) {
            component.inject(this);
            setUnBinder(ButterKnife.bind(this, v));
            mPresenter.onAttach(this);
            mPopularAdapter.setCallback(this);
        }
        return v;
    }


    @Override
    protected void setUp(View view) {
 
        mPresenter.onViewPrepared(pageNmr);
    }



    @Override
    public void onBlogEmptyViewRetryClick() {

    }
    @Override
    public void onDestroyView() {
        mPresenter.onDetach();
        super.onDestroyView();
    }

    @Override
    public void updateBlog(List<Image> imagesList) {

        this.pageNmr = pageNmr;
        Log.wtf("PopularFragment", "updateBlog: " );
        if (imagesList!=null){
            //Log.wtf("PopularFragment", "updateBlog: "+imagesList.size() );
            mPopularAdapter.addItems(imagesList);

        }
    }
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this, view);

        //setup materialviewpager



        if (GRID_LAYOUT) {
            mRecyclerView.setLayoutManager(mLayoutManager);
        } else {
            mRecyclerView.setLayoutManager(mLayoutManager);
        }


        mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                // Here get the child count, item count and visibleitems
                // from layout manager

                visibleItemCount = mLayoutManager.getChildCount();
                totalItemCount = mLayoutManager.getItemCount();
                pastVisiblesItems = mLayoutManager
                        .findFirstVisibleItemPosition();

                // Now check if userScrolled is true and also check if
                // the item is end then update recycler view and set
                // userScrolled to false
                if (userScrolled
                        && (visibleItemCount + pastVisiblesItems) == totalItemCount) {
                    userScrolled = false;
             
                    if (!getBaseActivity().isNetworkConnected()) {
                        getBaseActivity().onError("Internet is not Connected");
                        return;
                    }
 

                     Log.wtf(TAG, "onScrolled: reach end" );

                    mPresenter.loadMoreItems();

                }
            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

                // If scroll state is touch scroll then set userScrolled
                // true
                if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
                    userScrolled = true;

                }
            }
        });
        // Adds the scroll listener to RecyclerView
         mRecyclerView.setHasFixedSize(true);

        //Use this now
        mRecyclerView.addItemDecoration(new MaterialViewPagerHeaderDecorator());
        mRecyclerView.setAdapter(mPopularAdapter);
        //MaterialViewPagerHelper.registerScrollView(getActivity(), mScrollView);
    }


}
@chnouman
Copy link
Author

chnouman commented Feb 9, 2018

I have tried lot of different ways my all fragments have same layout.
mostly the error comes when there is some data in the list items otherwise with your given RecyclerViewFragment it works fine. dont know why but it. i have copied your RecyclerViewFragments item and added my image view and text views in your given layout but still get error.

@chnouman
Copy link
Author

chnouman commented Feb 9, 2018

@rameshvarun
@vijayrawatsan
@dan-silver
@championswimmer
@adamaviner
@florent37
@filipedelimabrito
please kindly help me if you need more information or may be video demonstration i will provide you.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant