1. Add dependencies
implementation ‘androidx.recyclerview:recyclerview:1.2.0-alpha03’
2. Build the Item layout
3. Build ItemBean
public class ItemBean_List {
private String id = "" ;
private String productModel = "" ;
ItemBean_List ( String id, String productModel) {
this . id = id;
this . productModel = productModel;
}
public String getId ( ) {
return id;
}
public void setId ( String id) {
this . id = id;
}
public String getProductModel ( ) {
return productModel;
}
public void setProductModel ( String productModel) {
this . productModel = productModel;
}
}
4. Build Adapter
public class ListViewAdapter extends RecyclerView. Adapter < ListViewAdapter. InnerHolder> {
private List< ItemBean_List> mData;
private static OnItemClickListener mOnItemClickListener;
void setData ( List< ItemBean_List> data) {
mData = data;
}
@NonNull
@Override
public InnerHolder onCreateViewHolder ( @NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater. from ( parent. getContext ( ) ) . inflate ( R. layout. item, parent, false ) ;
return new InnerHolder ( view) ;
}
@Override
public void onBindViewHolder ( @NonNull InnerHolder holder, int position) {
holder. setData ( mData. get ( position) , position) ;
}
@Override
public int getItemCount ( ) {
if ( mData != null) {
return mData. size ( ) ;
}
return 0 ;
}
static class InnerHolder extends RecyclerView. ViewHolder {
private static final String TAG = "InnerHolder" ;
TextView textView1, textView2;
int mPosition;
InnerHolder ( @NonNull View itemView) {
super ( itemView) ;
textView1 = itemView. findViewById ( R. id. textView1) ;
textView2 = itemView. findViewById ( R. id. textView2) ;
itemView. setOnClickListener ( new View. OnClickListener ( ) {
@Override
public void onClick ( View v) {
mOnItemClickListener. onClick ( mPosition, v) ;
}
} ) ;
}
void setData ( ItemBean_List itemBean, int position) {
textView1. setText ( itemBean. getId ( ) ) ;
textView2. setText ( itemBean. getProductModel ( ) ) ;
mPosition = position;
}
}
void setItemClickListener ( OnItemClickListener listener) {
mOnItemClickListener = listener;
}
public interface OnItemClickListener {
void onClick ( int position, View v) ;
}
}
use
List< ItemBean_List> mData_List = new ArrayList < > ( ) ;
LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager ( this ) ;
mLinearLayoutManager. setOrientation ( LinearLayoutManager. VERTICAL) ;
mRecyclerView. setLayoutManager ( mLinearLayoutManager) ;
ListViewAdapter listViewAdapter = new ListViewAdapter ( ) ;
for ( int i = 1 ; i < 20 ; i++ ) {
mData_List. add ( new ItemBean_List ( "A" + i, "xxxxx" ) ) ;
}
listViewAdapter. setData ( mData_List) ;
listViewAdapter. setItemClickListener ( new ListViewAdapter. OnItemClickListener ( ) {
@Override
public void onClick ( int position, View v) {
Log. d ( TAG, "onClick: " + position) ;
}
} ) ;
mRecyclerView. setAdapter ( listViewAdapter) ;
Data refresh
Update mData_List, and then execute listViewAdapter.notifyDataSetChanged();
It should be noted that the listViewAdapter here must keep the original object, and it is invalid to use notifyDataSetChanged after passing the parameter.
Infinite loop (pseudo)
The pseudo-infinite loop will still roll to the end, but it may require the program to continue to run for decades (I recognize it)
The first step is to modify getItemCount() to make the list very long
@Override
public int getItemCount ( ) {
if ( mData != null) {
return Integer. MAX_VALUE;
}
return 0 ;
}
The second step is to modify onBindViewHolder() to bind the data loop
@Override
public void onBindViewHolder ( @NonNull LongListViewAdapter. InnerHolder holder, int position) {
if ( mData != null && mData. size ( ) != 0 )
holder. setData ( mData. get ( position % mData. size ( ) ) , position) ;
}
The other parts are consistent and unchanged
Another circular idea
After scrolling to the bottom, go back to the top and swipe again
Then we need to know the event of sliding to the bottom. There are two methods. The first is to check whether the last position is the last one in the list after each sliding.
recyclerView. addOnScrollListener ( new RecyclerView. OnScrollListener ( ) {
@Override
public void onScrollStateChanged ( @NonNull RecyclerView recyclerView, int newState) {
if ( newState == RecyclerView. SCROLL_STATE_IDLE) {
if ( mLinearLayoutManager. findLastCompletelyVisibleItemPosition ( ) == mListViewAdapter_view1_recyclerView. getItemCount ( ) ) {
}
}
super . onScrollStateChanged ( recyclerView, newState) ;
}
@Override
public void onScrolled ( @NonNull RecyclerView recyclerView, int dx, int dy) {
super . onScrolled ( recyclerView, dx, dy) ;
}
} ) ;
The second is to customize RecyclerView (external call parameters are also OK, but this is better)
public boolean isSlideToBottom ( ) {
return computeVerticalScrollExtent ( ) + computeVerticalScrollOffset ( ) >= computeVerticalScrollRange ( ) ;
}