Vis*_*nav 5 android recycler-adapter android-recyclerview
我想在加载recyclerview之后滚动到项目ID == 5,我直接设置项目ID setTargetPosition....如果有任何人有更好的线索,请帮助我.根据响应我想在加载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();
}
Run Code Online (Sandbox Code Playgroud)
下面是我的代码;
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);
Run Code Online (Sandbox Code Playgroud)
以下是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
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
我通过以下方式完成我的工作;
拿LinearLayoutManager
LinearLayoutManager mLayoutManager = new LinearLayoutManager(RestaurantMenuActivity.this);
rvMenu.setLayoutManager(mLayoutManager);
Run Code Online (Sandbox Code Playgroud)
并滚动到如下代码所示的位置;
int scrollPosition = 0;
for (int j = 0; j < pos; j++) {
scrollPosition += menuItem.getData().getProductCategory().get(j).getProductDetails().size();
scrollPosition++;
}
mLayoutManager.scrollToPosition(scrollPosition);
final Handler handler = new Handler();
final int finalScrollPosition = scrollPosition;
handler.postDelayed(new Runnable() {
@Override
public void run() {
View rclItemView = rvMenu.getChildAt(0);
mLayoutManager.scrollToPositionWithOffset(finalScrollPosition, rclItemView.getBaseline());
}
}, 100);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1273 次 |
| 最近记录: |