[英]How to implement Item counter on RecyclerVIew?
I want to implement Item counter on RecyclerView in android. 我想在Android的RecyclerView上实现项目计数器。 If we scroll the list so item counter will increase like below Image.
如果我们滚动列表,则项目计数器将增加,如下图所示。 I am new in android so Please suggest me what is it called and where can we get it to read it.
我是android的新手,所以请向我建议它的名字,以及在哪里可以阅读它。 Thanku
感谢你
if you are using Array Adapter, you can set your counter text with item position overriding getView method in your adapter: 如果使用的是数组适配器,则可以在适配器中设置计数器文本,使其具有覆盖getView方法的项目位置:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//necessary code for inflating list items
//as index position starts from 0, set position+1 in textview
tvCounter.setText(Integer.parseInt(position+1));
return convertView;
}
if you are using RecyclerView Adapter , you can set your counter text with item position overriding onBindViewHolder method in your adapter: 如果您使用的是RecyclerView Adapter ,则可以使用适配器中的项目位置覆盖onBindViewHolder方法设置计数器文本:
@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
//other parts
//as index position starts from 0, set position+1 in textview
holder.tvCounter.setText(Integer.toString(position+1));
}
findFirstVisibleItemPosition()
will return visible item position while scroll top/bottom. 当滚动顶部/底部时,
findFirstVisibleItemPosition()
将返回可见项目的位置。
LinearLayoutManager myLayoutManager = myRecyclerView.getLayoutManager();
int scrollPosition = myLayoutManager.findFirstVisibleItemPosition();
for more about read this. 有关更多信息,请阅读此内容。
Create BroadcastReceiver in your activity/fragment. 在您的活动/片段中创建BroadcastReceiver。
private BroadcastReceiver counterChangeReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//set text by getting value from intent extras
}
};
and register it in activity/fragment onResume() like this 并像这样在活动/片段onResume()中注册它
LocalBroadcastManager.getInstance(context).registerReceiver(counterChangeReceiver , new IntentFilter("COUNTER_CHANGE"));
Send broadcast from onBindViewHolder() something like this 从onBindViewHolder()发送广播,类似这样
Intent intent = new Intent("COUNTER_CHANGE");
intent.putExtra("value",(position+1));
LocalBroadcastManager.getInstance(MyApplication.getInstance().getBaseContext()).sendBroadcast(intent);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.