自定义ListView菜单的基本活动

时间:2018-03-01 07:45:49

标签: android listview menu base-class

我是Android编程的新手。我正在开发一个有多个活动的应用程序。我已经使用ListView创建了一个自定义菜单。我想将此菜单放在基本活动中,以便在所有活动中使用。我该怎么做?

直到现在,我有这样的事情:

这是用于切换菜单的按钮

menuToggelIcon.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {
    // Hide layouts if VISIBLE
    if(menuLayout.getVisibility() == View.VISIBLE)
    {
      menuLayout.setVisibility(View.GONE);
    }
    // Show layouts if they're not VISIBLE
    else
    {
      menuLayout.setVisibility(View.VISIBLE);
    }
  }
});

这是菜单

menuListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    String name = menuArray[position];

    Context context = getApplicationContext();

    switch (name) {
      case "CASE1":
        Intent case1Intent = new Intent(context, Activity1.class);
        startActivity(case1Intent);
        break;
      case "CASE2":
        Intent case2Intent = new Intent(context, Activity2.class);
        startActivity(case2Intent);
        break;
      case "CASE3":
        Intent case3Intent = new Intent(context, Activity3.class);
        startActivity(case3Intent);
        break;
      case "CASE4":
        Intent case4Intent = new Intent(context, Activity4.class);
        startActivity(case4Intent);
        break;
      case "CASE5":
        Intent case5Intent = new Intent(context, Activity5.class);
        startActivity(case5Intent);
        break;
      case "CASE6":
        Intent case6Intent = new Intent(context, Activity6.class);
        startActivity(case6Intent);
        break;
      case "CASE7":
        Intent case7Intent = new Intent(context, Activity7.class);
        startActivity(case7Intent);
        break;
      default:
        break;
    }
  }
});

Android custom menu

1 个答案:

答案 0 :(得分:0)

制作一个BaseActivity类,所有活动都由BasyActivity类扩展。 BaseActivity类定义显示所有屏幕的主要内容,如菜单和其他内容。例如

public class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.manu_file_name, menu);


    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.icon) {
        Toast.makeText(getApplicationContext(), "Hello World", 0).show();
    }
    return super.onOptionsItemSelected(item);
}

}

此活动扩展了所有其他活动。