因此,我已按照本教程(https://www.youtube.com/watch?v=cS5l_hPwC10)的操作方法进行了卡片叠操作,即使同时使用2个卡片叠,它也可以正常工作。现在我想添加一个按钮,以便我可以重置堆栈和/或一个按钮以获取上一张卡
这是我与卡堆栈有关的所有代码。告诉我是否需要更多,因为这是我第一次尝试编程
hauptanwendung_fragment.xml
<LinearLayout 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:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FragmentHauptanwendung">
<com.wenchao.cardstack.CardStack
android:id="@+id/card_stack"
android:layout_width="280dp"
android:layout_height="170dp"
android:clipChildren="false"
android:clipToPadding="false" />
<com.wenchao.cardstack.CardStack
android:id="@+id/card_stack2"
android:layout_width="280dp"
android:layout_height="170dp"
android:clipChildren="false"
android:clipToPadding="false" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="@+id/stern"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
app:srcCompat="@drawable/ic_stern02" />
<Space
android:layout_width="50sp"
android:layout_height="match_parent"
android:layout_marginRight="15sp"
android:layout_weight="1" />
<ImageButton
android:id="@+id/stift"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
app:srcCompat="@drawable/ic_bearbeiten01" />
</LinearLayout>
</LinearLayout>
card_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical">
<ImageView
android:id="@+id/image_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="center" />
</LinearLayout>
card_layout2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical">
<ImageView
android:id="@+id/image_content2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="center" />
</LinearLayout>
片段Hauptanwendung.java
package com.example.ideenfinder3;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.wenchao.cardstack.CardStack;
import java.util.Objects;
public class FragmentHauptanwendung extends Fragment implements CardStack.CardEventListener {
View view;
private CardsDataAdapter card_adapter;
private CardsDataAdapter2 card_adapter2;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.hauptanwendung_fragment, container, false);
initImages();
initImages2();
CardStack card_stack = (CardStack) view.findViewById(R.id.card_stack);
card_stack.setContentResource(R.layout.card_layout);
card_stack.setStackMargin(20);
card_stack.setAdapter(card_adapter);
card_stack.setListener(this);
CardStack card_stack2 = (CardStack) view.findViewById(R.id.card_stack2);
card_stack2.setContentResource(R.layout.card_layout2);
card_stack2.setStackMargin(20);
card_stack2.setAdapter(card_adapter2);
card_stack2.setListener(this);
return view;
}
private void initImages2() {
card_adapter2 = new CardsDataAdapter2(Objects.requireNonNull(getActivity()).getApplicationContext(),0);
card_adapter2.add(R.drawable.logo);
card_adapter2.add(R.drawable.schriftzug);
}
private void initImages() {
card_adapter = new CardsDataAdapter(Objects.requireNonNull(getActivity()).getApplicationContext(),0);
card_adapter.add(R.drawable.bild01);
card_adapter.add(R.drawable.bild02);
card_adapter.add(R.drawable.bild03);
card_adapter.add(R.drawable.bild04);
card_adapter.add(R.drawable.bild05);
}
@Override
public boolean swipeEnd(int i, float v) {
return v > 300;
}
@Override
public boolean swipeStart(int i, float v) {
return false;
}
@Override
public boolean swipeContinue(int i, float v, float v1) {
return false;
}
@Override
public void discarded(int i, int i1) {
}
@Override
public void topCardTapped() {
}
}
CardsDataAdapter
package com.example.ideenfinder3;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
public class CardsDataAdapter extends ArrayAdapter<Integer> {
public CardsDataAdapter(Context context, int resource) {
super(context, resource);
}
@Override
public View getView(int position, final View ImgcontentView, ViewGroup parent){
ImageView ImgV = (ImageView) (ImgcontentView.findViewById(R.id.image_content));
ImgV.setImageResource(getItem(position));
return ImgcontentView;
}
}
CardsDataAdapter2
package com.example.ideenfinder3;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
public class CardsDataAdapter2 extends ArrayAdapter<Integer> {
public CardsDataAdapter2(@NonNull Context context, int resource) {
super(context, resource);
}
@Override
public View getView(int position, final View ImgcontentView2, ViewGroup parent){
ImageView ImgV2 = (ImageView) (ImgcontentView2.findViewById(R.id.image_content2));
ImgV2.setImageResource(getItem(position));
return ImgcontentView2;
}
}
最佳答案
只需保留CardStack
的句柄即可:
private CardStack cardStack01;
private CardStack cardStack02;
并让
Fragment
实现这些的getter和setter。