简体   繁体   English

操作栏,带有列表视图的导航抽屉

[英]Action bar, navigation drawer with listview

I am novice to android programming and try to implement a listview which gets populated from the database. 我是android编程的新手,并尝试实现从数据库中填充的listview I want to have an actionbar with navigation drawer in my application. 我想有一个actionbar导航抽屉里我的应用程序。 However as mainactivity.java extends listview , it's not possible to extend appcompatibility class. 但是,随着mainactivity.java扩展listview ,无法扩展appcompatibility类。

I am trying to find solution on internet, but unable to find anything concrete. 我正在尝试在Internet上找到解决方案,但找不到任何具体的方法。 Is there anyway in which we can implement this? 无论如何,我们可以实现这一目标吗? If there is any resource please provide that. 如果有任何资源,请提供。

below is code of my activity_main.xml 下面是我的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>

I have one more layout file app_custom_list.xml 我还有一个布局文件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>

My main_activity.java file is below: 我的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(){

}

} }

Now i want to add actionbar to it, but as i am already extending listactivity , I cant extend appcompat which is required to implement actionbar 现在,我想补充actionbar给它,但我已经延长listactivity ,我不能扩展appcompat这是实现所需的actionbar

Don't extend ListActivity. 不要扩展ListActivity。 You can add list to your xml and get it using findViewById(<id>) . 您可以将列表添加到xml中,并使用findViewById(<id>)获得它。 See example below. 请参见下面的示例。

main_activity.xml: 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: 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