类别

时间:2015-04-24 12:21:02

标签: android navigation-drawer android-custom-view

如何修改此代码并添加横向菜单类别?

其实我有:

  • 第1页
  • 第2页
  • 第3页

我想要获得的是:

  • 第1页
  • CATEGORY1
  • 第2页
  • 第3页
  • 第4页
  • CATEGORY2
  • 第2页
  • 第3页

MainActivity代码:

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public class MainActivity extends Activity {
 private List<News> newsList = new ArrayList<News>();
 private ListView listView;
 private CustomListNews adapter;

 private String[] mNavigationDrawerItemTitles;
 private DrawerLayout mDrawerLayout;
 private ListView mDrawerList;

 private ActionBarDrawerToggle mDrawerToggle;
 private CharSequence mDrawerTitle;
 private CharSequence mTitle;


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




MY APPLICATION CODE






  mTitle = mDrawerTitle = getTitle();
  mNavigationDrawerItemTitles = getResources().getStringArray(R.array.navigation_drawer_items_array);
  mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
  mDrawerList = (ListView) findViewById(R.id.left_drawer);

  mDrawerList.setOnItemClickListener(new OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
    Bundle extras = new Bundle();
    Intent intent = new Intent(MainActivity.this, ChannelList.class);
    Intent intent1 = new Intent(MainActivity.this, Redazione.class);
    Intent intent2 = new Intent(MainActivity.this, VideoGallery.class);

    switch (pos) {
     //Restart prima pagina
     case 0:
     restart();
     break;

     //Start zone
     case 1:
     extras.putString("Channel","canale1");
     intent.putExtras(extras);
     startActivity(intent);
     break;

     //Start argomenti
     case 2:
     extras.putString("Channel","canale2");
     intent.putExtras(extras);
     startActivity(intent);
     break;

     //Start multimedia
     case 3:
     startActivity(intent2);
     break;

     //Start redazione
     case 4:
     startActivity(intent1);
     break;

     //Start redazione
     case 5:
     extras.putString("Channel","rubriche");
     intent.putExtras(extras);
     startActivity(intent);
     break;

     default:
     break;
    }
   }
  });

  mDrawerList.bringToFront();
  mDrawerLayout.requestLayout();

  ObjectDrawerItem[] drawerItem = new ObjectDrawerItem[6];

  drawerItem[0] = new ObjectDrawerItem(R.drawable.ic_action_refresh, "Prima pagina");
  drawerItem[1] = new ObjectDrawerItem(R.drawable.ic_action_share, "Zone");
  drawerItem[2] = new ObjectDrawerItem(R.drawable.ic_action_copy, "Argomenti");
  drawerItem[3] = new ObjectDrawerItem(R.drawable.ic_action_copy, "Multimedia");
  drawerItem[4] = new ObjectDrawerItem(R.drawable.ic_action_share, "Reporter");
  drawerItem[5] = new ObjectDrawerItem(R.drawable.ic_action_copy, "Rubriche");

  DrawerItemCustomAdapter adapter = new DrawerItemCustomAdapter(this, R.layout.listview_item_row, drawerItem);
  mDrawerList.setAdapter(adapter);
  mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
  mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {

   public void onDrawerClosed(View view) {
    invalidateOptionsMenu();
   }

   public void onDrawerOpened(View drawerView) {
    invalidateOptionsMenu();
   }
  };

  mDrawerLayout.setDrawerListener(mDrawerToggle);
 }

 /******************************************************************************************************/

 public void connectivityMessage(String msg){
  AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
  alertDialog.setTitle("Nessuna connessione presente");
  alertDialog.setMessage(msg);
  alertDialog.setPositiveButton("Riprova", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog,int which) {
    restart();
   }
  });

  alertDialog.setNegativeButton("Esci", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int which) {
    onDestroy();
   }
  });

  alertDialog.show();
 }

 @Override
 public void onBackPressed() {
  AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
  alertDialog.setTitle("Conferma uscita");
  alertDialog.setMessage("Sei sicuro di voler uscire ??");
  alertDialog.setPositiveButton("Si", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog,int which) {
    finish();
   }
  });

  alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int which) {
    dialog.cancel();
   }
  });
  alertDialog.show();
 }

 public void change(){
  ChangeLog cl = new ChangeLog(this);
  if (cl.firstRun())
   cl.getLogDialog().show();
 }

 @Override
 public void onStart() {
  super.onStart();
  Tracker tracker = GoogleAnalytics.getInstance(this).getTracker("UA-49974758-1");
  tracker.set(Fields.SCREEN_NAME, "Visualizzazione prima pagina");
  tracker.send(MapBuilder.createAppView().set(Fields.customDimension(1), "Premium").build());
 }

 @Override
 public void onDestroy() {
  super.onDestroy();
 }

 @Override
 public void onPause() {
  super.onPause();
 }

 public void restart(){
  finish();
  startActivity(getIntent());
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
     int itemid = item.getItemId();
  if (mDrawerToggle.onOptionsItemSelected(item)) {
   return true;
  }if (itemid == R.id.meteo){
    Intent intent = new Intent(this, Meteo.class);
    startActivity(intent);  
  }
  return super.onOptionsItemSelected(item);  
 }

 @Override
 public boolean onPrepareOptionsMenu(Menu menu) {
  return super.onPrepareOptionsMenu(menu);
 }

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

 @Override
 public void setTitle(CharSequence title) {
  mTitle = title;
  getActionBar().setTitle(mTitle);
 }

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

 @Override
 public void onConfigurationChanged(Configuration newConfig) {
  super.onConfigurationChanged(newConfig);
  mDrawerToggle.onConfigurationChanged(newConfig);
 }
}

