1

I want to implement Item counter on RecyclerView in android. 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. Thanku

Item Counter's image

here you can see that the counter text is changed when scrolled

pb007
  • 153
  • 1
  • 5
  • 13
  • What do you mean by item counter ? – Kuls Jan 02 '18 at 07:01
  • you can get your row counter through the size of list you are passing to your adapter or in your case the number of items that are visible at a specific time. – Umair Jan 02 '18 at 07:02
  • I mean which counts the item in RecyclerView. I don't know what it is actual called so please see the above image – pb007 Jan 02 '18 at 07:03
  • do you want to show corresponding item counter on each item?(like 1..2..3..) or you want the total item count? – Sabid Habib Jan 02 '18 at 07:11
  • Yes corresponding item counter. @SabidBinHabib – pb007 Jan 02 '18 at 07:21
  • @user6734679 then you can use the solution I have posted as answer if you are using array adapter – Sabid Habib Jan 02 '18 at 07:22
  • But I don't want to show that TextView for every item. TextView will show only one fixed position and if I will scroll the Item then It will show and increment on it – pb007 Jan 04 '18 at 05:23

3 Answers3

2

if you are using Array Adapter, you can set your counter text with item position overriding getView method in your adapter:

@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:

@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));
    }
Sabid Habib
  • 419
  • 1
  • 4
  • 16
  • No I my not using Array Adapter – pb007 Jan 02 '18 at 07:21
  • I am using RecyclerView – pb007 Jan 02 '18 at 07:22
  • what kind of adapter are you using? recyclerview adapter? @user6734679 – Sabid Habib Jan 02 '18 at 07:23
  • recyclerview adapter – pb007 Jan 02 '18 at 07:25
  • check the edited answer for recycler view adapter, it should work for you @user6734679 – Sabid Habib Jan 02 '18 at 07:35
  • I also need that when I touch the screen than this text will show otherwise it will not show – pb007 Jan 02 '18 at 08:25
  • it's a different problem than the post so I can't post the solution as an answer. but the idea is to listen to your list item click listener in the activity you are initializing the recyclerview and from that listener set a status flag to your adapter item. from the status of the item, determine whether to keep your counter text view visible/invisible – Sabid Habib Jan 02 '18 at 08:29
  • The text will show only for single item if we scroll the List then text will increment – pb007 Jan 02 '18 at 10:56
  • I don't want to show that TextView for every item. TextView will show only one fixed position and if I will scroll the Item then It will show and increment on it – pb007 Jan 04 '18 at 05:23
0

findFirstVisibleItemPosition() will return visible item position while scroll top/bottom.

LinearLayoutManager myLayoutManager = myRecyclerView.getLayoutManager();
int scrollPosition = myLayoutManager.findFirstVisibleItemPosition();

for more about read this.

Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49
  • LinearLayoutManager myLayoutManager = myRecyclerView.getLayoutManager(); shows Incompatible types – pb007 Jan 02 '18 at 07:24
  • I don't want to show that TextView for every item. TextView will show only one fixed position and if I will scroll the Item then It will show and increment on it – pb007 Jan 04 '18 at 05:23
0

Create BroadcastReceiver in your activity/fragment.

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

LocalBroadcastManager.getInstance(context).registerReceiver(counterChangeReceiver , new IntentFilter("COUNTER_CHANGE"));

Send broadcast from onBindViewHolder() something like this

 Intent intent = new Intent("COUNTER_CHANGE");
 intent.putExtra("value",(position+1));
 LocalBroadcastManager.getInstance(MyApplication.getInstance().getBaseContext()).sendBroadcast(intent);
  • But I don't want to show that TextView for every item. TextView will show only one fixed position and if I will scroll the Item then It will show and increment on it. – pb007 Jan 04 '18 at 05:23
  • In that case, keep the text view in your activity. And change that value from onBindViewHolder() by using broadcast listeners. – Geethakrishna Juluri Jan 04 '18 at 05:32
  • Please check my updated question where I put one more image where the counter TextView is 40. and also refer me that code where I can read this type of article thanku – pb007 Jan 04 '18 at 05:39