从 BaseAdapter 填充 ImageView(刷卡)

Populate ImageView from BaseAdapter (Swipecards)

我正在尝试使用 Swipecards 库 (https://github.com/Diolor/Swipecards) 构建 tinder-esqe 应用程序。我正在使用一个 BaseAdapter 来填充一个布局,其中包含两个文本视图和一个将提供给主 SwipeFlingAdapterView 的图像视图。虽然两个文本字段都已填充,但我无法让图像出现在卡片上。我已经用 ArrayAdapter 和 BaseAdapter 尝试过这个实现,结果是一样的。

activity布局(deal_page_layout)

<FrameLayout
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_height="match_parent"
android:layout_width="match_parent">
<com.lorentzos.flingswipe.SwipeFlingAdapterView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/swipe_fling_view"
    app:rotation_degrees="10"
    tools:context=".DealPage"
    android:alpha="1.0"
    app:max_visible="2"
    app:min_adapter_stack="5"/>
</FrameLayout>

BaseAdapter 填充的布局 (deal_card)

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/deal_card_image">
</ImageView>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/deal_card_title"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_margin="15dp"
    android:gravity="center"
    android:textSize="20dp"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/deal_card_description"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_margin="15dp"
    android:gravity="center"
    android:textSize="20dp"/>
</RelativeLayout>

基础适配器class

public class DealBaseAdapter extends BaseAdapter {

private Context context;
private List<GrubbyDeal> dealList;
private LayoutInflater li;

public DealBaseAdapter(Context context, LayoutInflater li, ArrayList<GrubbyDeal> dealList){
    this.context = context;
    this.dealList = dealList;
    this.li = li;
}

@Override
public int getCount(){
    return dealList.size();
}

@Override
public Object getItem(int position){
    return dealList.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent){
    ViewHolder viewHolder;

    //resuse a view if possible
    if(convertView == null){
        convertView = li.inflate(R.layout.deal_card,parent,false);
        viewHolder = new ViewHolder();
        viewHolder.img = (ImageView) convertView.findViewById(R.id.deal_card_image);
        viewHolder.title = (TextView) convertView.findViewById(R.id.deal_card_title);
        viewHolder.desc = (TextView) convertView.findViewById(R.id.deal_card_description);

        convertView.setTag(viewHolder);
    }
    else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    GrubbyDeal curDeal = dealList.get(position);

    viewHolder.img.setImageURI(curDeal.getImageUri());
    viewHolder.title.setText(curDeal.getTitle());
    viewHolder.desc.setText(curDeal.getDescription());

    return convertView;
}

//view holder class to hold cached findViewByID results
private static class ViewHolder {
    public ImageView img;
    public TextView title;
    public TextView desc;
}

和主要 activity(DealPage)

public class DealPage extends Activity {
private ArrayList<GrubbyDeal> dealList;

private DealBaseAdapter dealAdapter;

SwipeFlingAdapterView flingContainer;

@Override
public void onCreate(Bundle sis){
    super.onCreate(sis);
    setContentView(R.layout.deal_page_layout);
    //add some awesome cat deals to the adapter
    dealList = new ArrayList<>();
    for(int i=0; i < 5; i++){
        GrubbyDeal tmp = new GrubbyDeal(i);
        dealList.add(tmp);
    }
    //add another type of cat deal to the list
    dealList.add(new GrubbyDeal());

   dealAdapter = new DealBaseAdapter(this, getLayoutInflater(), dealList);

    flingContainer = (SwipeFlingAdapterView) findViewById(R.id.swipe_fling_view);
    flingContainer.setAdapter(dealAdapter);
    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!");
            GrubbyDeal popped = dealList.remove(0);
            dealList.add(popped);
            dealAdapter.notifyDataSetChanged();
        }

        @Override
        public void onLeftCardExit(Object dataObject) {
            makeToast(DealPage.this, "Left!");
        }

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

        @Override
        public void onAdapterAboutToEmpty(int itemsInAdapter) {
            dealList.add(new GrubbyDeal());
            dealAdapter.notifyDataSetChanged();
            Log.d("LIST", "notified");
        }

        @Override
        public void onScroll(float scrollProgressPercent) {
            View view = flingContainer.getSelectedView();
        }
    });

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

}

我是不是遗漏了什么明显的东西?我应该使用一些非常高级的库吗?谢谢,

伊恩

我建议使用 Picasso 将图像加载到图像视图中。

Picasso.with(context).load(imgurl).into(viewHolder.img);

问题在于格式化。我试图使用

Uri.parse("android.resource://com.thepackage.theapp/R.drawable.cat4.jpg");

但没有得到有效的 Uri。因此,我将资源 ID 与 picasso 一起使用,并且该卡效果很好!