我的抽屉适配器

package com.alessandrianews.utility;


public class DrawerItemCustomAdapter extends ArrayAdapter<ObjectDrawerItem> {

    Context mContext;
    int layoutResourceId;
    ObjectDrawerItem data[] = null;

    /*
     * @mContext - app context
     * 
     * @layoutResourceId - the listview_item_row.xml
     * 
     * @data - the ListItem data
     */
    public DrawerItemCustomAdapter(Context mContext, int layoutResourceId, ObjectDrawerItem[] data) {

        super(mContext, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.mContext = mContext;
        this.data = data;
    }

    /*
     * @We'll overried the getView method which is called for every ListItem we
     * have.
     * 
     * @There are lots of different caching techniques for Android ListView to
     * achieve better performace especially if you are going to have a very long
     * ListView.
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View listItem = convertView;

        // inflate the listview_item_row.xml parent
        LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
        listItem = inflater.inflate(layoutResourceId, parent, false);

        // get the elements in the layout

        ImageView imageViewIcon = (ImageView) listItem.findViewById(R.id.imageViewIcon);
        TextView textViewName = (TextView) listItem.findViewById(R.id.textViewName);


        /*
         * Set the data for the list item. You can also set tags here if you
         * want.
         */
        ObjectDrawerItem folder = data[position];


        imageViewIcon.setImageResource(folder.icon);
        textViewName.setText(folder.name);

        return listItem;
    }

}

package com.alessandrianews.utility;

对象抽屉项目

    public int icon;
    public String name;

    // Constructor.
    public ObjectDrawerItem(int icon, String name) {

        this.icon = icon;
        this.name = name;
    }
}

这是我的布局: My layout

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以在DrawerItemCustomAdapter中添加一个标记,例如&#34; isHeader&#34;。然后,您必须在适配器的数据阵列中添加标头。

在您的适配器中,您可以覆盖getItemViewType() - 如下所示的方法:

@Override
public int getItemViewType(int position) {
    return data[position].isHeader() ? 0 : 1;
}

因此,您有两种不同类型的可能视图。

在你的getView中,你要求每个项目,它是什么类型,然后你可以膨胀另一个布局,做不同的东西。

编辑: 并请开始谷歌搜索Android视图持有人! 使用viewHolder,您可以回收您的视图。您的代码会一次又一次地为每个项目的布局充气。使用viewHolder,您可以根据需要拥有如此多的视图!