Skip to content

Instantly share code, notes, and snippets.

@wightwulf1944
Last active September 29, 2017 09:22
Show Gist options
  • Save wightwulf1944/b1b1b15ca0df6a205db07e427581b6ba to your computer and use it in GitHub Desktop.
Save wightwulf1944/b1b1b15ca0df6a205db07e427581b6ba to your computer and use it in GitHub Desktop.
scrollToPosition(int) doesnt work?
public class MainActivity extends AppCompatActivity {
private int currentIndex;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
currentIndex = savedInstanceState.getInt("currentIndex");
}
@Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
public void setCurrentIndex(int i) {
currentIndex = i;
}
public int getCurrentIndex() {
return currentIndex;
}
}
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;
public class MasterFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
MainActivity parentActivity = (MainActivity) getActivity();
int currentIndex = parentActivity.getCurrentIndex();
FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(parentActivity);
layoutManager.setFlexWrap(FlexWrap.WRAP);
layoutManager.setJustifyContent(JustifyContent.SPACE_AROUND);
MasterAdapter adapter = new MasterAdapter();
adapter.setData(//);
RecyclerView recyclerView = findById(view, R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
recyclerView.scrollToPosition(currentIndex); //POINT OF INTEREST
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment