当前位置:网站首页>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 .
边栏推荐
- STM32F1与STM32CubeIDE编程实例-TM1637驱动4位7段数码管
- Probability theory: application of convolution in calculating moving average
- QT:QSS自定义QToolBar和QToolBox实例
- Qt:qss custom qgroupbox instance
- Test what the leader should do
- snownlp情感分析
- 月薪过万的测试员,是一种什么样的生活状态?
- I have been doing software testing for three years, and my salary is less than 20K. Today, I put forward my resignation
- 搭建ADG后,实例2无法启动 ORA-29760: instance_number parameter not specified
- Software testing (test case) writing: vulgar, native and skillful
猜你喜欢

8年测试工程师总结出来的《测试核心价值》与《0基础转行软件测试超全学习指南》

11. Provider service registration of Nacos service registration source code analysis

多路IO转接——前导

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

redis那些事儿

Cache routing component

那些一门心思研究自动化测试的人,后来怎样了?

你真的需要自动化测试吗?

Is it OK to test the zero basis software?

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
随机推荐
Typescript learning summary
Software testing redis database
软件测试——Redis数据库
线性表顺序表综合应用题P18
QT: QSS custom qtreeview instance
Internet Socket (非)阻塞write/read n个字节
Basic theoretical knowledge of software testing -- app testing
Summary of the history of Mathematics
Qt:qss custom QSlider instance
Cause: org. apache. ibatis. builder. Builderexception: error parsing SQL mapper configuration problem analysis
Win10系统下提示“系统组策略禁止安装此设备”的解决方案(家庭版无组策略)
[proteus simulation] 16 channel water lamp composed of 74hc154 four wire to 12 wire decoder
Overview of testing theory
测试Leader应该做哪些事
T5 的尝试
ByteDance layoffs, test engineers were almost destroyed: how terrible is the routine behind the recruitment of large factories?
QT:QSS自定义QMenu实例
UI自动化测试如何走出困境?价值又如何体现?
软件测试工程师的5年之痒,讲述两年突破瓶颈经验
EPS电动转向系统分析