当前位置:网站首页>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 .
边栏推荐
- 测试Leader应该做哪些事
- Stack, monotone stack, queue, monotone queue
- Solutions of n-ary linear equations and their criteria
- 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
- Qt:qss custom qpprogressbar instance
- ExecutorException: Statement returned more than one row, where no more than one was expected.
- Imread change image display size
- What is the salary level of 17k? Let's take a look at the whole interview process of post-95 Test Engineers
- 17K薪资要什么水平?来看看95后测试工程师的面试全过程…
- Qt:qss custom QSlider instance
猜你喜欢
月薪过万的测试员,是一种什么样的生活状态?
Some abilities can't be learned from work. Look at this article, more than 90% of peers
Snownlp emotion analysis
那些一门心思研究自动化测试的人,后来怎样了?
QT:QSS自定义QTableView实例
Use ml.net+onnx pre training model to liven the classic "Huaqiang buys melons" in station B
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
redis那些事儿
MAUI Developer Day in GCR
QT: QSS custom qtableview instance
随机推荐
The five-year itch of software testing engineers tells the experience of breaking through bottlenecks for two years
Word line and bit line
The highest monthly salary of 18K has a good "mentality and choice", and success is poor "seriousness and persistence"~
嵌入式软件测试怎么实现自动化测试?
sqlmap基本使用方法
MAUI Developer Day in GCR
游戏测试相关 测试一个英雄的技能(春招被问比较多的一道题)
QT: QSS custom qtreeview instance
8年测试工程师总结出来的《测试核心价值》与《0基础转行软件测试超全学习指南》
QT: QSS custom qtoolbutton instance
Tencent micro app to get wechat user information
QT:QSS自定义 QProgressBar实例
硬 货 | 一改测试步骤代码就全写?为什么不试试用 Yaml实现数据驱动?
我对测试工作的一些认识(资深测试人员总结)
Clion debug
What are the strengths of "testers"?
反正切熵(Arctangent entropy):2022.7月最新SCI论文
2021 postgraduate entrance examination mathematics 2 linear algebra
Logstash backup tracks the data records reported
The normal one inch is 25.4 cm, and the image field is 16 cm