当前位置:网站首页>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 .
边栏推荐
- 《通信软件开发与应用》
- 那些一门心思研究自动化测试的人,后来怎样了?
- Is pinduogai's sales safe in 2022?
- QT:QSS自定义 QTreeView实例
- QT: QSS custom qtabwidget and qtabbar instances
- Software testing redis database
- 公司测试部门来了个00后卷王之王,老油条感叹真干不过,但是...
- 值得关注的15种软件测试趋势
- After 8 years of industry thinking, the test director has a deeper understanding of test thinking
- Some abilities can't be learned from work. Look at this article, more than 90% of peers
猜你喜欢
I have been doing software testing for three years, and my salary is less than 20K. Today, I put forward my resignation
Is it OK to test the zero basis software?
How did I grow up in the past eight years as a test engineer of meituan? I hope technicians can gain something after reading it
Hard goods | write all the codes as soon as you change the test steps? Why not try yaml to realize data-driven?
做软件测试三年,薪资不到20K,今天,我提出了辞职…
QT:QSS自定义QTableView实例
Cache routing component
What happened to those who focused on automated testing?
8年测试总监的行业思考,看完后测试思维认知更深刻
10. Nacos source code construction
随机推荐
Hard goods | write all the codes as soon as you change the test steps? Why not try yaml to realize data-driven?
Qt:qss custom qspinbox instance
《通信软件开发与应用》
QT:QSS自定义 QProgressBar实例
QT:QSS自定义QTableView实例
What experience is there only one test in the company? Listen to what they say
2021 postgraduate entrance examination mathematics 2 linear algebra
EPS电动转向系统分析
Crawl with requests
独家分析 | 关于简历和面试的真 相
软件测试(测试用例)编写之俗手、本手、妙手
Qt:qss custom qmenu instance
Cache routing component
Stack, monotone stack, queue, monotone queue
. Net core - a queuing system for wechat official account
Multiple IO transfer - preamble
Inexplicable problems in the nesting of constraintlayout and relativelayout
现在零基础转行软件测试还OK吗?
Exclusive analysis | truth about resume and interview
Game test related tests a hero's skills (spring moves are asked more questions)