导航抽屉列表适配器

时间:2018-10-26 22:04:47

标签: java

我要将您添加到我的无限项中,但是我不能,因为当项数太大时,程序崩溃,并且项被移动并且列表不是无限的。我很高兴能帮助您公共类NavDrawerListAdapter扩展BaseAdapter {

    private Context context;
    private ArrayList<NavDrawerItem> navDrawerItems;

    public NavDrawerListAdapter(Context context, ArrayList<NavDrawerItem> navDrawerItems){
        this.context = context;
        this.navDrawerItems = navDrawerItems;

    }

    @Override
    public int getCount() {
        return navDrawerItems.size();
    }

    @Override
    public Object getItem(int position) {
        return navDrawerItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

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

    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater)
                context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        if (position == 0) {
            convertView = mInflater.inflate(R.layout.drawer_list_item_head, null);
        }

        else if (position == 7) {
            convertView = mInflater.inflate(R.layout.drawer_list_item_divider, null);
        }
        else if (position == 9) {
            convertView = mInflater.inflate(R.layout.drawer_list_item_divider, null);
        } else if (position == 11) {
            convertView = mInflater.inflate(R.layout.drawer_list_item_divider, null);
        }
        else if (position == 13) {
            convertView = mInflater.inflate(R.layout.drawer_list_item_divider, null);
        }
        else if (position == 15) {
            convertView = mInflater.inflate(R.layout.drawer_list_item_divider, null);
        }

        else {
            if (!Settings_Activity.loadNightMode(context)) {
                convertView = mInflater.inflate(R.layout.drawer_list_item, null);
            } else {
                convertView = mInflater.inflate(R.layout.drawer_list_item_dark, null);
            }

        }
    }

    if (position !=0& position !=7& position !=9& position !=11& position !=13& position !=15) {
        ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon);
        TextViewPlus txtTitle = (TextViewPlus) convertView.findViewById(R.id.drawertitle);

        imgIcon.setImageResource(navDrawerItems.get(position).getIcon());
        txtTitle.setText(navDrawerItems.get(position).getTitle());
    }


    return convertView;
}

公共类NavConstructor {

public static int which_act;
public static Context cc;
public static Activity ac;
public static ListView _mDrawerList;
public static DrawerLayout _mDrawerLayout;
public static int defpos;
public static String[] _navMenuTitles;
public static TypedArray _navMenuIcons;
public static ArrayList<NavDrawerItem> _navDrawerItems;
public static NavDrawerListAdapter _adapter;
public static boolean intenthappennav = false;

@SuppressWarnings("ResourceType")
public static void Build(Context context, ListView mDrawerList,
                         DrawerLayout mDrawerLayout, int DEFAULT_POSITION) {

    _mDrawerList = mDrawerList;
    _mDrawerLayout = mDrawerLayout;
    cc = context;
    ac = (Activity) context;
    defpos = DEFAULT_POSITION;
    intenthappennav = false;
    _navMenuTitles = context.getResources().getStringArray(R.array.nav_drawer_items);

    // nav drawer icons from resources
    _navMenuIcons = context.getResources()
            .obtainTypedArray(R.array.nav_drawer_icons_dark);

    _navDrawerItems = new ArrayList<NavDrawerItem>();
    _navDrawerItems.add(new NavDrawerItem());
    _navDrawerItems.add(new NavDrawerItem(_navMenuTitles[1], _navMenuIcons.getResourceId(1, -1)));
    _navDrawerItems.add(new NavDrawerItem(_navMenuTitles[2], _navMenuIcons.getResourceId(2, -1)));
    _navDrawerItems.add(new NavDrawerItem(_navMenuTitles[3], _navMenuIcons.getResourceId(3, -1)));
    _navDrawerItems.add(new NavDrawerItem(_navMenuTitles[4], _navMenuIcons.getResourceId(4, -1)));
    _navDrawerItems.add(new NavDrawerItem(_navMenuTitles[5], _navMenuIcons.getResourceId(5, -1)));
    _navDrawerItems.add(new NavDrawerItem(_navMenuTitles[6], _navMenuIcons.getResourceId(6, -1)));
    _navDrawerItems.add(new NavDrawerItem());
    _navDrawerItems.add(new NavDrawerItem(_navMenuTitles[7], _navMenuIcons.getResourceId(7, -1)));
    _navDrawerItems.add(new NavDrawerItem());
    _navDrawerItems.add(new NavDrawerItem(_navMenuTitles[8], _navMenuIcons.getResourceId(8, -1)));
    _navDrawerItems.add(new NavDrawerItem());
    _navDrawerItems.add(new NavDrawerItem(_navMenuTitles[9], _navMenuIcons.getResourceId(9, -1)));
    _navDrawerItems.add(new NavDrawerItem());
    _navDrawerItems.add(new NavDrawerItem(_navMenuTitles[10], _navMenuIcons.getResourceId(10, -1)));
    _navDrawerItems.add(new NavDrawerItem());
    _navDrawerItems.add(new NavDrawerItem(_navMenuTitles[11], _navMenuIcons.getResourceId(11, -1)));

    _navDrawerItems.add(new NavDrawerItem(_navMenuTitles[12], _navMenuIcons.getResourceId(12, -1)));

    // Recycle the typed array

    _navMenuIcons.recycle();

    // setting the nav drawer list adapter
    _adapter = new NavDrawerListAdapter(context.getApplicationContext(), _navDrawerItems);
    mDrawerList.setAdapter(_adapter);

    mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
}


public static class SlideMenuClickListener implements
        ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
                            long id) {
        displayView(position);
    }
}

