16

如何在扩展 ListActivity 的 Activity 中使用“android-support-v7-appcompat”的 ActionBarActivity。

例如我有一个活动

public class xxxxxListActivity
  extends ListActivity implements OnItemSelectedListener  { 
  // ...................
} 

在上述活动中,我想使用“ActionBarActivity”,但由于 java dosent 支持多重继承,我无法让它工作。

4

6 回答 6

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(XML 中为“@android:id/list”)的 ListView 的布局。

getListAdapter() 中的 spiel 是处理标题视图已添加到 ListView 的情况。似乎 ListView 将自己的适配器设置为HeaderViewListAdapter,因此我们必须尝试获取包装的适配器以防止转换错误。

编辑:尝试添加此功能以满足 onListItemClick 的需要:

protected void onListItemClick(ListView lv, View v, int position, long id) {
    getListView().getOnItemClickListener().onItemClick(lv, v, position, id);
}
于 2013-10-09T22:00:51.317 回答
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>
于 2013-09-19T14:02:34.167 回答
0

该解决方案基于@patrick接受的解决方案。这是完整的代码:

首先是 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() 你可以说当用户接受某些东西时要做什么。

在评论中让我知道您的想法/问题。

干杯

于 2015-01-24T08:33:32.067 回答
0

我的答案基于接受的一个,还包含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);
    }
};
}
于 2014-06-06T06:56:14.443 回答
0

Add following line of code to your class and make sure that the class implements AdapterView.OnItemClickListener:

getListView().setOnItemClickListener(this);
于 2015-02-15T18:24:14.120 回答
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) {
    }
}
于 2014-05-27T10:23:47.007 回答