0

Ich habe versucht, mehrere RecyclerView innerhalb eines Layouts zu implementieren, das Teil einer zusammenklappenden Registerkarte ist. Allerdings funktioniert meine RecyclerView nicht und ich weiß nicht, was ich in meinem Code falsch gemacht habe. Bitte hilf mir! HierRecyclerView wird nicht ordnungsgemäß angezeigt (scrunched up)

ist die Github-Link: github.com/arxbombus/RecipeDetails

Hier ist die gewünschte Ansicht: image

Hier ist, was ich aus irgendeinem Grund immer: image

Wie Sie kann sehen, alles ist alles zusammengeknüllt. :(

Unten habe ich die Layouts für meine MainActivity und auch die Java-Dateien für meine Adapter enthalten.

Hier ist meine activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
 
<android.support.design.widget.CoordinatorLayout 
 
    xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
 
    android:id="@+id/coordinatorLayout" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent"> 
 

 
    <android.support.design.widget.AppBarLayout 
 
     android:id="@+id/appBarLayout" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="400dp" 
 
     android:fitsSystemWindows="true" 
 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 
 

 
     <android.support.design.widget.CollapsingToolbarLayout 
 
      android:id="@+id/collapsingToolbarLayout" 
 
      android:layout_width="match_parent" 
 
      android:layout_height="400dp" 
 
      app:title="Some Randome Recipe" 
 
      app:titleEnabled="true" 
 
      app:contentScrim="?attr/colorPrimary" 
 
      app:layout_scrollFlags="scroll|exitUntilCollapsed"> 
 

 
      <ImageView 
 
       android:id="@+id/ivParallax" 
 
       android:layout_width="match_parent" 
 
       android:layout_height="match_parent" 
 
       android:scaleType="centerCrop" 
 
       android:src="@drawable/food" 
 
       app:layout_collapseMode="parallax" 
 
       app:layout_collapseParallaxMultiplier="0.7" /> 
 

 
      <android.support.v7.widget.Toolbar 
 
       android:id="@+id/toolbar" 
 
       android:layout_width="match_parent" 
 
       android:layout_height="?attr/actionBarSize" 
 
       app:layout_collapseMode="pin" 
 
       app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
 
       app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" /> 
 
     </android.support.design.widget.CollapsingToolbarLayout> 
 
    </android.support.design.widget.AppBarLayout> 
 

 
    <android.support.v4.widget.NestedScrollView 
 
     android:id="@+id/nestedScrollView" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="match_parent" 
 
     android:fillViewport="true" 
 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 
 

 
     <LinearLayout 
 
      android:layout_width="match_parent" 
 
      android:layout_height="wrap_content" 
 
      android:orientation="vertical"> 
 

 
      <TextView 
 
       android:layout_width="match_parent" 
 
       android:layout_height="wrap_content" 
 
       android:id="@+id/txtAbout" 
 
       android:text="About this recipe" 
 
       android:textStyle="bold" 
 
       android:textSize="13sp" 
 
       android:padding="15dp" 
 
       android:layout_marginBottom="-25dp"/> 
 

 
      <TextView 
 
       android:layout_width="match_parent" 
 
       android:layout_height="wrap_content" 
 
       android:id="@+id/txtRecipeDescription" 
 
       android:text="@string/recipe_description" 
 
       android:padding="15dp" 
 
       android:layout_marginBottom="-5dp" 
 
       android:textSize="12sp"/> 
 

 
      <View 
 
       android:layout_width="match_parent" 
 
       android:layout_height="1dp" 
 
       android:background="@color/colorDivider" 
 
       android:layout_marginLeft="10dp" 
 
       android:layout_marginRight="10dp" 
 
       /> 
 

 
      <TextView 
 
       android:layout_width="match_parent" 
 
       android:layout_height="wrap_content" 
 
       android:id="@+id/txtInfo" 
 
       android:text="Info" 
 
       android:textStyle="bold" 
 
       android:textSize="13sp" 
 
       android:padding="15dp" 
 
       android:layout_marginBottom="-20dp" 
 
       android:layout_marginTop="-5dp"/> 
 

 
      <android.support.v7.widget.RecyclerView 
 
       android:id="@+id/rvRecipeInfo" 
 
       android:layout_width="match_parent" 
 
       android:layout_height="75dp" 
 
       android:layout_gravity="center_vertical" 
 
       android:orientation="horizontal" 
 
       android:padding="15dp"></android.support.v7.widget.RecyclerView> 
 

 
      <View 
 
       android:layout_width="match_parent" 
 
       android:layout_height="1dp" 
 
       android:background="@color/colorDivider" 
 
       android:layout_marginLeft="10dp" 
 
       android:layout_marginRight="10dp" 
 
       /> 
 

 
      <TextView 
 
       android:layout_width="match_parent" 
 
       android:layout_height="wrap_content" 
 
       android:id="@+id/txtIngredient" 
 
       android:text="Ingredients" 
 
       android:textStyle="bold" 
 
       android:textSize="13sp" 
 
       android:padding="15dp" 
 
       android:layout_marginBottom="-20dp" 
 
       android:layout_marginTop="-5dp"/> 
 

 
      <android.support.v7.widget.RecyclerView 
 
       android:id="@+id/rvRecipeIngredient" 
 
       android:layout_width="match_parent" 
 
       android:layout_height="wrap_content" 
 
       android:orientation="vertical" 
 
       android:padding="15dp"></android.support.v7.widget.RecyclerView> 
 

 
      <Button 
 
       android:id="@+id/button" 
 
       android:layout_width="match_parent" 
 
       android:layout_height="wrap_content" 
 
       android:text="Button" /> 
 

 
      <View 
 
       android:layout_width="match_parent" 
 
       android:layout_height="1dp" 
 
       android:background="@color/colorDivider" 
 
       android:layout_marginLeft="10dp" 
 
       android:layout_marginRight="10dp" 
 
       /> 
 

 

 
      walking 
 
      <TextView 
 
       android:layout_width="match_parent" 
 
       android:layout_height="wrap_content" 
 
       android:id="@+id/txtProcedures" 
 
       android:text="Procedures" 
 
       android:textStyle="bold" 
 
       android:textSize="13sp" 
 
       android:padding="15dp" 
 
       android:layout_marginBottom="-20dp" 
 
       android:layout_marginTop="-5dp"/> 
 

 
      <android.support.v7.widget.RecyclerView 
 
       android:id="@+id/rvRecipeProcedure" 
 
       android:layout_width="match_parent" 
 
       android:layout_height="wrap_content" 
 
       android:orientation="vertical" 
 
       android:padding="15dp"></android.support.v7.widget.RecyclerView> 
 

 

 

 
     </LinearLayout> 
 

 
    </android.support.v4.widget.NestedScrollView> 
 

 
</android.support.design.widget.CoordinatorLayout>

Und mein CardView

<?xml version="1.0" encoding="utf-8"?> 
 
<android.support.v7.widget.CardView 
 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
 
    xmlns:android="http://schemas.android.com/apk/res/android" 
 
    android:id="@+id/recipeInfoCards" 
 
    android:layout_width="81dp" 
 
    android:layout_height="75dp" 
 
    app:cardCornerRadius="6dp" 
 
    android:elevation="15dp" 
 
    android:orientation="horizontal" 
 
    > 
 

 
<LinearLayout 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent" 
 
    android:background="@color/colorRecipeInfoCardBG" 
 
    android:padding="10dp" 
 
    android:orientation="vertical"> 
 

 

 
    <TextView 
 
     android:id="@+id/txtRecipeInfoCardTitle" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:text="Cooking Time:" 
 
     android:textSize="11sp" 
 
     android:textColor="@android:color/black" 
 
     android:layout_gravity="center_horizontal" 
 
     android:gravity="center" 
 
     android:layout_marginTop="5dp" 
 
/> 
 

 
    <TextView 
 
     android:id="@+id/txtRecipeInfoCardDescription" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content" 
 
     android:text="20 Minutes" 
 
     android:textSize="11sp" 
 
     android:textColor="@color/colorTextSecondary" 
 
     android:layout_gravity="center_horizontal" 
 
     android:gravity="center"/> 
 

 

 
</LinearLayout> 
 

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

Und hier sind meine MainActivity und Adapter respectiviely

public class MainActivity extends AppCompatActivity { 
 

 
    ArrayList <Recipe> recipeData; 
 

 
    @Override 
 
    protected void onCreate(Bundle savedInstanceState) { 
 
    super.onCreate(savedInstanceState); 
 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
 
     WindowManager.LayoutParams.FLAG_FULLSCREEN); 
 
    setContentView(R.layout.activity_main); 
 

 
    recipeData = new ArrayList <Recipe>(); 
 
    createData(); 
 

 

 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
 
    setSupportActionBar(toolbar); 
 
    if (getSupportActionBar() != null) { 
 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
 
    } 
 

 
    RecyclerView recipeInfoCardRV = (RecyclerView) findViewById(R.id.rvRecipeInfo); 
 
    recipeInfoCardRV.setHasFixedSize(true); 
 
    recipeInfoCardRV.setNestedScrollingEnabled(false); 
 
    RecipeInfoAdapter recipeInfoAdapter = new RecipeInfoAdapter(this, recipeData); 
 
    recipeInfoCardRV.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)); 
 
    recipeInfoCardRV.setAdapter(recipeInfoAdapter); 
 

 
    RecyclerView recipeIngredientRV = (RecyclerView) findViewById(R.id.rvRecipeIngredient); 
 
    recipeIngredientRV.setHasFixedSize(true); 
 
    recipeIngredientRV.setNestedScrollingEnabled(false); 
 
    recipeIngredientRV.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)); 
 
    RecipeIngredientAdapter recipeIngredientAdapter = new RecipeIngredientAdapter(this, recipeData); 
 
    recipeIngredientRV.setAdapter(recipeIngredientAdapter); 
 
    } 
 

 
    public void createData() { 
 
    ArrayList <RecipeInfoCard> recipeInfoCards = new ArrayList <RecipeInfoCard>(); 
 
    recipeInfoCards.add(new RecipeInfoCard("Cooking Time", "20 Minutes")); 
 
    recipeInfoCards.add(new RecipeInfoCard("Calories", "3501")); 
 
    recipeInfoCards.add(new RecipeInfoCard("Procedures", "Three")); 
 

 

 
    ArrayList <RecipeIngredient> recipeIngredients = new ArrayList <RecipeIngredient>(); 
 
    for (int i = 1; i <= 10; i++) { 
 
     recipeIngredients.add(new RecipeIngredient("Ingredient " + i, String.valueOf(i), "grams")); 
 
    } 
 

 
    Recipe dm = new Recipe(recipeInfoCards, recipeIngredients); 
 
    recipeData.add(dm); 
 
    } 
 

 
}

