我有两个回收站视图,一个水平,一个垂直 在我的卧式回收机视图中,有类别,而在垂直式中,有物品清单。因此,当我单击任何类别时,我需要这样做,我的垂直回收器视图会滚动到该特定项目 将屏幕快照附加在链接中-https://ibb.co/n67WdRn https://ibb.co/P5CrjnH
我尝试使用意图,它可以工作,但是会启动一个新活动 请帮助
Mainactivity.java
package com.example.myapplication;
public class MainActivity extends AppCompatActivity {
DatabaseReference reference;
ArrayList<dish> list;
private static final String TAG = "MainActivity";
private ArrayList<String> mCategory = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getCategory();
menu();
}
private void getCategory(){
Log.d(TAG, "Preparing Categories");
mCategory.add("Vagetables");
mCategory.add("Indian Bread");
mCategory.add("Paranthas");
mCategory.add("Starters");
mCategory.add("Snacks");
mCategory.add("Salad");
mCategory.add("Noodles");
mCategory.add("Sandwiches");
mCategory.add("Rice");
mCategory.add("Soups");
mCategory.add("Beverages");
mCategory.add("Yoghurt");
mCategory.add("Papad");
initRecyclerView();
}
private void initRecyclerView(){
Log.d(TAG, "init RecyclerView");
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(layoutManager);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(this, mCategory);
recyclerView.setAdapter(adapter);
}
private void menu(){
list = new ArrayList<dish>();
Log.d(TAG, "recycler MenuView : Firebase Initialized");
FirebaseApp.initializeApp(this);
reference = FirebaseDatabase.getInstance().getReference().child("dishes");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Log.d(TAG, "onDataChange : called");
for (DataSnapshot dataSnapshot1: dataSnapshot.getChildren())
{
Log.d(TAG,"Adding Menu");
dish p = dataSnapshot1.getValue(dish.class);
Log.d(TAG, "called : " + p);
Log.d(TAG, " : " + dataSnapshot + " : " + dataSnapshot1);
list.add(p);
}
getmenu();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(MainActivity.this, " Opps....Somethinf Went wrong!", Toast.LENGTH_LONG).show();
}
});
}
private void getmenu(){
Log.d(TAG,"Setting adapter");
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
RecyclerView recyclerMenuView = findViewById(R.id.recyclerMenuView);
recyclerMenuView.setLayoutManager(linearLayoutManager);
MenuAdapter adapter = new MenuAdapter(MainActivity.this, list);
recyclerMenuView.setAdapter(adapter);
}
}
RecyclerViewAdapter.java-类别recyclerview(单击此项目)
package com.example.myapplication;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
@NonNull
private static final String TAG = "RecyclerViewAdapter";
private ArrayList<String> mCategory = new ArrayList<>();
private Context context;
public RecyclerViewAdapter(Context context, ArrayList<String> category) {
this.context = context;
mCategory = category;
}
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Log.d(TAG ,"On Create View Holder: called");
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
Log.d(TAG, "OnBindViewHolder: called");
holder.category.setText(mCategory.get(position));
holder.category.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG,"onClick : Clicked on Text" + mCategory.get(position));
Toast.makeText(context, mCategory.get(position), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return mCategory.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder{
TextView category;
public ViewHolder(@NonNull View itemView) {
super(itemView);
category = itemView.findViewById(R.id.category);
}
}
}
MenuAdapter.java-在此回收站视图中滚动项目
package com.example.myapplication;
public class MenuAdapter extends RecyclerView.Adapter<MenuAdapter.menuViewHolder>{
Context context;
ArrayList<dish> dishes;
public MenuAdapter(Context c , ArrayList<dish> p)
{
context = c;
dishes = p;
}
@NonNull
@Override
public menuViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Log.d(TAG ,"On Create View Holder: called");
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview, parent, false);
return new MenuAdapter.menuViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull menuViewHolder holder, final int position) {
holder.dishname.setText(dishes.get(position).getDishname());
holder.dishrate.setText(dishes.get(position).getDishrate());
Picasso.get().load(dishes.get(position).getDishImage()).placeholder(R.drawable.ic_launcher_background).into(holder.dishImage);
boolean flag = dishes.get(position).getPermission();
if (flag){
Log.d(TAG," recommended : " + dishes.get(position).getDishname());
holder.recommended.setVisibility(View.VISIBLE);
}
else{
holder.recommended.setVisibility(View.INVISIBLE);
}
}
@Override
public int getItemCount() {
return dishes.size();
}
class menuViewHolder extends RecyclerView.ViewHolder {
TextView dishname, dishrate, recommended;
ImageView dishImage;
public menuViewHolder(@NonNull View itemView)
{
super(itemView);
dishname = (TextView) itemView.findViewById(R.id.dishname);
dishrate = (TextView) itemView.findViewById(R.id.dishrate);
dishImage = (ImageView) itemView.findViewById(R.id.dishImage);
recommended = (TextView) itemView.findViewById(R.id.recommended);
}
}
}
答案 0 :(得分:0)
尝试使用RecyclerView的scrollTo(position)
或smoothScrollTo(position)