单击另一个RecyclerView的项目时更新RecyclerView的内容

时间:2018-07-22 16:06:17

标签: android android-recyclerview

所以我正在尝试在酒精和调音台上创建一个android应用,但是一段时间以来一直困扰于以下问题...

我想在水平滚动的RecyclerView中显示每个酒精类别(例如:杜松子酒,伏特加,威士忌等),在滚动的RecyclerView中显示每个酒精类型(例如,威士忌类别的波旁威士忌和苏格兰威士忌)垂直。

我为每个RecyclerView创建了一个适配器(对于名为 category 的水平RecyclerView创建了 CategoryAdapter ,对于名为的垂直RecyclerView创建了 MixerAdapter > categoryDe​​tails )。

到目前为止,我已经设法根据需要创建和显示 category ,但是在 categoryDe​​tails 上存在一些困难。

基本上,当选择 category 项目时,我不知道如何更新 categoryDe​​tails 的内容:
例如
如果用户在类别中选择威士忌,我希望 categoryDe​​tails 显示波旁威士忌苏格兰威士忌
如果用户然后选择杜松子酒,我希望 categoryDe​​tails 仅显示杜松子酒风味杜松子酒,等等...

我希望我对要完成的任务已经足够清楚了!
任何帮助将不胜感激,谢谢!!


这是加载活动时屏幕显示方式的屏幕截图。
如果用户选择朗姆酒(黑色RecyclerView上为白色),则我希望RecyclerView当前显示 Gin Flavored Gin (黑色白色RecyclerView)以显示与朗姆酒类别相关的酒精。 Screenshot


这是包含两个recyclerViews category categoryDe​​tails

的XML文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TipsDrinks">

<android.support.v7.widget.RecyclerView
    android:id="@+id/category"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:background="@android:color/black"
    android:orientation="horizontal"
    android:scrollbars="horizontal"
    android:visibility="visible"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/SpinnerPrompt" >

</android.support.v7.widget.RecyclerView>

<android.support.v7.widget.RecyclerView
    android:id="@+id/categoryDetails"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="8dp"
    android:background="@android:color/white"
    android:orientation="horizontal"
    android:scrollbars="vertical"
    android:visibility="visible"
    app:layout_constraintBottom_toTopOf="@+id/adView4"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/category"
    app:layout_constraintVertical_bias="0.0">

</android.support.v7.widget.RecyclerView>

</android.support.constraint.ConstraintLayout>

与以前的布局关联的类

public class TipsDrinks extends AppCompatActivity {

    private CategoryAdapter categoryAdapter; // Adapter used for the category RecyclerView
    public MixerAdapter mixerAdapter; //Adapter used for the categoryDetails RecyclerView

    private RecyclerView categories; // The RecyclerView holding the name of each alcohol category
    public RecyclerView catDetails; // The RecyclerView holding each type of that alcohol category

    private  DrinkMenu drinkMenu; // The DrinkMenu is another Class holding every Alcohol Category, the type of each alcohol and the mixers good with it
    private ArrayList<String> drinkCat = new ArrayList<>(); // A String ArrayList holding the name of each alcohol category (Gin, Vodka, Rum, Whiskey, Other)
    private ArrayList<Drink> drinkMixers = new ArrayList<>(); // A Drink(String, ArrayList<String>) ArrayList holding the mixers of every Drink

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tips_drinks);

    categories = findViewById(R.id.category);
    catDetails = findViewById(R.id.categoryDetails);

    drinkMenu = new DrinkMenu();

    setCategoryView(); // Creates the category RecyclerView

    setDrinkMixers(); // Creates the categoryDetails RecyclerView
  }

  private void setCategoryView(){

    categoryAdapter = new CategoryAdapter(drinkCat);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(TipsDrinks.this, LinearLayoutManager.HORIZONTAL, false);

    categories.addItemDecoration(new DividerItemDecoration(TipsDrinks.this, DividerItemDecoration.HORIZONTAL));
    categories.setLayoutManager(layoutManager);
    categories.setItemAnimator(new DefaultItemAnimator());
    categories.setAdapter(categoryAdapter);

    prepareCategories();
  }

  private void prepareCategories(){
    drinkCat.clear();
    drinkCat.addAll(drinkMenu.getDrinkCategories());
    for (String drink : drinkCat) {
        System.out.println(drink);
    }
    categoryAdapter.notifyItemInserted(drinkCat.size() - 1);
  }

  private void setDrinkMixers() {
    mixerAdapter = new MixerAdapter(drinkMixers);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());

    catDetails.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
    catDetails.setLayoutManager(layoutManager);
    catDetails.setItemAnimator(new DefaultItemAnimator());
    catDetails.setAdapter(mixerAdapter);

    prepareMixers();
  }

  private void prepareMixers() {
    drinkMixers.clear();
    drinkMixers.addAll(drinkMenu.getDrinkMixers(categoryAdapter.getCurrentCategory()));
    mixerAdapter.notifyItemChanged(drinkMixers.size() -1);
  }
}

