我正在尝试使用导航抽屉添加网格视图,但我的导航抽屉在打开后没有响应触摸。我无法访问onNavigationItemSelected方法中的任何项目。我也使用网格视图,抽屉工作得更早。你能看一下吗?
public static ViewHolder listViewHolder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.bringToFront();
GridView gridview = (GridView) findViewById(R.id.gridview);
List<ItemObject> allItems = getAllItemObject();
CustomAdapter customAdapter = new CustomAdapter(MainActivity.this, allItems);
gridview.setAdapter(customAdapter);
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent myIntent = new Intent(MainActivity.this, Game.class);
myIntent.putExtra("position",position);
startActivity(myIntent);
// Toast.makeText(MainActivity.this, "Position: " + position, Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.Profile) {
Intent I = new Intent(MainActivity.this,userprofile.class);
startActivity(I);
// Handle the camera action
} else if (id == R.id.share) {
} else if (id == R.id.support) {
} else if (id == R.id.aboutus) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.krashunt.com"));
startActivity(browserIntent);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private List<ItemObject> getAllItemObject(){
ItemObject itemObject = null;
List<ItemObject> items = new ArrayList<>();
items.add(new ItemObject("Amazon Echo Dot", "https://firebasestorage.googleapis.com/v0/b/krashunt-8ebf7.appspot.com/o/imageedit_1_8004680559.png?alt=media&token=8cf09ac9-0f98-46d8-a827-d6748fe8d2c8"));
items.add(new ItemObject("LeEco Bluetooth Speaker", "https://firebasestorage.googleapis.com/v0/b/krashunt-8ebf7.appspot.com/o/imageedit_1_7949050176.png?alt=media&token=794bab46-0293-45a0-8b19-298b137ab1ef"));
items.add(new ItemObject("Samsung Charm Lifestyle Fitness Smart Band", "https://firebasestorage.googleapis.com/v0/b/krashunt-8ebf7.appspot.com/o/imageedit_1_7185715928.png?alt=media&token=2040a5b8-5955-4634-98e9-a23aefd00fb7"));
items.add(new ItemObject("Skullcandy Grind On-Ear with Tap Tech", "https://firebasestorage.googleapis.com/v0/b/krashunt-8ebf7.appspot.com/o/imageedit_1_7440771859.png?alt=media&token=80c52411-47df-4cfb-9119-9675c9afd85d"));
items.add(new ItemObject("Samsung Gear VR SM-R323 ", "https://firebasestorage.googleapis.com/v0/b/krashunt-8ebf7.appspot.com/o/imageedit_1_2950042441.png?alt=media&token=e8c3d994-7c15-42da-9820-a26cdfafb688"));
return items;
}
public static class ViewHolder {
TextView textInListView;
ImageView imageInListView;
}
public class CustomAdapter extends BaseAdapter {
private LayoutInflater layoutinflater;
private List<ItemObject> listStorage;
private Context context;
public CustomAdapter(Context context, List<ItemObject> customizedListView) {
this.context = context;
layoutinflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
listStorage = customizedListView;
}
@Override
public int getCount() {
return listStorage.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
listViewHolder = new ViewHolder();
convertView = layoutinflater.inflate(R.layout.listview_with_text_image, parent, false);
listViewHolder.textInListView = (TextView)convertView.findViewById(R.id.textView);
listViewHolder.imageInListView = (ImageView)convertView.findViewById(R.id.gridimageView);
convertView.setTag(listViewHolder);
}else{
listViewHolder = (ViewHolder)convertView.getTag();
}
listViewHolder.textInListView.setText(listStorage.get(position).getContent());
Glide.with(MainActivity.this)
.load(listStorage.get(position).getImageResource())
.fitCenter()
.into(listViewHolder.imageInListView);
return convertView;
}
}
这是我的main.xml文件
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:visibility="invisible"
app:headerLayout="@layout/nav_header_main2"
app:menu="@menu/activity_main2_drawer" />
这是我的content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="80dp"
android:layout_marginTop="50dp"
android:gravity="center"
android:numColumns="2"
android:stretchMode="columnWidth"
/>`