public static void displayView(int position) {
    switch (position) {

        case 1:
            which_act = 1;
            break;
        case 2:
            which_act = 2;
            break;
        case 3:
            which_act = 3;
            break;
        case 4:
            which_act = 4;
            break;
        case 5:
            which_act = 5;
            break;
        case 6:
            which_act = 6;
            break;
        case 7:
            which_act = 7;
            break;
        case 8:
            which_act = 8;
            break;
        case 9:
            which_act = 9;
            break;
        case 10:
            which_act = 10;
            break;
        case 11:
            which_act = 11;
            break;
        case 12:
            which_act = 12;
            break;
        case 13:
            which_act = 13;
            break;
        case 14:
            which_act = 14;
            break;
        case 15:
            which_act = 15;
            break;
        case 16:
            which_act = 16;
            break;
        case 17:
            which_act = 17;
            break;


        default:
            break;
    }
    if (position != 0) {

        _mDrawerList.setItemChecked(position, true);
        _mDrawerList.setSelection(position);
        _mDrawerLayout.closeDrawer(_mDrawerList);
    } else {
        _mDrawerList.setItemChecked(defpos, true);
        _mDrawerList.setSelection(defpos);
    }

    NavigationClosed();
}



public static void NavigationClosed() {

    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            switch (which_act) {
                case 0:

                    break;

                case 1: //main menu
                    if (defpos != 1) {
                        intenthappennav = true;
                        Intent i1 = new Intent(cc, MainMenu.class);
                        ac.startActivity(i1);
                        ac.overridePendingTransition(R.anim.fadein, R.anim.fadeout);
                        //ac.finish();
                    }
                    break;
                case 2: //content
                    if (defpos != 2) {
                        intenthappennav = true;
                        Intent i2;
                        if (MainMenu.tabnum == 1) {
                            i2 = new Intent(cc, ContentMenu.class);
                        } else {
                            i2 = new Intent(cc, SeasonMenu.class);
                        }
                        ac.startActivity(i2);
                        ac.overridePendingTransition(R.anim.fadein, R.anim.fadeout);
                        //ac.finish();
                    }
                    break;
                case 3: // favorite
                    if (defpos != 3) {
                        intenthappennav = true;
                        Intent i3 = new Intent(cc, Favorites.class);
                        ac.startActivity(i3);
                        ac.overridePendingTransition(R.anim.fadein, R.anim.fadeout);
                        //ac.finish();
                    }

                    break;
                case 4: // search
                    if (defpos != 4) {
                        intenthappennav = true;
                        Intent i4 = new Intent(cc, Search_all.class);
                        i4.putExtra("nav", true);
                        i4.putExtra("defpos", defpos);
                        ac.startActivity(i4);
                        ac.overridePendingTransition(R.anim.fadein, R.anim.fadeout);
                    }

                    break;

                case 5: //settings
                    if (defpos != 5) {
                        intenthappennav = true;
                        Intent i6 = new Intent(cc, Settings_Activity.class);
                        ac.startActivity(i6);
                        ac.overridePendingTransition(R.anim.fadein, R.anim.fadeout);
                        //ac.finish();
                    }

                    break;

                case 6:  //aboutus
                    if (defpos != 6) {
                        intenthappennav = true;
                        Intent i7 = new Intent(cc, AboutUs.class);
                        ac.startActivity(i7);
                        ac.overridePendingTransition(R.anim.fadein, R.anim.fadeout);
                        //ac.finish();
                    }
                    break;
                case 7:

                    break;
                case 8:  //aboutus
                    if (defpos != 8) {
                        intenthappennav = true;
                        Intent i9 = new Intent(cc, WebView1.class);
                        ac.startActivity(i9);
                        ac.overridePendingTransition(R.anim.fadein, R.anim.fadeout);
                        //ac.finish();
                    }
                    break;
                case 9:

                    break;
                case 10:  //aboutus
                    if (defpos != 10) {
                        intenthappennav = true;
                        Intent i9 = new Intent(cc, WebView2.class);
                        ac.startActivity(i9);
                        ac.overridePendingTransition(R.anim.fadein, R.anim.fadeout);
                        //ac.finish();
                    }
                    break;
                case 11:

                    break;
                case 12:  //aboutus
                    if (defpos != 12) {
                        intenthappennav = true;
                        Intent i9 = new Intent(cc, WebView3.class);
                        ac.startActivity(i9);
                        ac.overridePendingTransition(R.anim.fadein, R.anim.fadeout);
                        //ac.finish();
                    }
                    break;
                case 13:

                    break;
                case 14:  //aboutus
                    if (defpos != 14) {
                        intenthappennav = true;
                        Intent i9 = new Intent(cc, WebView4.class);
                        ac.startActivity(i9);
                        ac.overridePendingTransition(R.anim.fadein, R.anim.fadeout);
                        //ac.finish();
                    }
                    break;
                case 15:

                    break;
                case 16:  //aboutus
                    if (defpos != 16) {
                        intenthappennav = true;
                        Intent i9 = new Intent(cc, WebView5.class);
                        ac.startActivity(i9);
                        ac.overridePendingTransition(R.anim.fadein, R.anim.fadeout);
                        //ac.finish();
                    }
                    break;

                case 17:  //aboutus
                    if (defpos != 17) {
                        intenthappennav = true;
                        Intent i9 = new Intent(cc, WebView6.class);
                        ac.startActivity(i9);
                        ac.overridePendingTransition(R.anim.fadein, R.anim.fadeout);
                        //ac.finish();
                    }
                    break;


            }

        }
    }, 250);


}

}