Mein Adapter für meinen CardView

public class RecipeInfoAdapter extends RecyclerView.Adapter <RecipeInfoAdapter.RecipeInfoCardItemRowHolder> { 
 

 
    private Context mContext; 
 
    private ArrayList <Recipe> recipeData; 
 

 
    public RecipeInfoAdapter(Context mContext, ArrayList <Recipe> recipeData) { 
 
    this.mContext = mContext; 
 
    this.recipeData = recipeData; 
 
    } 
 

 
    @Override 
 
    public RecipeInfoCardItemRowHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
 
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recipe_info_card_view, null); 
 
    return new RecipeInfoCardItemRowHolder(v); 
 
    } 
 

 
    @Override 
 
    public void onBindViewHolder(RecipeInfoCardItemRowHolder recipeInfoCardItemRowHolder, int position) { 
 
    Recipe recipe = recipeData.get(position); 
 
    recipeInfoCardItemRowHolder.infoCardTitle.setText(recipe.getRecipeInfoCards().get(position).getRecipeInfoCardTitle()); 
 
    recipeInfoCardItemRowHolder.infoCardDescription.setText(recipe.getRecipeInfoCards().get(position).getRecipeInfoCardDescription()); 
 
    } 
 

 
    @Override 
 
    public int getItemCount() { 
 
    return (null != recipeData ? recipeData.size() : 0); 
 
    } 
 

 
    public class RecipeInfoCardItemRowHolder extends RecyclerView.ViewHolder { 
 
    protected TextView infoCardTitle; 
 
    protected TextView infoCardDescription; 
 

 
    public RecipeInfoCardItemRowHolder(View view) { 
 
     super(view); 
 
     this.infoCardTitle = (TextView) view.findViewById(R.id.txtRecipeInfoCardTitle); 
 
     this.infoCardDescription = (TextView) view.findViewById(R.id.txtRecipeInfoCardDescription); 
 
    } 
 
    } 
 
}