类别 RecyclerView的 CategoryAdapter

public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.MyViewHolder> {

    private List<String> drinkList; //A List of String holding the category for each drink
    private String textName = "Gin";

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

    private TextView name; // The TextView holding the name of the category 

    MyViewHolder(View view) {
        super(view);
        view.setOnClickListener(this);
        name = view.findViewById(R.id.catName);
    }

    @Override
    public void onClick(View v) {
        //Code to update the contents of the categoryDetails RecyclerView
    }
  }

  CategoryAdapter(ArrayList<String> drinkList) { this.drinkList = drinkList; }

  @Override
  public CategoryAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.category_text, parent, false);

        return new MyViewHolder(itemView);
  }

  @SuppressLint("SetTextI18n")
  @Override
  public void onBindViewHolder(CategoryAdapter.MyViewHolder holder, int position) {
    String drink = drinkList.get(position);
    holder.name.setText(drink);
    textName = holder.name.getText().toString();
  }

  @Override
  public int getItemCount() {
    return drinkList.size();
  }

  public String getCurrentCategory() {
    return textName;
  }
}

categoryDe​​tails RecyclerView

MixerAdapter
public class MixerAdapter extends RecyclerView.Adapter<MixerAdapter.MyViewHolder>{

    private List<Drink> mixerList;

  class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    TextView mixerCat, mixers;


    MyViewHolder(View view) {
        super(view);
        view.setOnClickListener(this);
        mixerCat = view.findViewById(R.id.mixerCat2);
        mixers = view.findViewById(R.id.mixers2);
    }

    @Override
    public void onClick(View v) {
        if (mixers.getVisibility() == mixerCat.getVisibility()) {
            mixers.setVisibility(View.GONE);
        }
        else {
            mixers.setVisibility(View.VISIBLE);
        }
    }
  }

  MixerAdapter(ArrayList<Drink> mixerList) {
    this.mixerList = mixerList;
  }

  @Override
  public MixerAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.drinks_row, parent, false);

    return new MixerAdapter.MyViewHolder(itemView);
  }

  @SuppressLint("SetTextI18n")
  @Override
  public void onBindViewHolder(MixerAdapter.MyViewHolder holder, int position) {
    Drink drink = mixerList.get(position);
    StringBuilder mixerStringList = new StringBuilder("");
    holder.mixerCat.setText(drink.getSorM());
    for (String mixer: drink.getMixers()) {
        mixerStringList.append(mixer).append("\n");
    }
    holder.mixers.setText(mixerStringList.toString().trim());
  }

  @Override
  public int getItemCount() {
    return mixerList.size();
  }
}

2 个答案:

答案 0 :(得分:0)

CategoryAdapter中创建一个回调接口。当用户单击Category RecycleView中的某个项目时,活动职责是根据选择为MixerAdapter填充新项目。

CategoryAdapter

public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.MyViewHolder> {

private CategoryInterface callback;

 CategoryAdapter(ArrayList<String> drinkList, CategoryInterface listener) {
this.drinkList = drinkList;
callback = listener;
}

@Override
    public void onClick(View v) {
        //Code to update the contents of the categoryDetails RecyclerView
        callback.onItemSelected(getAdapterPosition());
    }

public interface CategoryInterface {
void onItemSelected(int position);
}
}

活动

public class TipsDrinks extends AppCompatActivity implements CategoryInterface {
 private void setCategoryView(){

    categoryAdapter = new CategoryAdapter(drinkCat, this);
}

@Override
void onItemSelected(int position) {
//Reassign items in MixerAdapter
}
}

答案 1 :(得分:0)

取决于您的实现,一种适用于所有实现的全局方法是使用HolderView方法,并在组的根视图上使用onTouchListener

  v.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if(event.getAction() == MotionEvent.ACTION_UP){
                        //the child should expand and be updated
                    }
                    return false;
                }
            });