为什么回收站视图无法加载更多功能?

时间:2016-07-15 12:57:36

标签: android android-recyclerview

当执行代码那个时间,空指针到来时,我无法理解下一步该做什么?

activity_main.xml中

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout    
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:tools="http://schemas.android.com/tools"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:orientation="vertical"
         android:paddingBottom="@dimen/activity_vertical_margin"
         android:paddingLeft="@dimen/activity_horizontal_margin"
         android:paddingRight="@dimen/activity_horizontal_margin"
         android:paddingTop="@dimen/activity_vertical_margin"
         tools:context="com.siliconinfo.loadmoredemo.MainActivity">

         <Button
             android:id="@+id/openFragment"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:text="Click to open" />

             <FrameLayout android:id="@+id/contain"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 android:background="@color/grey_dark"
                  xmlns:android="http://schemas.android.com/apk/res/android">

            </FrameLayout>
    </LinearLayout>

fragment_product.xml

    <?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:id="@+id/filterSortLayout"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:background="@color/white_dim_filter"
           android:visibility="gone"
           android:orientation="horizontal">

          <TextView
              android:id="@+id/filterProductLsit"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:drawableLeft="@mipmap/ic_filter_icon"
              android:drawablePadding="@dimen/margin_10"
              android:gravity="center_vertical"
              android:padding="@dimen/margin_5"
              android:text="@string/filter_text"
              android:textColor="@color/grey_dark"
              android:textSize="@dimen/drawer_subheader" />

          <TextView
              android:id="@+id/sortProductList"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:layout_marginLeft="@dimen/margin_5"
              android:drawableLeft="@mipmap/ic_sort_icon"
              android:drawablePadding="@dimen/margin_10"
              android:gravity="center_vertical"
              android:padding="@dimen/margin_5"
              android:text="@string/sort_text"
              android:textColor="@color/grey_dark"
              android:textSize="@dimen/drawer_subheader" />

          <ImageView
              android:id="@+id/gridListToggle"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center_vertical"
              android:padding="@dimen/activity_horizontal_margin"
              android:src="@mipmap/gri_view_icon" />
      </LinearLayout>

      <View
          android:layout_width="match_parent"
          android:layout_height="1px"
          android:background="@color/skyblue_view" />

     <TextView
         android:id="@+id/noDataMessageText"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:gravity="center"
         android:text="@string/no_item_found"
         android:visibility="gone" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@mipmap/bestsellers_mainbg">

        <ProgressBar
            android:id="@+id/progressBarItemsRecycler"
            style="?android:progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center" />

       <android.support.v7.widget.RecyclerView
            android:id="@+id/productListRecyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/white_dim_details"
            android:fitsSystemWindows="true"
            android:visibility="gone" />

      <ProgressBar
            android:id="@+id/progressBarItemsRecyclerOuter"
            style="?android:progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:visibility="gone" />
    </FrameLayout>
    </LinearLayout>

在适配器中设置的行布局

row_gridviewtype.xml

    <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@mipmap/bestsellers_pro_listbg"
            android:orientation="vertical"
            android:padding="@dimen/margin_5">

                <FrameLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center">

               <ProgressBar
               android:id="@+id/progressBarItems"
               style="?android:progressBarStyleSmall"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_gravity="center" />

          <ImageView
              android:id="@+id/imageview"
              android:layout_width="@dimen/row_accessories_grid_width"
              android:layout_height="@dimen/row_accessories_grid_width_hight"
              android:src="@mipmap/no_image_available"
              android:layout_gravity="center_horizontal" />
     </FrameLayout>

     <TextView
         android:id="@+id/productNameText"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:gravity="center_horizontal"
         android:singleLine="true"
         android:textSize="@dimen/text_header" />

     <TextView
         android:id="@+id/productPriceText"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:gravity="center_horizontal"
         android:paddingBottom="@dimen/margin_5"
         android:paddingTop="@dimen/margin_5"
         android:singleLine="true"
         android:textColor="@color/skyblue_view"
         android:textSize="@dimen/text_header" />

     <TextView
         android:id="@+id/productRegularPriceText"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:gravity="center_horizontal"
         android:paddingBottom="@dimen/margin_5"
         android:paddingTop="@dimen/margin_5"
         android:singleLine="true"
         android:textColor="@color/grey"
         android:textSize="@dimen/text_header" />
    </LinearLayout>

