[Quick use] RecyclerView (LinearLayoutManager)

1. Add dependencies

  • implementation ‘androidx.recyclerview:recyclerview:1.2.0-alpha03’

2. Build the Item layout

  • slightly

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();
}

Guess you like

Origin blog.csdn.net/qq_36881363/article/details/106302248