如何在扩展ListActivity的活动中使用“android-support-v7-appcompat”的ActionBarActivity。
例如我有一个活动
public class xxxxxListActivity
extends ListActivity implements OnItemSelectedListener {
// ...................
}
在上面的活动中,我想使用“ActionBarActivity”,但由于java dosent支持多重继承,我无法使其正常工作。
答案 0 :(得分:41)
这是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 / list”)。
getListAdapter()中的spiel用于处理已将List视图添加到ListView的情况。似乎ListView将自己的适配器设置为HeaderViewListAdapter,因此我们必须尝试获取包装的适配器以防止出现错误。
编辑:尝试添加此函数以满足onListItemClick:
的需要protected void onListItemClick(ListView lv, View v, int position, long id) {
getListView().getOnItemClickListener().onItemClick(lv, v, position, id);
}
答案 1 :(得分:2)
也许您可以尝试扩展ActionBarActivity
,并为该活动的默认布局设置一些具有ListView
的布局。
这样的事情:
public class AlarmListActivity extends ActionBarActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_with_list_view);
ListView lv = (ListView) findViewById(R.id.listView1);
// populate list view
}
}
和相应的布局文件:
<LinearLayout>
<ListView
android:id="@+id/listView1">
</ListView>
</LinearLayout>
答案 2 :(得分:0)
我决定用代码完成,而不是用XML创建布局。文件a是代码中的一个替代品。
package com.stackoverflow.free.examples;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TableRow;
import android.widget.TextView;
/**
* based of https://raw.githubusercontent.com/android/platform_frameworks_base/d6c1919779acb042392615637b9007e0c4b89023/core/java/android/app/ListActivity.java
* Created by elcuco on 5/27/2014.
*/
@SuppressWarnings("UnusedDeclaration")
public class SupportListActivity extends ActionBarActivity {
protected ListAdapter mAdapter;
protected ListView mList;
protected TextView mEmptyMessage;
@Override
protected void onCreate(Bundle savedBundle)
{
super.onCreate(savedBundle);
mEmptyMessage = new TextView(this);
mEmptyMessage.setText("No results");
mList = new ListView(this);
mList.setEmptyView(mEmptyMessage);
LinearLayout ll = new LinearLayout(this);
ll.addView(mEmptyMessage, TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT);
ll.addView(mList, TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT);
setContentView(ll);
}
public void setListAdapter(ListAdapter adapter) {
synchronized (this) {
mAdapter = adapter;
mList.setAdapter(adapter);
}
}
/**
* Get the activity's list view widget.
*/
public ListView getListView() {
return mList;
}
/**
* Set the currently selected list item to the specified
* position with the adapter's data
*
* @param position the position on list to select
*/
public void setSelection(int position) {
mList.setSelection(position);
}
/**
* Get the position of the currently selected list item.
*/
public int getSelectedItemPosition() {
return mList.getSelectedItemPosition();
}
/**
* Get the cursor row ID of the currently selected list item.
*/
public long getSelectedItemId() {
return mList.getSelectedItemId();
}
private AdapterView.OnItemClickListener mOnClickListener = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
onListItemClick((ListView)parent, v, position, id);
}
};
/**
* This method will be called when an item in the list is selected.
* Subclasses should override. Subclasses can call
* getListView().getItemAtPosition(position) if they need to access the
* data associated with the selected item.
*
* @param l The ListView where the click happened
* @param v The view that was clicked within the ListView
* @param position The position of the view in the list
* @param id The row id of the item that was clicked
*/
protected void onListItemClick(ListView l, View v, int position, long id) {
}
}
答案 3 :(得分:0)
我的回答基于accepted one,还包含 onListItemClick 实现。但它有空视图的问题。
public abstract class ActionBarListActivity extends ActionBarActivity {
private ListView mListView;
protected ListView getListView() {
if (mListView == null) {
mListView = (ListView) findViewById(android.R.id.list);
mListView.setOnItemClickListener(mOnClickListener);
}
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;
}
}
protected void onListItemClick(ListView l, View v, int position, long id) { }
private AdapterView.OnItemClickListener mOnClickListener = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
onListItemClick((ListView) parent, v, position, id);
}
};
}
答案 4 :(得分:0)
此解决方案基于@patrick的accepted solution。这是完整的代码:
首先是XML布局文件activity_main.xml。请注意,我有一个带有ID entryList的ListView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.MainActivity" >
<ListView
android:id="@+id/entryList"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
接下来是我自己的ActionBarListActivity。你会发现一些变化。我希望尽可能使其具有通用性和可重用性。
package com.example.api;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.HeaderViewListAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
public abstract class ActionBarListActivity extends ActionBarActivity {
private final class ListOnItemClickListener implements OnItemClickListener {
public void onItemClick(AdapterView<?> lv, View v, int position, long id) {
onListItemClick((ListView) lv, v, position, id);
// String str = ((TextView) arg1).getText().toString();
// Toast.makeText(getBaseContext(), str,
// Toast.LENGTH_LONG).show();
// Intent intent = new Intent(getBaseContext(),
// your_new_Intent.class);
// intent.putExtra("list_view_value", str);
// startActivity(intent);
}
}
private ListView mListView;
protected ListView getListView() {
if (mListView == null) {
initListView();
}
return mListView;
}
private void initListView() {
mListView = (ListView) findViewById(getListViewId());
if (mListView == null) {
throw new RuntimeException(
"ListView cannot be null. Please set a valid ListViewId");
}
mListView.setOnItemClickListener(new ListOnItemClickListener());
}
protected abstract int getListViewId();
protected void setListAdapter(ListAdapter adapter) {
getListView().setAdapter(adapter);
}
protected void onListItemClick(ListView lv, View v, int position, long id) {
// No default functionality. To override
}
protected ListAdapter getListAdapter() {
ListAdapter adapter = getListView().getAdapter();
if (adapter instanceof HeaderViewListAdapter) {
return ((HeaderViewListAdapter) adapter).getWrappedAdapter();
} else {
return adapter;
}
}
}
接下来是我的MainActivity扩展了上面的类。
package com.example;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.example.api.ActionBarListActivity;
public class MainActivity extends ActionBarListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Log.d("click", "Position click " + position);
String item = (String) getListAdapter().getItem(position);
Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
}
@Override
protected int getListViewId() {
return R.id.entryList;
}
}
基本上,通过覆盖onListItemClick(),您可以说当用户接受某些内容时该怎么做。
请在评论中告诉我您的想法/问题。
干杯
答案 5 :(得分:0)
将以下代码行添加到您的类中,并确保该类实现AdapterView.OnItemClickListener:
getListView().setOnItemClickListener(this);