我希望导航抽屉显示单击按钮,因此我使用以下代码。我在自定义适配器中处理单击事件,但是当单击列表中的任何对象时,抽屉不会在任何时间点关闭。知道怎么解决这个问题吗? 另一个问题是我只能从屏幕的底部到中间绘制导航抽屉。我该如何解决这个问题? 在我的活动中
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(android.R.id.list);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
mDrawerLayout.closeDrawer(Gravity.RIGHT);
} else {
mDrawerLayout.openDrawer(Gravity.RIGHT);
}
}
});
String[] values = new String[] { "1:","2","","3"};
MySimpleArrayAdapterUhr adapter = new MySimpleArrayAdapterUhr(this, values);
setListAdapter(adapter);
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Closing the drawer
mDrawerLayout.closeDrawers();
}
});
我的适配器:
public class MySimpleArrayAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] names;
static class ViewHolder {
public TextView text;
public ImageView image;
public LinearLayout full;
}
public MySimpleArrayAdapter(Activity context, String[] names) {
super(context, R.layout.drawer_list_item, names);
this.context = context;
this.names = names;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
// reuse views
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.drawer_list_item, null);
// configure view holder
ViewHolder viewHolder = new ViewHolder();
viewHolder.full = (LinearLayout) rowView.findViewById(R.id.full);
viewHolder.text = (Button) rowView.findViewById(R.id.label);
viewHolder.image = (ImageView) rowView.findViewById(R.id.image);
rowView.setTag(viewHolder);
}
// fill data
ViewHolder holder = (ViewHolder) rowView.getTag();
String s = names[position];
holder.text.setText(s);
if (s.equals("1")) {
holder.text.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
v.invalidate();
break;
}
case MotionEvent.ACTION_UP:
v.playSoundEffect(android.view.SoundEffectConstants.CLICK);
Intent intent = new Intent(context, activity_einstellungen_ereignis_erstellen.class);
v.getContext().startActivity(intent);
case MotionEvent.ACTION_CANCEL: {
Button view = (Button) v;
view.invalidate();
break;
}
}
return true;
});
}
return rowView;
}
}
答案 0 :(得分:0)
try this code
declare variable as private ActionBarDrawerToggle mDrawerToggle;
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(android.R.id.list);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
mDrawerLayout.closeDrawer(Gravity.RIGHT);
} else {
mDrawerLayout.openDrawer(Gravity.RIGHT);
}
}
});
String[] values = new String[] { "1:","2","","3"};
MySimpleArrayAdapterUhr adapter = new MySimpleArrayAdapterUhr(this, values);
setListAdapter(adapter);
// enabling action bar app icon and behaving it as toggle button
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, //nav menu toggle icon
R.string.app_name, // nav drawer open - description for accessibility
R.string.app_name // nav drawer close - description for accessibility
){
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
if (savedInstanceState == null) {
// on first time display view for first nav item
displayView(0);
} <br>
此链接click here