0

Hello I am working on demo application where I am using listview and custom adapter. I want to do that when I scroll the list when you come at position to 2 or 3 then that row should be on top of the screen means previous row should be hide completely.

Example I am on first row first time and started to scroll listview then comes to second when getview call with position 2 then list should only show of row 2 on the screen. How I can achieve this ? Please assist.

See screenshot when I scroll then both rows appear when I even move to row 2 then first row also appears until I move up list manually.

enter image description here

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView == null) {
        LayoutInflater mInflator = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = mInflator.inflate(R.layout.home_list_item, parent, false);
    }

    TextView mChapterContent = (TextView) convertView.findViewById(R.id.contentTextView);

    // get the data from verse instance
    Verse verse = rowItem.get(position);

    // setting the content
    mChapterContent.setText(Html.fromHtml("<html><body style=\"text-align:justify\">" + verse.getText() + "</body></html>"));



    return convertView;
}

Thanks in advance.

N Sharma
  • 33,489
  • 95
  • 256
  • 444

2 Answers2

2

Judging from the state, it looks to me as a good use-case of Vertical View pager.

Checkout these likes:

https://github.com/JakeWharton/Android-DirectionalViewPager/

Android: Vertical ViewPager

Community
  • 1
  • 1
vipul mittal
  • 17,343
  • 3
  • 41
  • 44
0

You can use the ListView's setSelection feature to scroll to a particular item in the list as follows...

listView.setSelection(2)

Or if you want it animated...

listView.smoothScrollToPosition(2)

This answer is referenced from this answer.

Community
  • 1
  • 1
Brian
  • 7,955
  • 16
  • 66
  • 107
  • Hmm I might be a bit confused about what you're trying to achieve. Do you simply want to scroll to a particular item in your `ListView`? Or do something more complicated? – Brian Jul 21 '14 at 17:11
  • I tried this but this is not scrolling my list to top of the screen when I come at row 2 mListView.setSelection(position); – N Sharma Jul 21 '14 at 17:26
  • Ah I see, so you want it to "snap" to the second row when scroll do it? If that's the case, you probably do want to consider vipul mittal's answer then. – Brian Jul 21 '14 at 18:03