2

我创建了一个扩展 CardArrayAdapter 的自定义适配器。只是玩它以获得舒适。将其视为带有查看器的自定义 baseadapter 等等。在测试应用程序中设置我认为正确的所有内容,然后应用程序打开,但没有填充任何内容。

任何帮助表示赞赏。

Java 文件:

    package com.example.test;

import it.gmariotti.cardslib.library.internal.Card;
import it.gmariotti.cardslib.library.internal.CardArrayAdapter;
import it.gmariotti.cardslib.library.view.CardListView;
import it.gmariotti.cardslib.library.view.CardView;

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

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_list);

        CardListView listView = (CardListView) this.findViewById(R.id.myList);      
        List<Card> cards= new ArrayList<Card>();

        for (int i = 0; i < 10; i++) {
            String header = "Poop card" + String.valueOf(i);
            String body = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut";

            CardExample card = new CardExample(this, header, body);
            cards.add(card);
        }

        MyCardArrayAdapter cardArrayAdapter = new MyCardArrayAdapter(this, cards);
        if (listView != null) {
            listView.setAdapter(cardArrayAdapter);
        }
    }

    public class CardExample extends Card {

        protected TextView headerView;
        protected TextView titleView;

        protected String mTitleHeader;
        protected String mTitleMain;

        public CardExample(Context context, String titleHeader, String titleMain) {
            super(context, R.layout.row_card);
            Log.d("CardExample",
                    String.valueOf(titleHeader) + ", "
                            + String.valueOf(titleMain));

            this.mTitleHeader = titleHeader;
            this.mTitleMain = titleMain;
        }

        @Override
        public void setupInnerViewElements(ViewGroup parent, View view) {
            // TODO Auto-generated method stub
            headerView = (TextView) view
                    .findViewById(R.id.card_main_inner_simple_header);
            titleView = (TextView) view
                    .findViewById(R.id.card_main_inner_simple_title);

            headerView.setText(mTitleHeader);
            titleView.setText(mTitleMain);

        }
    }

    public class MyCardArrayAdapter extends CardArrayAdapter {

        Context mContext;
        ViewHolder mHolder;
        List<Card> cards;

        public MyCardArrayAdapter(Context context, List<Card> cards) {
            super(context, cards);
            // TODO Auto-generated constructor stub
            mContext = context;
            this.cards = cards;

            Log.d("MyCardArrayAdapter", "Cards size = " + String.valueOf(cards.size()));
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return cards.size();
        }

        @Override
        public Card getItem(int position) {
            // TODO Auto-generated method stub
            return cards.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) mContext
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.layout_cardview,
                        parent, false);
                mHolder = new ViewHolder();

                mHolder.cardView = (CardView) convertView
                        .findViewById(R.id.row_card);

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

            mHolder.cardView.setCard(getItem(position));

            return convertView;
        }

        public class ViewHolder {
            private CardView cardView;
        }
    }
}

布局列表.xml:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <it.gmariotti.cardslib.library.view.CardListView
        android:id="@+id/myList"
        style="@style/list_card.thumbnail"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        card:list_card_layout_resourceID="@layout/list_card_thumbnail_layout" />

</LinearLayout>

layout_cardview.xml:

<it.gmariotti.cardslib.library.view.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card="http://schemas.android.com/apk/res-auto"
android:id="@+id/row_card"
style="@style/list_card"
android:layout_width="190dp"
android:layout_height="wrap_content"
card:card_layout_resourceID="@layout/row_card" />

row_card.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
    android:id="@+id/card_main_inner_simple_header"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp" />

<TextView
    android:id="@+id/card_main_inner_simple_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp" />

4

2 回答 2

1

您的getView()不会调用 super.getView();

于 2014-08-27T07:58:26.733 回答
1

我以稍微不同的方式解决了这个问题。与其创建另一个自定义适配器,不如简单地创建一个普通适配器。通过方法 setupInnerViewElements() 进行的内部绑定已经解决了这个问题。

public class CardExample extends Card {

    protected TextView headerView;
    protected TextView titleView;

    protected String mTitleHeader;
    protected String mTitleMain;

    public CardExample(Context context, String titleHeader, String titleMain) {
        super(context, R.layout.row_card);
        Log.d("CardExample",
                String.valueOf(titleHeader) + ", "
                        + String.valueOf(titleMain));

        this.mTitleHeader = titleHeader;
        this.mTitleMain = titleMain;
    }

    @Override
    public void setupInnerViewElements(ViewGroup parent, View view) {
        // TODO Auto-generated method stub
        headerView = (TextView) view
                .findViewById(R.id.card_main_inner_simple_header);
        titleView = (TextView) view
                .findViewById(R.id.card_main_inner_simple_title);

        headerView.setText(mTitleHeader);
        titleView.setText(mTitleMain);

    }
}

将其传递给 CardAdapter,例如 -

ArrayList<Card> timeCardsList = new ArrayList<Card>();

    timeCardsList.add(new CardExample(getApplicationContext(), "Hello", "World"));
    timeCardsList.add(new CardExample(getApplicationContext(),"Hakuna", "Matata"));
    timeCardsList.add(new CardExample(getApplicationContext(), "Chal", "Chaiyan Chaiyan"));



    CardArrayAdapter mCardArrayAdapter = new CardArrayAdapter(this, timeCardsList);

    if (timeCardListView != null) {
        timeCardListView.setAdapter(mCardArrayAdapter);
    } 
于 2015-08-21T10:16:30.537 回答