ActionBarActivity的" Android的支持-V7-appcompat"与ListActivity在同一活动 [英] ActionBarActivity of "android-support-v7-appcompat" and ListActivity in Same activity
问题描述
如何在其中扩展了ListActivity活动使用的Android的支持-V7-appcompatActionBarActivity。
How to use ActionBarActivity of "android-support-v7-appcompat" in the activity which Extends the ListActivity.
例如我有一个活动
public class xxxxxListActivity
extends ListActivity implements OnItemSelectedListener {
// ...................
}
在上面的活动,我想用ActionBarActivity但与Java dosent支持多重继承我无法得到它的工作。
In the above activity i want to use "ActionBarActivity" but as java dosent support multiple inheritance I am not able to get it working.
推荐答案
下面是ActionBarListActivity的实现:
Here's an implementation of ActionBarListActivity:
public abstract class ActionBarListActivity extends ActionBarActivity {
private ListView mListView;
protected ListView getListView() {
if (mListView == null) {
mListView = (ListView) findViewById(android.R.id.list);
}
return mListView;
}
protected void setListAdapter(ListAdapter adapter) {
getListView().setAdapter(adapter);
}
protected ListAdapter getListAdapter() {
ListAdapter adapter = getListView().getAdapter();
if (adapter instanceof HeaderViewListAdapter) {
return ((HeaderViewListAdapter)adapter).getWrappedAdapter();
} else {
return adapter;
}
}
}
就像普通ListActivity,你需要的是包含了ID android.R.id.list一个ListView(:在XML@android ID /列表)的布局。
Just like regular ListActivity, you'll need a layout that contains a ListView with the ID android.R.id.list ("@android:id/list" in XML).
在getListAdapter()的高谈阔论是处理其中的header视图被添加到ListView箱子。好像ListView的设置它自己的适配器到<一个href="http://developer.android.com/reference/android/widget/HeaderViewListAdapter.html">HeaderViewListAdapter,所以我们要尝试并获得包裹适配器prevent转换错误。
The spiel in getListAdapter() is to handle cases where header views have been added to the ListView. Seems like ListView sets its own adapter to a HeaderViewListAdapter, so we have to try and get the wrapped adapter to prevent casting errors.
编辑:尝试添加该功能,以满足需要onListItemClick:
Try adding this function to satisfy the need for onListItemClick:
protected void onListItemClick(ListView lv, View v, int position, long id) {
getListView().getOnItemClickListener().onItemClick(lv, v, position, id);
}
这篇关于ActionBarActivity的&QUOT; Android的支持-V7-appcompat&QUOT;与ListActivity在同一活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!