我想在加载 recyclerview 后滚动到项目 id ==5,我直接在 setTargetPosition
中设置项目 id。如果有人对此有更好的线索,请帮助我。根据响应我想在加载 recyclerview 后直接滚动到三明治类别....
这是我的 setAdapter 代码;
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();
}
下面是我的代码;
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);
以下是api响应;
{
"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
}
]
}
}
最佳答案
试试这段代码
recyclerView.smoothScrollToPosition(position);
关于android - 滚动到 recyclerview 中的特定类别位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48093141/