【问题标题】:NavigationDrawer is not working?导航抽屉不工作?
【发布时间】:2017-03-07 18:22:34
【问题描述】:

程序在我运行后没有响应,没有编译时错误。
欲了解更多信息,请查看以下链接
https://www.youtube.com/watch?v=rs4LW3GxOgE

public class MainActivity extends AppCompatActivity  {

private DrawerLayout mDrawerLayout;
 ListView mListView;
private ActionBarDrawerToggle toogle;
private custome_adapter adp;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    adp = new custome_adapter(this);
    mListView.setAdapter(adp);

    mDrawerLayout = (DrawerLayout)findViewById(R.id.drawerlayout);
    mListView = (ListView)findViewById(R.id.list);


    toogle = new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close){

        @Override
        public void onDrawerOpened(View drawerView) {
            Toast.makeText(MainActivity.this,"opening the Scroll",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            Toast.makeText(MainActivity.this,"closing the scroll",Toast.LENGTH_SHORT).show();

        }
    };

    mDrawerLayout.setDrawerListener(toogle);

    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}



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

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


@Override
public boolean onOptionsItemSelected(MenuItem item) {
   if(toogle.onOptionsItemSelected(item))
   {
       return true;
   }
    return super.onOptionsItemSelected(item);
}




}





class custome_adapter extends BaseAdapter


{

private Context context;

 String[] week;

int[] images = {
        R.drawable.a,
        R.drawable.b,
        R.drawable.c,
        R.drawable.d,
        R.drawable.e

};

public custome_adapter(Context context)
{
    this.context = context;
   week= context.getResources().getStringArray(R.array.week);
}
@Override
public int getCount() {
    return week.length;
}

@Override
public Object getItem(int position) {
    return week[position];
}

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

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

    View row =null;
    if(convertView==null)
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.custome_row, parent, false);

    }
    else{

        row = convertView;
    }
    ImageView imageView = (ImageView)row.findViewById(R.id.imageView);
    TextView textView = (TextView)row.findViewById(R.id.textView);
    imageView.setImageResource(images[position]);
    textView.setText(week[position]);

    return row;
}
}

【问题讨论】:

  • 检查所有与此相关的答案,否则请从其他网站获取任何教程。

标签: android navigation navigation-drawer


【解决方案1】:

如果你想要一个不费吹灰之力的导航抽屉,Android Studio里面有一个模板,你只需要用导航抽屉启动项目,做到这一点如下:

1.点击文件

2.然后新建 > 新建项目

3.然后点击下一步直到到达 向移动设备添加一个活动

  1. 然后单击下一步并完成

【讨论】:

  • 但我是初学者,我想先学习,然后我会尝试模板
  • 是的,确实如此,但对于行业工作来说,时间有限,我们有时倾向于使用模板。即使你创建了这个模板,你也会有想法给你一个答案,因为那个视频正在使用 ListView 并且 ListView 被 RecyclerView 取代,所以很快你就会恢复到这个
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-23
  • 1970-01-01
相关资源
最近更新 更多