2016-12-30 1 views
1

Mein Problem ist, dass ich eine Set-Stack-Karte habe. Jede Karte enthält verschiedene Formulare, so dass ich verschiedene Layouts platzieren muss.Android Stacked Taskcards mit verschiedenen Layouts

Meine Frage ist, wie kann ich unterschiedliche Layout für jede Aufgabenkarten zuweisen?

Finden Sie das Bild unten, die meine Anforderung ist. Kann mir bitte jemand helfen, ein Beispiel vorzuschlagen. [! [Bildbeschreibung hier eingeben] [1]] [1]

Ich verwende hierfür die Cardstack-Bibliothek.

Mein MyActivity.java

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 

import com.wenchao.cardstack.CardStack; 


public class MyActivity extends Activity { 
    private CardStack mCardStack; 
    private CardsDataAdapter mCardAdapter; 

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

     mCardStack = (CardStack)findViewById(R.id.container); 

     mCardStack.setContentResource(R.layout.card_content); 
     mCardStack.setStackMargin(20); 

     mCardAdapter = new CardsDataAdapter(getApplicationContext()); 
     mCardAdapter.add("task1"); 
     mCardAdapter.add("task2"); 
     mCardAdapter.add("task3"); 
     mCardAdapter.add("task4"); 
     mCardAdapter.add("task5"); 
     mCardAdapter.add("task6"); 

     mCardStack.setAdapter(mCardAdapter); 

     if(mCardStack.getAdapter() != null) { 
      Log.i("MyActivity", "Card Stack size: " + mCardStack.getAdapter().getCount()); 
     } 
    } 
    @Override 
    public void onBackPressed() { 
     mCardStack.setAdapter(mCardAdapter); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.my, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 

CardsDataAdapter.java

import android.content.Context; 

import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class CardsDataAdapter extends ArrayAdapter<String> { 

    public CardsDataAdapter(Context context) { 
     super(context, R.layout.card_content); 
    } 

    @Override 
    public View getView(int position, final View contentView, ViewGroup parent){ 
     TextView v = (TextView)(contentView.findViewById(R.id.content)); 
     v.setText(getItem(position)); 
     return contentView; 
    } 

} 

activity_my.xml

<RelativeLayout 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" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MyActivity"> 

    <com.wenchao.cardstack.CardStack 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:clipChildren="false" 
     android:clipToPadding="false" 
     android:padding="20dp" 
     app:stackMargin="40" /> 

</RelativeLayout> 

card_content.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    card_view:cardCornerRadius="6dp" 
    card_view:cardElevation="10dp"> 

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

     <TextView 
      android:id="@+id/content" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      tools:text="Test Text" /> 

     <EditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:inputType="textEmailAddress" 
      android:ems="10" 
      android:id="@+id/editText" /> 
     <EditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:inputType="textEmailAddress" 
      android:ems="10" 
      android:id="@+id/editText2" /> 


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

** Gradle Dateiabhängigkeiten **

dependencies { 
    compile fileTree(include: ['*.jar'], dir: 'libs') 
    //compile(name:'android-card-stack-0.1.0', ext:'aar') 
    //http://stackoverflow.com/questions/21170395/how-to-include-a-library-module-dependency-in-an-android-studio-project 
    compile project(':CardStack') 
    compile 'com.android.support:appcompat-v7:23.1.1' 
    compile 'com.android.support:cardview-v7:23.1.1' 
} 

Ich möchte

card_content.xml layout for first task card 
card_content_two.xml for second 
card_content_three.xml for fourth etc 

Wie ich dies erreichen können? Bitte hilf mir.

Antwort

1

Entschuldigung, ich habe nicht genug Ruf, um das zu kommentieren, deshalb habe ich hier meinen Kommentar gepostet.

Bitte schauen Sie sich das Beispiel in github an, das Ihnen vielleicht helfen kann. Fügen Sie für das andere Layout beim Erstellen der Adapterliste auch das Layout hinzu. Und das Layout dort drüben aufblasen. Bitte sehen Sie sich die

+1

Danke für Ihren Vorschlag, ich werde das überprüfen. –