1

我对 Github 上 pakerfeldt 的 ViewFlow 类有疑问。

我正在尝试制作“不同的视图视图流”。这是我的活动代码:

package com.suncco.silent_communities.activity;

import org.taptwo.android.widget.TitleFlowIndicator;
import org.taptwo.android.widget.ViewFlow;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

import com.suncco.silent_communities.R;

public class HousingActivity extends BaseActivity {

private ViewFlow viewFlow;
private ListView listView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.housing_activity);
    initMenu();
    viewFlow = (ViewFlow) findViewById(R.id.viewflow);
    DiffAdapter adapter = new DiffAdapter(this);
    viewFlow.setAdapter(adapter);
    TitleFlowIndicator indicator = (TitleFlowIndicator) findViewById(R.id.viewflowindic);
    indicator.setTitleProvider(adapter);
    viewFlow.setFlowIndicator(indicator);

    /** To populate ListView in diff_view1.xml */
    // listView = (ListView) findViewById(R.id.listView1);
    // String[] names = new String[] { "Cupcake", "Donut", "Eclair",
    // "Froyo",
    // "Gingerbread", "Honeycomb", "IceCream Sandwich" };
    // listView.setAdapter(new ArrayAdapter<String>(this,
    // android.R.layout.simple_list_item_1, names));

    }
}

如您所见,这与示例代码非常相似。

这是适配器:

package com.suncco.silent_communities.view;

import org.taptwo.android.widget.TitleProvider;

import com.suncco.silent_communities.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class HousingViewflowAdapter extends BaseAdapter implements
        TitleProvider {

    private static final int VIEW1 = 0;
    private static final int VIEW2 = 1;
    private static final int VIEW_MAX_COUNT = VIEW2 + 1;
    private final String[] names = { "View1", "View2" };

    private LayoutInflater mInflater;

    public HousingViewflowAdapter(Context context) {
        mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return VIEW_MAX_COUNT;
    }

    @Override
    public int getCount() {
        return 0;
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int view = getItemViewType(position);
        if (convertView == null) {
            switch (view) {
            case VIEW1:
                convertView = mInflater.inflate(
                        R.layout.housing_flowview_view1, null);
                break;
            case VIEW2:
                convertView = mInflater.inflate(
                        R.layout.housing_flowview_view2, null);
                break;
            }
        }
        return convertView;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.taptwo.android.widget.TitleProvider#getTitle(int)
     */
    public String getTitle(int position) {
        return names[position];
    }

}

也类似于示例和结束视图:

<?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/com.suncco.silent_communities"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <org.taptwo.android.widget.TitleFlowIndicator
            android:id="@+id/viewflowindic"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dip"
            app:clipPadding="5dp"
            app:footerColor="#FFFFC445"
            app:footerLineHeight="2dp"
            app:footerTriangleHeight="10dp"
            app:selectedColor="#FFFFC445"
            app:selectedSize="12dp"
            app:textColor="#FFFFFFFF"
            app:textSize="11sp"
            app:titlePadding="10dp" />
    </LinearLayout>

    <org.taptwo.android.widget.ViewFlow
        android:id="@+id/viewflow"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        app:sidebuffer="5" />

</LinearLayout>

这是我的例外:

04-03 15:19:59.538: E/AndroidRuntime(1677): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
04-03 15:19:59.538: E/AndroidRuntime(1677):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
04-03 15:19:59.538: E/AndroidRuntime(1677):     at java.util.ArrayList.get(ArrayList.java:311)
04-03 15:19:59.538: E/AndroidRuntime(1677):     at org.taptwo.android.widget.TitleFlowIndicator.onDraw(TitleFlowIndicator.java:157)
04-03 15:19:59.538: E/AndroidRuntime(1677):     at android.view.View.draw(View.java:6880)
04-03 15:19:59.538: E/AndroidRuntime(1677):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
04-03 15:19:59.538: E/AndroidRuntime(1677):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)

有人知道问题是什么吗?提前致谢。

4

1 回答 1

1
@Override
public int getItemViewType(int position) {
    return position % 2;
}

希望这会有所帮助。您覆盖的方法是返回精确值作为位置。而且getCount方法返回 0,它应该是要添加到ListView基于您的 DataProvider 的元素的数量。您可以names.length改为返回。

于 2013-04-03T07:34:54.810 回答