当前位置:网站首页>A simple method of adding dividing lines in recyclerview
A simple method of adding dividing lines in recyclerview
2022-07-03 11:08:00 【Lindroy】
1、 Preface
Just beginning to learn RecyclerView When I learned it from a video , At that time, the dividing line was imported from the outside Java class , Then use the function inside to create the split line , So I have always done this . Until a few days ago, I accidentally found , original v7 One is provided in the package DividerItemDecoration class , Take advantage of it , We can easily realize RecyclerView Dividing line ! What are you waiting for ? Use it quickly .
2、 Create a simple RecyclerView
First of all, of course, there should be one RecyclerView, I won't go into that here , Just make some data . Go straight to the code :
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private ArrayList<String> titles = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initialization RecyclerView
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
for (int i = 0; i < 20; i++) {
titles.add(" title "+i);
}
recyclerView.setAdapter(new RecyclerViewAdapter(this,titles));
}
}RecyclerViewAdapter Code for :
**
* RecyclerView Adapter
*/
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private Context context;
private ArrayList<String> datas;
class ViewHolder extends RecyclerView.ViewHolder {
ImageView ivIcon;
TextView tvTitle;
public ViewHolder(View itemView) {
super(itemView);
ivIcon = (ImageView) itemView.findViewById(R.id.iv_icon);
tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
}
}
public RecyclerViewAdapter(Context context, ArrayList<String> datas) {
this.context = context;
this.datas = datas;
}
/** * amount to getView In the method View and ViewHolder * * @param parent * @param viewType * @return */
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = View.inflate(context, R.layout.item_recyclerview, null);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
itemView.setLayoutParams(lp);
return new ViewHolder(itemView);
}
/** * @param holder * @param position */
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// according to position Get the corresponding data
String data = datas.get(position);
holder.tvTitle.setText(data);
}
/** * Get the total number * * @return */
@Override
public int getItemCount() {
return datas.size();
}
}After running, it's like this :

Obviously , There is no dividing line at this time .
3、 Add default split line
Now let's add the dividing line , First, try the simplest , That is, Android's own split line . Just add a line of code .
// add to Android Its own split line
recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));addItemDecoration Method requires a DividerItemDecoration The object of , When creating its object , We need to pass in the context and the direction of the divider . Run it again , You can see that the dividing line appears :

4、 Custom split line
Sometimes the default light gray split line can't meet our requirements , Then we need ourselves “ Write ” A dividing line , How should I write it ? open DividerItemDecoration Look at the source code , There is such a function :
public void setDrawable(@NonNull Drawable drawable) {
if (drawable == null) {
throw new IllegalArgumentException("Drawable cannot be null.");
}
mDivider = drawable;
}We just need to call this method , Then pass in a Drawable Function object is ok . Now we can use shape To write a divider style :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient android:centerColor="#ff00ff00" // green android:endColor="#ff0000ff" // Blue android:startColor="#ffff0000" // Red android:type="linear" />
<size android:height="3dp" />
</shape>What I got here is a coquettish gradient dividing line . Then the code of adding the dividing line is changed as follows :
// Add custom split lines
DividerItemDecoration divider = new DividerItemDecoration(this,DividerItemDecoration.VERTICAL);
divider.setDrawable(ContextCompat.getDrawable(this,R.drawable.custom_divider));
recyclerView.addItemDecoration(divider);After running , You can see a colorful dividing line :

In this way, the scope of use is much wider , such as item A certain interval is required between , Then we can add a transparent dividing line .
5、 summary
The code to add a divider is simple , You can do this when you need to add a simple dividing line in the future . Note, however, that this only applies to list layouts (LinearLayout) Of RecyclerView, Grid layout (GridLayout) And streaming layout (StaggeredGridLayout) It doesn't apply .
边栏推荐
- The normal one inch is 25.4 cm, and the image field is 16 cm
- Hard goods | write all the codes as soon as you change the test steps? Why not try yaml to realize data-driven?
- Imread change image display size
- . Net core - a queuing system for wechat official account
- File upload and download test point
- “测试人”,有哪些厉害之处?
- Snownlp emotion analysis
- 独家分析 | 关于简历和面试的真 相
- QT:QSS自定义 QTreeView实例
- 做软件测试三年,薪资不到20K,今天,我提出了辞职…
猜你喜欢

What is the salary level of 17k? Let's take a look at the whole interview process of post-95 Test Engineers

The testing department of the company came to the king of the Post-00 roll, and the veteran exclaimed that it was really dry, but

MAUI Developer Day in GCR

What are the strengths of "testers"?

QT:QSS自定义QTableView实例

Stack, monotone stack, queue, monotone queue

Is it OK to test the zero basis software?

软件测试工程师的5年之痒,讲述两年突破瓶颈经验

My understanding of testing (summarized by senior testers)

Crawl with requests
随机推荐
First line of code kotlin notes
Crawl with requests
MAUI Developer Day in GCR
8年测试总监的行业思考,看完后测试思维认知更深刻
栈,单调栈,队列,单调队列
Qt:qss custom qstatusbar instance
QT:QSS自定义 QStatusBar实例
sqlmap基本使用方法
Qt:qss custom qmenu instance
Qt:qss custom qlistview instance
文件上传下载测试点
The highest monthly salary of 18K has a good "mentality and choice", and success is poor "seriousness and persistence"~
线性表顺序表综合应用题P18
软件测试工程师的5年之痒,讲述两年突破瓶颈经验
Software testing e-commerce projects that can be written into your resume, don't you come in and get it?
11. Provider service registration of Nacos service registration source code analysis
Lecture 1 number field
年中了,准备了少量的自动化面试题,欢迎来自测
现在零基础转行软件测试还OK吗?
Software testing (test case) writing: vulgar, native and skillful