I want to scroll to element id == 5 after loading recyclerview, and I directly set the element id to setTargetPosition.... Please help me if someone better understands it. In response to the request, I want to directly scroll to the Sandwich category after loading recyclerview ....
this one is my setAdapter code;
try {
JSONArray jsonArray = arrayList.get(0);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jObj = jsonArray.getJSONObject(i);
String catName = jObj.getString("CategoryName");
String catId = jObj.getString("CategoryID");
Category cat1 = createCategory(catName, Integer.parseInt(catId));
JSONArray jProductDetails = jObj.getJSONArray("ProductDetails");
ArrayList<HashMap<String, String>> productdetaildata = new ArrayList<HashMap<String, String>>();
for (int j = 0; j < jProductDetails.length(); j++) {
JSONObject jP = jProductDetails.getJSONObject(j);
HashMap<String, String> map = new HashMap<String, String>();
map.put(HitUtilities.product_id, jP.getString("ProductID"));
map.put(HitUtilities.product_name, jP.getString("ProductName"));
map.put(HitUtilities.product_image, jP.getString("PhotoImagePath"));
map.put(HitUtilities.product_price, jP.getString("CurrentPrice"));
map.put(HitUtilities.product_isFavorite, jP.getString("Favorited"));
productdetaildata.add(map);
}
cat1.setItemList(createItems(productdetaildata, jProductDetails.length()));
catList.add(cat1);
}
restaurantMenuAdapter = new RestaurantMenuAdapter(catList);
rvMenu.setAdapter(restaurantMenuAdapter);
smoothScroller.setTargetPosition(getPositionWithName("Sandwich"));
mLayoutManager.startSmoothScroll(smoothScroller);
} catch (Exception e) {
e.printStackTrace();
}
below is my code;
smoothScroller = new LinearSmoothScroller(RestaurantMenuActivity.this) {
@Override
protected int getVerticalSnapPreference() {
return LinearSmoothScroller.SNAP_TO_ANY;
}
@Override
public PointF computeScrollVectorForPosition(int targetPosition) {
return null;
}
};
smoothScroller.setTargetPosition(catList.get(5).getId());
mLayoutManager.startSmoothScroll(smoothScroller);
Below is the api answer;
{
"Status":"Success",
"StatusCode":"200",
"Message":"data fetch successfully.",
"Data":{
"RestaurantID":"1",
"ProductCategory":[
{
"CategoryID":"1",
"CategoryName":"Restaurant Offers",
"No_of_Product":2
},
{
"CategoryID":"2",
"CategoryName":"Cold Drinks",
"No_of_Product":4
},
{
"CategoryID":"3",
"CategoryName":"Pizza",
"No_of_Product":2
},
{
"CategoryID":"4",
"CategoryName":"Burger",
"No_of_Product":1
},
{
"CategoryID":"5",
"CategoryName":"Sandwich",
"No_of_Product":2
},
{
"CategoryID":"6",
"CategoryName":"Chinese",
"No_of_Product":1
},
{
"CategoryID":"7",
"CategoryName":"Maxican",
"No_of_Product":1
}
]
}
}