📜 ⬆️ ⬇️

The implementation of the list with the title, footer and pagination in Android

image


RecyclerView


RecyclerView is an enhanced version of ListView with some performance improvements and new features. As the name implies, RecyclerView recycles or reuses item views when scrolling. RecyclerView makes it much easier to add animations than ListView. In this tutorial, we will explain how to create a RecyclerView with a title, footer, pagination, and animation.


Gradle setting


Add the following dependency to the build.gradle file:


//,    compile 'com.android.support:recyclerview-v7:23.1.1' 

Adding RecyclerView to an XML View


After the project is synchronized, add the RecyclerView component to your layout:


  <android.support.v7.widget.RecyclerView android:id="@+id/recycleView" android:clipToPadding="false" android:padding="8dp" android:layout_height="match_parent" android:layout_width="match_parent"/> 

Binding XML with JAVA class


Now in the onCreate method of your activity, add the following code:


  @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setcontentview     mRecyclerView = (RecyclerView) findViewById(R.id.recycleView); //  true,   RecyclerView      mRecyclerView.setHasFixedSize(false); //   LayoutManager mLayoutManager = new LinearLayoutManager(this); mRecyclerView.setLayoutManager(mLayoutManager); //      RecyclerView mAdapter = new MyAdapter(mList); mRecyclerView.setAdapter(mAdapter); } 

Before we go further, let's take a closer look at the code above.



Layout Manager Types
  1. LinearLayoutManager displays items in a list with vertical or horizontal scrolling.
  2. GridLayoutManager shows the elements in the grid.
  3. StaggeredGridLayoutManager shows the elements in the chess grid.


RecyclerView ItemDecoration


ItemDecoration allows an application to add custom strips and offsets to specific views of elements from the adapter dataset. This can be useful for drawing delimiters between elements, selections, visual grouping borders, etc. - developer.android.com

In this example, we will use ItemDecoration to add indents to each element.


 public class ItemOffsetDecoration extends RecyclerView.ItemDecoration { private int offset; public ItemOffsetDecoration(int offset) { this.offset = offset; } @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { //      if (parent.getChildAdapterPosition(view) == 0) { outRect.right = offset; outRect.left = offset; outRect.top = offset; outRect.bottom = offset; } } } } 

In the above class, we set the padding to the zero element.


  //  onCreate     RecyclerView //    ,  20 mRecyclerView.addItemDecoration(new ItemOffsetDecoration(20)); 

RecyclerView Adapter


Now let's configure the ReeyclerView adapter with a header and a footer.


  public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private static final int TYPE_HEADER = 0; private static final int TYPE_ITEM = 1; private static final int TYPE_FOOTER = 2; ArrayList<String> mList = new ArrayList<>(); public MyAdapter(ArrayList<String> mList) { this.mList = mList; } @Override public int getItemViewType(int position) { if (isPositionHeader(position)) { return TYPE_HEADER; } else if (isPositionFooter(position)) { return TYPE_FOOTER; } return TYPE_ITEM; } private boolean isPositionHeader(int position) { return position == 0; } private boolean isPositionFooter(int position) { return position > mList.size(); } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { if (viewType == TYPE_ITEM) { View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_layout, viewGroup, false); return new ItemViewHolder(view); } else if (viewType == TYPE_HEADER) { View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.header_layout, viewGroup, false); return new HeaderViewHolder(view); } else if (viewType == TYPE_FOOTER) { View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.footer_layout, viewGroup, false); return new FooterViewHolder(view); } throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly"); } @Override public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) { if (holder instanceof HeaderViewHolder) { //        ,   . ((HeaderViewHolder) holder).txtName.setText(mList.get(position)) //       }else if (holder instanceof ItemViewHolder) { //    }else if (holder instanceof FooterViewHolder) { //    } } @Override public int getItemCount() { //         return this.mList.size() + 2; } // ViewHolders  ,    class HeaderViewHolder extends RecyclerView.ViewHolder { public View View; private final TextView txtName; public HeaderViewHolder(View itemView) { super(itemView); View = itemView; //    ui ,    txtName = (TextView) View.findViewById(R.id.txt_name); } } public class ItemViewHolder extends RecyclerView.ViewHolder { public View View; public ViewHolder(View v) { super(v); View = v; //     . } } public class FooterViewHolder extends RecyclerView.ViewHolder { public View View; public ViewHolder(View v) { super(v); View = v; //     . } } } 

Pagination


Now that the adapter is ready, let's see how to add pagination to the RecyclerView list. This is fairly easy to do and should be added to onCreate after installing Adapter to Recycler-View.


  mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); int lastvisibleitemposition = mLayoutManager.findLastVisibleItemPosition(); if (lastvisibleitemposition == mAdapter.getItemCount() - 1) { if (!loading && !isLastPage) { loading = true; fetchData((++pageCount)); //   1 pagecount          // make loading = false    //  mAdapter.notifyDataSetChanged (),      } } } }); 

Whenever the data changes in mList, call the function below to update the adapter RecyclerView and show the new data.


 mAdapter.notifyDataSetChanged(); 

I hope that this post will help you get a general idea of ​​how to configure RecyclerView with title, basement and pagination.


')

Source: https://habr.com/ru/post/336184/


All Articles