我已使用RecyclerViewAdapter
LinearLayoutManager
class WaterSummaryListAdapter(private var dataList: List<WaterSummaryListItem>): RecyclerView.Adapter<RecyclerView.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val v = LayoutInflater.from(parent.context)
.inflate(R.layout.list_row_water_summary,
parent,
false)
return ItemViewHolder(v)
}
override fun getItemCount(): Int = dataList.size
override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {
val dataItem = dataList[position]
if (holder is ItemViewHolder){
holder.update(dataItem)
}
}
inner class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private var date = itemView.itemDateSummaryList
private var value = itemView.waterValue
fun update(item: WaterSummaryListItem){
this.date.text = item.date
this.value.text = item.volume.toString()
}
}
fun changeValueOf(itemNo: Int, value: Float){
dataList[itemNo].volume = value
notifyDataSetChanged()
}
}
但是在滚动方面,它不能平滑滚动。它突然停了下来。我甚至试过.setHasFixedSize(true)
,但即使这样也行不通。
recyclerView = view.summaryListRecyclerView
val manager = LinearLayoutManager(context)
adapter = WaterSummaryListAdapter(mSummaryListItems)
recyclerView.layoutManager = manager
recyclerView.adapter = adapter
recyclerView.setHasFixedSize(true)
以上是我如何将适配器实现到回收站视图。
答案 0 :(得分:1)
您需要按以下方式设置布局
recyclerView.layoutManager = LinearLayoutManager(activity, LinearLayout.VERTICAL, false)
recyclerView.adapter = MyAdapter(yourList)
答案 1 :(得分:0)
在holder.update(dataItem)
方法正文
答案 2 :(得分:0)
删除onBindViewHolder中的if条件并直接调用update方法
if(holder是ItemViewHolder){}
holder.update(dataItem)
答案 3 :(得分:0)
您可以使用SpeedyLinearLayoutManager
SpeedyLinearLayoutManager linearLayoutManager = new SpeedyLinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
Adapter myAdapter = new Adapter(myContext);
myRecyclerView.setLayoutManager(linearLayoutManager);
myRecyclerView.setAdapter(myAdapter);
public class SpeedyLinearLayoutManager extends LinearLayoutManager {
private static final float MILLISECONDS_PER_INCH = 6f; //default is 25f
// set the speed here
public SpeedyLinearLayoutManager(Context context) {
super(context);
}
public SpeedyLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
public SpeedyLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public boolean canScrollVertically() {
//Similarly you can customize "canScrollHorizontally()" for managing horizontal scroll
return CommonUtils.IsContactActivityScrollEnabled && super.canScrollVertically();
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
final LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {
@Override
public PointF computeScrollVectorForPosition(int targetPosition) {
return SpeedyLinearLayoutManager.this.computeScrollVectorForPosition(targetPosition);
}
@Override
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
}
};
linearSmoothScroller.setTargetPosition(position);
startSmoothScroll(linearSmoothScroller);
}
}
答案 4 :(得分:0)
首先,您需要更改第一行,
class WaterSummaryListAdapter(private var dataList: List<WaterSummaryListItem>): RecyclerView.Adapter<RecyclerView.ViewHolder>()
与
class WaterSummaryListAdapter(private var dataList: List<WaterSummaryListItem>): RecyclerView.Adapter< WaterSummaryListAdapter.ItemViewHolder>()
更新:删除了不必要的建议,感谢@Viktor Yakunin