ViewFlipper与ListActivity的详细视图?

时间:2011-04-05 14:43:49

标签: android listview viewflipper viewswitcher

我觉得我错过了一些基本的东西,我实际上有点尴尬地问这个问题。但是当你开始学习一个新的平台时会发生这种情况......

我有一个通过CursorAdapter填充的ListViewActivity。单击列表项时,我启动一个新的Activity来显示该项的详细信息。我想要做的是启用flinging / swiping来遍历详细活动中的项目。我不想完全填充ViewFlipper,因为List中的项目数没有硬编码限制,这看起来很浪费。有没有办法拦截投掷手势/轨迹球运动,并使用下一个/上一个项目细节重置细节活动?

我的ListViewActivity只是设置了一个Cursor(通过managedQuery)和相关的CursorAdapter,以及一个用适当的Intent调用startActivity的OnListItemClick。

public final class ModelList extends ListActivity {

    private Cursor m_models = null;
    private ModelAdapter m_adapter;

    @Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(m_models == null) {
            m_models = managedQuery(Model.CONTENT_URI, Model.PROJECTION, null, null, Model.DEFAULT_SORT_ORDER);
        }
        m_adapter = new ModelAdapter(this, R.layout.modelitem, m_models);
        setContentView(R.layout.forcelist);
        setTitle(R.string.activity_list);
        setListAdapter(m_adapter);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Log.d(getPackageName(), "Clicked item id:" + id);
        if(id < 0) {
            startActivity(new Intent(Intent.ACTION_EDIT, Model.CONTENT_URI));
        } else {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(Model.CONTENT_URI + "/" + id)));
        }
    }
}

我的细节活动似乎很简单;它只是一个RelativeLayout,其中设置了许多视图,以根据Intent的Uri显示项目的详细信息。

public class ModelDetail extends Activity {

    private Uri mUri;
    private FactionHelper factionHelper;
    private static String mCostFormat;
    private static String mDescriptionFormat;
    private ModelAdapter.MarAdapter marAdapter;

    @Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.modeldetail);
        setTitle(R.string.activity_detail);
        mCostFormat = getBaseContext().getResources().getString(R.string.modelCostFormat);
        mDescriptionFormat = getBaseContext().getResources().getString(R.string.modelDescriptionFormat);
        mUri = getIntent().getData();
        factionHelper = new FactionHelper(getBaseContext());
        populateData();
    }

    protected void populateData() {

        TextView name = (TextView) findViewById(R.id.modelName);
        TextView description = (TextView) findViewById(R.id.modelDescription);
        // ... More finding Views and populating data ...
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        populateData();
    }

}

1 个答案:

答案 0 :(得分:0)

只需将当前记录的ID传递给详细信息活动即可。然后,它将查询给定ID的详细信息,并获取该记录的Cursor。

然后创建一个扩展ViewSwitcher的类(用户一次只能看到一个细节,所以我们一次只需要2个视图)。在自定义视图切换器类中添加类似于以下的方法:

class MySwitcher extends ViewSwitcher {
    ...

    long currentRowId;
    long nextRowId;
    long previousRowId;

    public void setItemDetails(long rowId) {
        final View nextView = this.getNextView();

        currentRowId = rowId;
        /* Get the next/previous record IDs with a query like
         * "SELECT row_id FROM table WHERE row_id < "+currentRowId+" ORDER BY row_id DESC LIMIT 1;" (Previous)
         * "SELECT row_id FROM table WHERE row_id > "+currentRowId+" ORDER BY row_id ASC LIMIT 1;" (Next)
         */        

        // Do something with nextView, like set some text or something
        // You could get a cursor based on the rowID with
        // "SELECT * FROM table WHERE row_id = "+currentRowId+" LIMIT 1;"
        ((TextView) nextView.findViewById(R.id.myTextView)).setText("Details for item #" + rowId);
        this.showNext();
    }
}

如果您第一次调用setItemDetails时不想要动画,请在ViewSwitcher上使用setAnimateFirstView(false)

然后你需要添加一个OnTouchListener来检测拖动/滑动,然后在检测到水平滑动时用下一个/上一个行ID调用setItemDetails - 你可能想要根据是否设置不同的动画你正在转向下一个或上一个项目。