habe ich alle meine Code hier nicht, weil ich denke, die Frage ist lang genug, aber ich würde wirklich schätzen, wenn jemand mir geholfen. Vielen Dank!

+0

kann u ... mehr darüber –

+0

jede Ausnahme erklären? –

+0

@NileshRathod Ich versuche ein RecyclerView zu erstellen, aber aus irgendeinem Grund wird es nicht richtig angezeigt und ich weiß nicht warum. Ich habe mehrere Elemente hinzugefügt, aber es zeigt sich nur als eines der Elemente und alles ist zusammengeknüllt. – Harrison

Antwort

0

Versuchen Sie, Ihre rvRecipeInfo RecyclerView Höhe zu ändern .. weil Ihr hardicoading es 75 dp ist, was falsch ist.

<android.support.v7.widget.RecyclerView 
       android:id="@+id/rvRecipeInfo" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_vertical" 
       android:orientation="horizontal" 
       android:padding="15dp"></android.support.v7.widget.RecyclerView> 

similer cardview sowie

< ?xml version="1.0" encoding="utf-8"?> 
    <android.support.v7.widget.CardView 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/recipeInfoCards" 
     android:layout_width="100dp" 
     android:layout_height="75dp" 
     app:cardCornerRadius="6dp" 
     android:elevation="15dp" 
     android:orientation="horizontal" 
     > 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@color/colorRecipeInfoCardBG" 
     android:padding="10dp" 
     android:orientation="vertical"> 


     <TextView 
      android:id="@+id/txtRecipeInfoCardTitle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Cooking Time:" 
      android:textSize="11sp" 
      android:textColor="@android:color/black" 
      android:layout_gravity="center_horizontal" 
      android:gravity="center" 
      android:layout_marginTop="5dp" 
    /> 

     <TextView 
      android:id="@+id/txtRecipeInfoCardDescription" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="20 Minutes" 
      android:textSize="11sp" 
      android:textColor="@color/colorTextSecondary" 
      android:layout_gravity="center_horizontal" 
      android:gravity="center"/> 


    </LinearLayout> 

    </android.support.v7.widget.CardView> 
+0

ja aber die andere RecyclerView, die für die Ingredients funktioniert auch nicht und alles was ist, ist ein Layout mit einigen TextViews drin – Harrison

+0

rezeptInfoCardRV .setHasFixedSize (wahr); entfernen Sie diese Eigenschaft und versuchen Sie es erneut –

+0

Immer noch nicht funktioniert :(Es sieht immer noch genauso aus wie zuvor. Ich denke, es kann ein Problem sein mit der Art, wie ich Daten oder mein Layout in activity_main hinzufügen, aber ich bin ein Neuling zu Java, also weiß ich nicht – Harrison

Verwandte Themen