公共类NavDrawerItem {

private String title;
private int icon;



public NavDrawerItem(String title, int icon){
    this.title = title;
    this.icon = icon;
}

public NavDrawerItem(){

}


public String getTitle(){
    return this.title;
}

public int getIcon(){
    return this.icon;
}

}

1 个答案:

答案 0 :(得分:1)

这是一个列表视图的示例。由于以下代码holder.fill(this,item,position);

public class AdapterNote extends ArrayAdapter<StructNote> {

    public AdapterNote(ArrayList<StructNote> array) {
        super(G.context, R.layout.adapter_notes, array);
    }


    private static class ViewHolder {

        public ViewGroup layoutRoot;
        public TextView  txtTitle;
        public TextView  txtDescription;
        public CheckBox  chkDone;
        public ImageView imgDelete;


        public ViewHolder(View view) {
            txtTitle = (TextView) view.findViewById(R.id.txtTitle);
            txtDescription = (TextView) view.findViewById(R.id.txtDescription);
            chkDone = (CheckBox) view.findViewById(R.id.chkDone);
            layoutRoot = (ViewGroup) view.findViewById(R.id.layoutRoot);
            imgDelete = (ImageView) view.findViewById(R.id.imgDelete);
        }


        public void fill(final ArrayAdapter<StructNote> adapter, final StructNote item, final int position) {
            txtTitle.setText(item.title);
            txtDescription.setText(item.description);
            chkDone.setChecked(item.done);

            imgDelete.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    adapter.remove(item);
                }
            });
            layoutRoot.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    Intent intent = new Intent(G.currentActivity, ActivityEdit.class);
                    intent.putExtra("POSITION", position);
                    G.currentActivity.startActivity(intent);
                }
            });
            chkDone.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View view) {
                    CheckBox checkBox = (CheckBox) view;
                    item.done = checkBox.isChecked();

                    if (item.done) {
                        txtTitle.setTextColor(Color.RED);
                    } else {
                        txtTitle.setTextColor(Color.WHITE);
                    }
                }
            });

            if (item.done) {
                txtTitle.setTextColor(Color.RED);
            } else {
                txtTitle.setTextColor(Color.WHITE);
            }
        }
    }


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

        StructNote item = getItem(position);
        if (convertView == null) {
            convertView = G.inflater.inflate(R.layout.adapter_notes, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.fill(this, item, position);
        return convertView;
    }
}