问题
I want the navigation drawer to show up wenn clicking a button, therefore I use the following code. I handle click events inside the custom adapter, but the drawer does not close at any point in time when clicking any of the objects in my list. Any idea how to fix that? Another problem is that I can draw in the navigation drawer only from the bottom to the middle of the screen. How can I fix that? In my Activity
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();
}
});
My Adapter:
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;
}
}
回答1:
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>
fellow this links click here
来源:https://stackoverflow.com/questions/29022049/navigation-drawer-setonitemclicklistener-not-working