I feel like I'm missing something basic, and I'm actually kind of embarrassed asking this question. But that's what happens when you start learning a new platform...
I have a ListViewActivity that is populated via a CursorAdapter. When a list item is clicked, I start a new Activity to display the details for that item. What I want to do is enable flinging/swiping to iterate through the items from the detail activity. I don't want to fully populate a ViewFlipper, as there is no hard-coded limit to the number of items in the List, and it just seems wasteful. Is there some way to intercept the fling gesture/trackball movement, and reset the detail activity with the next/previous item detail?
My ListViewActivity just sets up a Cursor (via managedQuery) and the associated CursorAdapter, with an OnListItemClick that calls startActivity with the appropriate Intent.
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)));
}
}
}
My detail Activity seems pretty straightforward; it's simply a RelativeLayout with a number of Views set up to display the details of the item based on the Intent's Uri.
public class ModelDetail extends Activity {
private Uri mU开发者_Go百科ri;
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();
}
}
Just pass the ID of the current record to the details activity. This will then query the details for the ID given and get a Cursor for that record.
Then create a class which extends ViewSwitcher
(the user will only be able to see one details at a time, so we only need 2 views at any one time). In your custom view switcher class add a method similar to:
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();
}
}
If you don't want an animation the first time setItemDetails
is called then use setAnimateFirstView(false)
on the ViewSwitcher.
Then you need to add an OnTouchListener to detect drags/swipes, and then call setItemDetails
with the next/previous row ID when you detect a horizontal swipe -- you might want to set a different animation depending on whether you are moving to the next or previous item.
精彩评论