正常列表展示:
我们要限制显示的item数量效果:
两个比较不难发现虽然都是能滑动,但是展示在当前页面的列表数量不一样。
最近玩蓝牙有这个需求我才知道这玩意还能限制条目。。。
就是最多展示5,多余的点击跳转展示更多。。。死数据研究中
好了废话不多说,回归正题
感谢大佬:https://blog.csdn.net/unknown_315/article/details/88297132
以上网址亲测了下可以实现的,效果上面。
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Adapter item XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:gravity="center"
android:padding="6dp"
android:text="item"
android:textSize="18sp" />
</RelativeLayout>
MainActivity
package com.example.set_recyclerview_item_number;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecycler;
private ArrayList<String> items = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i = 0; i <30 ; i++) {
items.add("条目");
}
initView();
}
private void initView() {
mRecycler = (RecyclerView) findViewById(R.id.recycler);
mRecycler.setLayoutManager(new LinearLayoutManager(this));
mRecycler.setAdapter(new ItemNumberAdapter(items));
}
}
Adapter
package com.example.set_recyclerview_item_number;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
/**
* @auther eerdunsang
* @date 2020/10/29
* @time 8:26.
*/
public class ItemNumberAdapter extends RecyclerView.Adapter<ItemNumberAdapter.ItemHolder> {
private ArrayList<String> items;
public ItemNumberAdapter(ArrayList<String> items) {
this.items = items;
}
@NonNull
@Override
public ItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout, parent, false);
//不允许用这个获取view!!
// View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout, null);
ItemHolder itemHolder = new ItemHolder(view);
int parentHeight = parent.getHeight();
parent.getWidth();
ViewGroup.LayoutParams params = itemHolder.itemView.getLayoutParams();
params.height = (parentHeight/5); //需要展示几个item 就写几个
return itemHolder;
}
@Override
public void onBindViewHolder(@NonNull ItemHolder holder, int position) {
holder.tv.setText("当前"+items.get(position)+":"+position);
if (position%2!=0){//奇数
holder.tv.setBackgroundResource(R.color.colorAccent);
}else {//偶数
holder.tv.setBackgroundResource(R.color.colorPrimaryDark);
}
}
@Override
public int getItemCount() {
return items.size();
}
public class ItemHolder extends RecyclerView.ViewHolder {
private TextView tv;
public ItemHolder(@NonNull View itemView) {
super(itemView);
tv = itemView.findViewById(R.id.tv);
}
}
}
注意!
- xml 根布局高宽度必须match_parent,不然不显示
- 以上列表用在竖向,横向估计同理,日后更新
- 限制仅仅是限制在当前页面展示的数量,而不是列表数量
- params.height = (parentHeight/5); 需要显示几个填写几个
- 适配器onCreateViewHolder中获取view 必须用parent样式获取!代码有注释。