如何更改android导航抽屉项目

时间:2016-06-13 12:57:49

标签: android listview android-drawer

我的导航抽屉使用listview,因此我将listview作为抽屉菜单。现在在我的项目中,我想DIY将列表视图更改为textviewimageview。这是抽屉主要布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/wallpaper01"
    android:id="@+id/drawer_layout">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/main_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="luis"/>
    </RelativeLayout>
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lv_sliding_menu"
        android:background="@drawable/wallpaper01"
        android:choiceMode="singleChoice"
        android:layout_gravity="start"></ListView>

</android.support.v4.widget.DrawerLayout>

我添加RelativeLayoutTextView,但模拟不起作用,我不知道为什么? 我这样添加:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/wallpaper01"
    android:id="@+id/drawer_layout">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/main_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="luis"/>

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start">
      <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="hello"/>
      <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lv_sliding_menu"
        android:background="@drawable/wallpaper01"
        android:choiceMode="singleChoice"
        ></ListView>
    </RelativeLayout>
</android.support.v4.widget.DrawerLayout>

这是我的MainActivity:

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;

import com.ad_imagine.vor.adapter.SlidingMenuAdapter;
import com.ad_imagine.vor.fragment.coach;
import com.ad_imagine.vor.fragment.emotion;
import com.ad_imagine.vor.fragment.historique;
import com.ad_imagine.vor.fragment.parametre;
import com.ad_imagine.vor.fragment.profile;
import com.ad_imagine.vor.fragment.process;
import com.ad_imagine.vor.model.ItemSlideMenu;

import java.util.ArrayList;
import java.util.List;


public class Accueil extends AppCompatActivity {

    private List<ItemSlideMenu> listSliding;//class ItemSlideMenu
    private SlidingMenuAdapter adapter;
    private ListView listViewSliding;
    private DrawerLayout drawerLayout;
    private ActionBarDrawerToggle actionBarDrawerToggle;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.accueil);

        //Init component( accueil.xml)
        listViewSliding = (ListView) findViewById(R.id.lv_sliding_menu);//id listview
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);//id drawerlayout
        listSliding = new ArrayList<>();
        //Add item for sliding list
        listSliding.add(new ItemSlideMenu("VOR Emotion", "Calculer mon VOR"));
        listSliding.add(new ItemSlideMenu("VOR Process", "Calculer mon VOR"));
        listSliding.add(new ItemSlideMenu("VOR Autre", "Historique"));
        listSliding.add(new ItemSlideMenu("", "Parametre"));
        listSliding.add(new ItemSlideMenu("", "Mon Profile"));
        listSliding.add(new ItemSlideMenu("", "Mon Coach"));
        adapter = new SlidingMenuAdapter(this, listSliding);
        listViewSliding.setAdapter(adapter);

        //Display icon to open/ close sliding list
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        //Set title
        //setTitle(listSliding.get(0).getTitle());
        //item selected
        listViewSliding.setItemChecked(0, true);
        //Close menu
        drawerLayout.closeDrawer(listViewSliding);

        //**************Display emotion when start**********************
        replaceFragment(0);
        //***************Hanlde on item click*****************

        listViewSliding.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //Set title
                setTitle(listSliding.get(position).getTitle());
                //item selected
                listViewSliding.setItemChecked(position, true);
                //Replace fragment
                replaceFragment(position);
                //Close menu
                drawerLayout.closeDrawer(listViewSliding);
            }
        });

        actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_opened, R.string.drawer_closed){

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                invalidateOptionsMenu();
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                invalidateOptionsMenu();
            }
        };

        drawerLayout.setDrawerListener(actionBarDrawerToggle);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if(actionBarDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        actionBarDrawerToggle.syncState();
    }

    //Create method replace fragment

    private void replaceFragment(int pos) {
        Fragment fragment = null;
        switch (pos) {
            case 0:
                fragment = new emotion();
                break;
            case 1:
                fragment = new process();
                break;
            case 2:
                fragment = new historique();
                break;
            case 3:
                fragment = new parametre();
                break;
            case 4:
                fragment = new profile();
                break;
            case 5:
                fragment = new coach();
                break;
            default:
                fragment = new emotion();
                break;
        }

        if(null!=fragment) {
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.replace(R.id.main_content, fragment);//accueil.xml  Relativelayout
            transaction.addToBackStack(null);
            transaction.commit();
        }
    }
}

如何改变?

1 个答案:

答案 0 :(得分:0)

导航抽屉中的Listview xml设置为match_parent宽度和高度。这就是textview和imageview不可见的原因。