有什么方法可以在一个活动中添加actionbar和listview吗?

时间:2015-03-15 08:29:13

标签: android listview android-listview android-actionbar navigation-drawer

当我打开活动时,导航抽屉不会出现。那么有什么办法可以在一个活动中一起添加listview和actionbar吗?如果有人能帮助我,我将不胜感激。感谢

public class Cart extends ListActivity {

ArrayAdapter<String> mAdapter;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cart);


    String[] items = new String[2];
    items[0] = "";


    mAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1,
            android.R.id.text1,
            new ArrayList<String>(Arrays.asList(items)));
    setListAdapter(mAdapter);

    ListView listView = getListView();

    SwipeDismissListViewTouchListener touchListener =
            new SwipeDismissListViewTouchListener(
                    listView,
                    new SwipeDismissListViewTouchListener.DismissCallbacks() {
                        @Override
                        public boolean canDismiss(int position) {
                            return true;
                        }

                        @Override
                        public void onDismiss(ListView listView, int[] reverseSortedPositions) {
                            for (int position : reverseSortedPositions) {
                                mAdapter.remove(mAdapter.getItem(position));
                            }
                            mAdapter.notifyDataSetChanged();
                        }
                    });
    listView.setOnTouchListener(touchListener);// ListView scrolling,

}

2 个答案:

答案 0 :(得分:8)

将ListView放在ActionBarActivity

public class someActivity extends ActionBarActivity {

    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_some);

        listView = (ListView) findViewById(R.id.listView);
        YourAdapter listAdapter = new YourAdapter();
        listView.setAdapter(listAdapter);
    }
}

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
    android:id="@+id/listView"
    android:layout_height="match_parent"
    android:layout_width="match_parent"/>

</LinearLayout>

答案 1 :(得分:1)

谢谢乔!我只需要登录并竖起大拇指。解决了我的问题。比我最近几天看到的任何答案都要好:)

这是我如何解决它的代码

使用AppCompatActivity作为ActionBarActivity似乎已被弃用。

public class SelectActivity extends AppCompatActivity /*ListActivity*/ implements OnClickListener {

ListView listView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.select_clothes);

    listView = (ListView) findViewById(R.id.listView);

    mList = getListData();
    MyAdapter listAdapter = new MyAdapter(this, R.layout.row, mList);
    listView.setAdapter(listAdapter);
}