如何将配置文件信息添加到android中的左侧导航抽屉

时间:2014-10-16 11:55:57

标签: android

我已经完成的大部分工作,但实际上我需要在Android左侧抽屉导航中显示用户配置文件详细信息和小图像。我有带图标的菜单列表,但它是简单的链接列表。我还想在菜单顶部用samll图像显示登录的用户详细信息。

就像这样,请查看此链接

http://www.androidhive.info/wp-content/uploads/2013/11/sliding-menu-example-applications.jpg?084cc0

这是我的代码

Adapter.java

public class MyAdapter extends ArrayAdapter<Model> {

        private final Context context;
        private final ArrayList<Model> modelsArrayList;

        public MyAdapter(Context context, ArrayList<Model> modelsArrayList) {

            super(context, R.layout.fragment_my, modelsArrayList);
            this.context = context;
            this.modelsArrayList = modelsArrayList;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            // 1. Create inflater
            LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            // 2. Get rowView from inflater

            View rowView = null;
            if(!modelsArrayList.get(position).isGroupHeader()){
                rowView = inflater.inflate(R.layout.fragment_my, parent, false);

                // 3. Get icon,title & counter views from the rowView
                ImageView imgView = (ImageView) rowView.findViewById(R.id.section_icon);
                TextView titleView = (TextView) rowView.findViewById(R.id.section_label);
                TextView counterView = (TextView) rowView.findViewById(R.id.section_counter);

                // 4. Set the text for textView
                imgView.setImageResource(modelsArrayList.get(position).getIcon());
                titleView.setText(modelsArrayList.get(position).getTitle());
                if(modelsArrayList.get(position).getCounter().toString().equals("0")) {
                    counterView.setVisibility(View.GONE);
                } else {
                    counterView.setText(modelsArrayList.get(position).getCounter());
                }
            }
            // 5. retrn rowView
            return rowView;
        }
}

Model.java

public class Model {
    private int icon;
    private String title;
    private String counter;

    private boolean isGroupHeader = false;

    public Model(String title) {
        this(-1,title,null);
        isGroupHeader = true;
    }
    public Model(int icon, String title, String counter) {
        super();
        this.icon = icon;
        this.title = title;
        this.counter = counter;
    }

    public int getIcon() {
        return icon;
    }

    public void setIcon(int icon) {
        this.icon = icon;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getCounter() {
        return counter;
    }

    public void setCounter(String counter) {
        this.counter = counter;
    }

    public boolean isGroupHeader() {
        return isGroupHeader;
    }

    public void setGroupHeader(boolean isGroupHeader) {
        this.isGroupHeader = isGroupHeader;
    }
}

fragement_my.xml

<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="48dp"
    android:background="@drawable/bottom_border_background"
    tools:context=".MyActivity$PlaceholderFragment">

    <ImageView
        android:id="@+id/section_icon"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_alignParentLeft="true"
        android:src="@drawable/ic_home"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"/>

    <TextView
        android:id="@+id/section_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/section_icon"
        android:text="@string/title_section1"
        android:layout_alignBaseline="@+id/section_counter"
        android:textSize="18dp"/>

    <TextView
        android:id="@+id/section_counter"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_alignParentRight="true"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/rectangle"
        android:gravity="center"
        android:textColor="#FFFFFF"
        android:textSize="12sp"
        android:textStyle="bold"
        />

</RelativeLayout>

fragement_navigation_drawer.xml

<ListView 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:choiceMode="singleChoice"
    android:divider="@drawable/drawer_shadow"
    android:dividerHeight="1dp"
    android:headerDividersEnabled="true"
    android:background="#FFFFFF"
    tools:context=".NavigationDrawerFragment" />

请帮助我如何在菜单列表顶部显示用户信息。

由于

0 个答案:

没有答案