簡體   English   中英

操作欄,帶有列表視圖的導航抽屜

[英]Action bar, navigation drawer with listview

我是android編程的新手,並嘗試實現從數據庫中填充的listview 我想有一個actionbar導航抽屜里我的應用程序。 但是,隨着mainactivity.java擴展listview ,無法擴展appcompatibility類。

我正在嘗試在Internet上找到解決方案,但找不到任何具體的方法。 無論如何,我們可以實現這一目標嗎? 如果有任何資源,請提供。

下面是我的activity_main.xml代碼

<LinearLayout 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"
tools:context=".MainActivity" >

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

我還有一個布局文件app_custom_list.xml

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

<ImageView
android:id="@+id/appIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="3dp"
android:layout_marginTop="3dp"/>
//android:src="@drawable/facebook" />
<TextView
android:id="@+id/titleTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/appIcon"
android:layout_toRightOf="@id/appIcon"
android:layout_marginLeft="3dp"
android:text="Application Title"
android:textSize="22dp"/>


<TextView
android:id="@+id/dlTxt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/titleTxt"
android:layout_marginLeft="10dp"
android:layout_marginRight="3dp"
android:text="description"
android:textSize="18dp"/>

</RelativeLayout>

我的main_activity.java文件如下:

package com.example.gurje.beyondteaching1;

import java.util.List;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ListActivity implements FetchDataListener{
private ProgressDialog dialog;
private String[] mPlanetTitles;

private ListView mDrawerList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();

}

private void initView() {
    // show progress dialog
    dialog = ProgressDialog.show(this, "", "Loading...");

    String url = "http://192.168.0.29:8080/connect.php";

    FetchDataTask task = new FetchDataTask(this);
    task.execute(url);
}

@Override
public void onFetchComplete(List<Application> data) {
    // dismiss the progress dialog
    if(dialog != null)  dialog.dismiss();
    // create new adapter
    ApplicationAdapter adapter = new ApplicationAdapter(this, data);
    // set the adapter to list
    setListAdapter(adapter);
}

@Override
public void onFetchFailure(String msg) {
    // dismiss the progress dialog
    if(dialog != null)  dialog.dismiss();
    // show failure message
    Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}

public void NavigationDrawer(){

}

}

現在,我想補充actionbar給它,但我已經延長listactivity ,我不能擴展appcompat這是實現所需的actionbar

不要擴展ListActivity。 您可以將列表添加到xml中,並使用findViewById(<id>)獲得它。 請參見下面的示例。

main_activity.xml:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent">

      <android.support.v7.widget.Toolbar
      android:id="@+id/toolbar"
      android:layout_width="match_parent"
      android:layout_height="?attr/actionBarSize">

      </android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>


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

</android.support.design.widget.CoordinatorLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:menu="@menu/drawer_menu" />
</android.support.v4.widget.DrawerLayout>

MainActivity.java:

public class MainActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    setContentView(R.layout.main_activity);
    setSupportActionBar((Toolbar) findViewById(R.id.toolbar));

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

  }
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM