如何为SwipeCard自定义适配器?

时间:2018-11-21 06:03:16

标签: android android-studio swipe swipecardview

我正在使用SwipeCard

制作闪存卡应用

这是我的FlashCardActivity

import android.os.Bundle;
import android.text.style.TtsSpan;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lorentzos.flingswipe.SwipeFlingAdapterView;

import org.koreanlab.fabloading.R;
import org.koreanlab.fabloading.adapter.CardListAdapter;
import org.koreanlab.fabloading.basickit.BasicCompatActivity;
import org.koreanlab.fabloading.basickit.remote.RemoteService;
import org.koreanlab.fabloading.basickit.remote.ServiceGenerator;
import org.koreanlab.fabloading.item.CardItem;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class FlashCardActivity extends BasicCompatActivity {
    private String TAG = getClass().getSimpleName();
    private ArrayList<CardItem> cardList;
    private ArrayAdapter<String> cardAdapter;
    private CardListAdapter cardListAdapter;

    private int i;
    CardItem newCard;

    @BindView(R.id.frame)
    SwipeFlingAdapterView flingContainer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flashcard);
        ButterKnife.bind(this);

        cardList = new ArrayList<>();
        Log.d(TAG, "cardList created");

        // get Two Words;
        cardList.add(getCardItem());
        cardList.add(getCardItem());

        Log.d(TAG, "added Card");
        cardListAdapter = new CardListAdapter(this, R.layout.card_item, cardList);
        Log.d(TAG, "adapter created: " + (cardListAdapter == null ? "cardAdapter is NULL" : "cardAdapter is not NULL"));
        flingContainer.setAdapter(cardListAdapter);
        Log.d(TAG, "adapter set: " + (flingContainer == null ? "flingContainer is NULL" : "flingContainer is not NULL"));
        flingContainer.setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
            @Override
            public void removeFirstObjectInAdapter() {
                // this is the simplest way to delete an object from the Adapter (/AdapterView)
                Log.d("LIST", "removed object!");
                cardList.remove(0);
                cardListAdapter.notifyDataSetChanged();
            }

            @Override
            public void onLeftCardExit(Object dataObject) {
                //Do something on the left!
                //You also have access to the original object.
                //If you want to use it just cast it (String) dataObject
                makeToast(FlashCardActivity.this, "Left!");
            }

            @Override
            public void onRightCardExit(Object dataObject) {
                makeToast(FlashCardActivity.this, "Right!");
            }

            @Override
            public void onAdapterAboutToEmpty(int itemsInAdapter) {
                // Ask for more data here
                Log.d(TAG, "onAdapterAboutToEmpty: "+itemsInAdapter);
                cardList.add(getCardItem());
                cardListAdapter.notifyDataSetChanged();
                Log.d("LIST", "notified");
                i++;
            }

            @Override
            public void onScroll(float scrollProgressPercent) {
                View view = flingContainer.getSelectedView();
                view.findViewById(R.id.item_swipe_right_indicator).setAlpha(scrollProgressPercent < 0 ? -scrollProgressPercent : 0);
                view.findViewById(R.id.item_swipe_left_indicator).setAlpha(scrollProgressPercent > 0 ? scrollProgressPercent : 0);
            }
        });

        // Optionally add an OnItemClickListener
        flingContainer.setOnItemClickListener(new SwipeFlingAdapterView.OnItemClickListener() {
            @Override
            public void onItemClicked(int itemPosition, Object dataObject) {
                makeToast(FlashCardActivity.this, "Clicked!");
            }
        });
    }

    @OnClick(R.id.right)
    public void right() {
        /**
         * Trigger the right event manually.
         */
        flingContainer.getTopCardListener().selectRight();
    }

    @OnClick(R.id.left)
    public void left() {
        flingContainer.getTopCardListener().selectLeft();
    }

    public CardItem getCardItem() {
        // No problem here.
        return newCard;
    }    
}

请注意,getCardItem()仅获得一张卡。首先,创建活动后,它将调用两张卡,并且cardList有两张卡。之后,我只想在刷卡后得到一张卡。 getCardItem()没问题。我可以看到我从服务器接收到数据。

这是我的自定义CardListAdapter

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import org.koreanlab.fabloading.R;
import org.koreanlab.fabloading.item.CardItem;

import java.util.ArrayList;
import java.util.List;

public class CardListAdapter extends ArrayAdapter {
    private final String TAG = this.getClass().getSimpleName();
    private Context context;
    private int cardResId;
    private int frontResId;
    private int backResId;
    private List<CardItem> cardList;
    private LayoutInflater mInflater;

    //this, R.layout.card_item, R.id.card_front, R.id.card_back, cardList
    public CardListAdapter(Context context, int cardResId, ArrayList<CardItem> cardList) {
        super(context, cardResId);
        this.context = context;
        this.cardResId = cardResId;
        this.cardList = cardList;

        mInflater = LayoutInflater.from(context);
    }

    public void setItem(CardItem newItem) {
        Log.d(TAG, "setItem");

        for (int i = 0; i < cardList.size(); i++) {
            CardItem item = cardList.get(i);

            if (item.seq == newItem.seq) {
                cardList.set(i, newItem);
                break;
            }
        }
    }

    @Override
    public int getCount() {
        Log.d(TAG, "getVIew");
        return 0;
    }

    public int getPosition(CardItem item) {
        Log.d(TAG, "getPosition = "+item);
        return cardList.indexOf(item);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null){
            convertView = mInflater.inflate(cardResId, parent,false);
            holder = new ViewHolder();
            Log.d(TAG, "getView");

            holder.frontTV = convertView.findViewById(R.id.card_front);
            holder.backTV = convertView.findViewById(R.id.card_back);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }
        holder.frontTV.setText((String)getItem(position));
        holder.backTV.setText((String)getItem(position));
        return convertView;
    }
    static class ViewHolder
    {
        TextView frontTV, backTV;
    }
}

我检查了许多博客和其他问答,但还没有弄清楚如何解决此问题。从服务器收到“一个字”数据后,该活动将继续进行,并且不会显示任何错误消息。

0 个答案:

没有答案