Reputation: 103
I am struggling with this issue for a long time I am trying to replace my listview with recyclerview and my listview looks like this:-
<ListView
android:id="@+id/browser_sub_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:clipToPadding="false"
android:drawSelectorOnTop="true"
android:paddingTop="340dp"/>
Now when I call
view.getFirstVisiblePosition();
On this listview I returns the position of first item which is zero and as I have given top padding so it will keep returning only zero till the first item is out of screens bound.
But when I am trying to do the same thing with recyclerview even if the first item of recyclerview is visible still it gives me different values of firstvisible item and it happens because recyclerview considers toppadding as screen bound now I don't have any clue how to solve this problem. Please help.
Here is my code.
MainActivity.Java
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private TheAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
ArrayList<String> items = new ArrayList<>();
for (int i = 0; i < 300; i++) {
items.add("This is item:-" + i);
}
adapter = new TheAdapter(items);
mRecyclerView.setAdapter(adapter);
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
Log.d("DISTANCE", "" + linearLayoutManager.findFirstVisibleItemPosition());
}
});
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:clipToPadding="false"
android:paddingTop="340dp"
android:layout_height="match_parent"
/>
</RelativeLayout>
this is my adapter class
class TheAdapter extends RecyclerView.Adapter<TheAdapter.ItemHolder> {
public TheAdapter(ArrayList<String> data) {
mData = data;
}
private ArrayList<String> mData;
@Override
public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
return new ItemHolder(view);
}
@Override
public void onBindViewHolder(ItemHolder holder, int position) {
holder.mTextView.setText(mData.get(position));
}
@Override
public int getItemCount() {
return mData == null ? 0 : mData.size();
}
public class ItemHolder extends RecyclerView.ViewHolder {
private TextView mTextView;
public ItemHolder(View itemView) {
super(itemView);
mTextView = (TextView) itemView.findViewById(R.id.textTitle);
}
}
}
Upvotes: 4
Views: 2943
Reputation: 62831
Until you find a better answer, you can try the following code. int pos
is the position reported by findFirstVisibleItemPosition()
. This method will return the topmost visible position in the RecyclerView
.
public int findRealFirstVisibleItemPosition(int pos) {
View view;
final LinearLayoutManager linearLayoutManager =
(LinearLayoutManager) mRecyclerView.getLayoutManager();
while (pos > 0) {
view = linearLayoutManager.findViewByPosition(pos - 1);
if (view == null) {
break;
}
pos = pos - 1;
}
return pos;
}
I seem to recall that large padding causes these type of scrolling views problems.
Upvotes: 4