Zmiana stanu widoku w zależności od ilości kart

0

Witam,
Mam mały dylemat chodzi o zmianę stanu widoku w zależności ilości kart w przypadku gdy nie ma kart jest ustawiany widok z nazwą brak kart natomiast gdy jest więcej niż jedna karta wyświetla nam się lista kart. Poniżej znajduje się layout podanego widoku

 <LinearLayout
        android:id="@+id/layoutEmptyCard"
        android:layout_width="match_parent"
        android:layout_height="95dp"
        android:background="@drawable/card_view"
        android:orientation="vertical"
        android:visibility="visible">

        <Space
            android:layout_width="match_parent"
            android:layout_height="15dp" />

        <TextView
            android:id="@+id/textCard"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/na_razie_nie_posiadasz_adnych_kart"
            android:textColor="@color/blackColor"
            android:textSize="14sp" />

        <Button
            android:id="@+id/addCardButton"
            style="@style/Base.Widget.AppCompat.Button.Borderless"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/wygeneruj_kart"
            android:textAllCaps="false"
            android:textSize="14sp" />
    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycleViewCard"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone" />

MainActivty.class

Klasa która znajduje się podana lista

public class MainActivity extends AppCompatActivity {

    protected void attachBaseContext(Context context) {
        super.attachBaseContext(CalligraphyContextWrapper.wrap(context));
    }

    private static final int REQUEST_READ_PHONE_STATE = 100;
    public ArrayList<Card> cardsList;
    private String passwordString;

    private TextView textCard;
    private CardAdapter cardAdapter;
    private RecyclerView cardRecyclerView;
    private LinearLayout cardLayout;

    private DatabaseHandler databaseHandler;


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

        Default app = (Default) getApplicationContext();


        int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE);
        if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_READ_PHONE_STATE);
        } else {
            passwordString = deviceUDID(MainActivity.this);
            app.setUuidString(passwordString);
        }

        databaseHandler = new DatabaseHandler(this);
        cardsList = new ArrayList<>();
        cardsList = databaseHandler.getAllCard();


        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        Typeface custom_fonts = Typeface.createFromAsset(getAssets(), "fonts/OpenSans-Regular.ttf");
        Typeface custom_fonts2 = Typeface.createFromAsset(getAssets(), "fonts/OpenSans-Bold.ttf");
        cardLayout = (LinearLayout) findViewById(R.id.layoutEmptyCard);

        cardRecyclerView = (RecyclerView) findViewById(R.id.recycleViewCard);
        cardAdapter = new CardAdapter(this, cardsList, passwordString);
        cardRecyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
        cardRecyclerView.setAdapter(cardAdapter);

        TextView title_app = (TextView) findViewById(R.id.toolbar_title);
        title_app.setTypeface(custom_fonts);

        textCard = (TextView) findViewById(R.id.textCard);
        textCard.setTypeface(custom_fonts);

        Button addCardButton = (Button) findViewById(R.id.addCardButton);
        addCardButton.setTypeface(custom_fonts2);
        addCardButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addCard();
            }
        });
    }



0

Pisałem już w poprzednim Twoim wątku, że w Androidzie wymyślono do tego Fragmenty. Robisz sobie dwa fragmenty, jeden prezentujący ekran z listą kart, drugi prezentujący ekran z pustą listą kart. Następnie podmieniasz je w zależności od potrzeb.

https://developer.android.com/training/basics/fragments/fragment-ui.html#Replace

Jeżeli nie chcesz używać fragmentów, to możesz po prostu podmieniać widoki. Najpierw wczytujesz oba layouty w onCreate:

view1 = getLayoutInflater().inflate(R.layout.layout1, null);
view2 = getLayoutInflater().inflate(R.layout.layout2, null);

a później po sprawdzeniu warunku ustawiasz:

setContentView(view1);

lub

setContentView(view2);

1 użytkowników online, w tym zalogowanych: 0, gości: 1