这是进度条,在滚动回收站视图时加载到recyclerview下方。

row_load.xml

    <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp">
        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/progressBar"
            android:layout_gravity="center_horizontal" />
        </LinearLayout>

DesktopFragment cointain list

    public class DeskPortTprtOurFvtAromaThrpyFragment extends Fragment implements View.OnClickListener {
    private static String TAG ="DeskPortTprtOurFvtAromaThrpyFragment";
    private View mRoot;
    private SharedPreferences mPreferences;
    private Typeface mTypefaceBold,mTypefaceSemiBold,mTypefaceRegular;
    private RecyclerView mProductListRecyclerView;
    privateArrayList<ProductListResponseModel>
    mProductListResponseModelArrayList = new   ArrayList<ProductListResponseModel>();
    private ProgressBar mProgressBarRecycleView, mProgressBarOuter;
    private boolean mVieType = true;
    private String
    mProductResponsePortableStr,mProductResponseDesktopStr,
    mProductResponseTopRatedStr,mProductResponseOurFavouriteStr,
    mProductResponseAromaTherapyStr,  mCategoryStr;
    String pageNumber = "", shortValue = "", storeId = "", shopByFrom= "", shopByTo = "";
    private TextView mFilterTextView, mSortTextView;
    private ArrayList<String> categoryID = new ArrayList<String>();
    ArrayList<String> mCAtegoryIdFilter = new ArrayList<String>();
    private String shopByFrom1 = "", shopByFrom2 = "", shopByFrom3="",  shopByTo1 = "", shopByTo2 = "", shopByTo3 = "";
    View.OnClickListener mFilterClickListener;
    private String mFilterTag = "NO";
    private boolean zeroPosition = false, onePosition = false, secondPositon = false, thirdPosition = false;
    String pageNo = "0";
    private ImageView mToggleViewListGridImageView;
    Bundle productID;
    ProductListAdaptr mProductListAdaptr;
    private boolean loading = true;
    int pastVisiblesItems, visibleItemCount, totalItemCount;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup  container, Bundle savedInstanceState) {
    mRoot = inflater.inflate(R.layout.fragment_product, container,false);
    initView();
    return mRoot;
    }
    private void initView() {
    mPreferences = getActivity().getSharedPreferences(Utils.PREF_NAME,   Context.MODE_PRIVATE);
    mToggleViewListGridImageView = (ImageView)mRoot.findViewById(R.id.gridListToggle);
     ((ImageView)mRoot.findViewById(R.id.gridListToggle)).setOnClickListener(this);

    mProductListRecyclerView = (RecyclerView) mRoot.findViewById(R.id.productListRecyclerView);
    mFilterTextView = (TextView) mRoot.findViewById(R.id.filterProductLsit);
    mSortTextView = (TextView) mRoot.findViewById(R.id.sortProductList);
    ((TextView) mRoot.findViewById(R.id.filterProductLsit)).setOnClickListener(this);
    ((TextView) mRoot.findViewById(R.id.sortProductList)).setOnClickListener(this);

    Logger.e("TAG", "Product ID is : " + categoryID);
    mProgressBarRecycleView = (ProgressBar) mRoot.findViewById(R.id.progressBarItemsRecycler);
    mProgressBarOuter = (ProgressBar) mRoot.findViewById(R.id.progressBarItemsRecyclerOuter);
    categoryID.add("10");
    mProductListAdaptr = new ProductListAdaptr(getContext(), mProductListResponseModelArrayList, mProductClickListener, 0);

    final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
    mProductListRecyclerView.setLayoutManager(linearLayoutManager);
    mProductListRecyclerView.setItemAnimator(new DefaultItemAnimator());

    mProductListAdaptr.setLoadMoreListener(new ProductListAdaptr.OnLoadMoreListener() {
        @Override
        public void onLoadMore() {
            mProductListRecyclerView.post(new Runnable() {
                @Override
                public void run() {
                    int index = mProductListResponseModelArrayList.size() - 1;
                    loadMore(index);
                }
            });
        }
    });
    load(0);
    }

    private void load(int index) {
    getProductList(categoryID, String.valueOf(index), "", "", "", "",  "", "", "", "", "");
    }

    private void loadMore(int index) {

    }


    private void showProductList() {
    ((LinearLayout)  mRoot.findViewById(R.id.filterSortLayout)).setVisibility(View.VISIBLE);
    ((TextView)  mRoot.findViewById(R.id.noDataMessageText)).setVisibility(View.GONE);
    mProductListRecyclerView.setVisibility(View.VISIBLE);
    if (mVieType == true) {
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
    mProductListRecyclerView.setLayoutManager(linearLayoutManager);
    ProductListRecyclerView.setItemAnimator(newDefaultItemAnimator());
    mProductListRecyclerView.setAdapter(new    ProductListAdaptr(getContext(), mProductListResponseModelArrayList,   mProductClickListener, 0));
    } else if (mVieType == false) {
    final GridLayoutManager mLayoutManager = new   GridLayoutManager(getActivity(), 2);
    mProductListRecyclerView.setLayoutManager(mLayoutManager);
    mProductListRecyclerView.setItemAnimator(new  DefaultItemAnimator());
    mProductListRecyclerView.setAdapter(new   ProductListAdaptr(getContext(), mProductListResponseModelArrayList,  mProductClickListener, 1));
    }
     mProductListResponseModelArrayList.addAll(mProductListResponseModelArrayList);
    mProductListAdaptr.notifyDataChanged();
    }

    private View.OnClickListener mProductClickListener = new   View.OnClickListener() {
    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();
    }
    };

    private void getProductList(ArrayList<String> categoryId, String  pagenumber, String shopByFrom1, String shopByTo1,
                            String shopByFrom2, String shopByTo2,   String shopByFrom3, String shopByTo3, String shortValue, String storesID,  String FilterTag) {
    String categoryListString = "";
    for (int i = 0; i < categoryId.size(); i++) {
        if (i == 0)
            categoryListString = categoryId.get(i).toString();
        else
            categoryListString = categoryListString + ", " +  categoryId.get(i).toString();
    }
    if (Utils.checkInternetConnection(getActivity())) {
        final ProdcutListRequestTask mProductListRequestTask = new  ProdcutListRequestTask(getActivity());
        mProductListRequestTask.setAsyncCallListener(new   AsyncCallListener() {
        @Override
        public void onResponseReceived(Object response) {
        mProductListResponseModelArrayList  (ArrayList<ProductListResponseModel>) response;
        ((LinearLayout)mRoot.findViewById(R.id.filterSortLayout)).setVisibility(View.VISIBLE);
        mProgressBarOuter.setVisibility(View.GONE);
        mProgressBarRecycleView.setVisibility(View.GONE);
        if (mProductListResponseModelArrayList != null) {
        Logger.e(TAG, "DektoPportaOurFavouriteFragment  : " + mProductListResponseModelArrayList.size());
        }
        if (response == null) return;
                if (mProductListResponseModelArrayList == null || mProductListResponseModelArrayList.size() == 0) {
                    ((TextView) mRoot.findViewById(R.id.noDataMessageText)).setTypeface(mTypefaceBold);
                    ((TextView) mRoot.findViewById(R.id.noDataMessageText)).setVisibility(View.VISIBLE);
                    ((LinearLayout) mRoot.findViewById(R.id.filterSortLayout)).setVisibility(View.GONE);
                    return;
                }

                showProductList();
            }

            @Override
            public void onErrorReceived(String error) {
                mProgressBarOuter.setVisibility(View.GONE);
            }
        });
        mProductListRequestTask.execute(categoryListString, pagenumber, shopByFrom1, shopByTo1, shopByFrom2, shopByTo2, shopByFrom3, shopByTo3,
                shortValue, storesID, FilterTag);
    } else {
        Toast.makeText(getActivity(), getString(R.string.no_internet), Toast.LENGTH_SHORT).show();
        mProgressBarOuter.setVisibility(View.GONE);
    }
    }
    }

1 个答案:

答案 0 :(得分:0)

使用它,为我工作。

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                    @Override
                    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                        super.onScrolled(recyclerView, dx, dy);
                        if (hasMore && !(hasFooter())) {
                            LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                            //position starts at 0
                            if (layoutManager.findLastCompletelyVisibleItemPosition() == layoutManager.getItemCount() - 2) {
                                new GetPostData().execute();
                            }
                        }
                        else{

                        }
                    }
                });

希望它也会帮助你。