我自己构建了一个带有recyclerview和汉堡菜单的自定义导航抽屉。我创建了用于处理项目点击的界面以及ItemTouchHelper类,但抽屉项目没有响应点击事件,只是快速关闭抽屉。
我也尝试在适配器中设置click事件但仍然是相同的情况。这是我的代码:
适配器:
public class RecyclerDrawerAdapter extends RecyclerView.Adapter<RecyclerDrawerHolder> {
private final Context context;
private List<DrawerItems> itemsList;
private String decodedImage;
private SharedPreferences imagePrefs;
private final ConnectionDetector detector;
public RecyclerDrawerAdapter(Context context, List<DrawerItems> itemsList) {
this.context = context;
this.itemsList = itemsList;
detector = new ConnectionDetector(context);
}
@Override
public RecyclerDrawerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.drawer_menu_items_layout, parent, false);
return new RecyclerDrawerHolder(view);
}
@Override
public void onBindViewHolder(RecyclerDrawerHolder viewholder, final int position) {
DrawerItems drawerItems = itemsList.get(position);
viewholder.menuTitleTV.setText(drawerItems.getTitleTxt());
if (!detector.isConnected()){
getImageFromPrefs();
} else {
fetchUserImageOnline();
}
Picasso.with(context)
.load(drawerItems.getIconImg())
.into(viewholder.menuIcon);
//viewholder.badgeCountTV.setText(drawerItems.getBadgeCount());
viewholder.badgeCountTV.setVisibility(View.GONE);
viewholder.setClickListener(new RecyclerClickListener() {
@Override
public void onClick(View view, int position) {
switch (position){
case 0:
context.startActivity(new Intent(context, MyProfileActivity.class));
break;
case 1:
context.startActivity(new Intent(context, ChatsActivity.class));
break;
case 2:
context.startActivity(new Intent(context, MyHugsActivity.class));
break;
case 3:
context.startActivity(new Intent(context, ChatCirclesActivity.class));
break;
case 4:
context.startActivity(new Intent(context, SettingsActivity.class));
break;
}
}
@Override
public void onLongClick(View view, int position) {
}
});
}
@Override
public int getItemCount() {
if (itemsList == null) {
return 0;
}
return itemsList.size();
}
private void fetchUserImageOnline() {
}
private void getImageFromPrefs() {
imagePrefs = context.getSharedPreferences("ImagePrefs", Context.MODE_PRIVATE);
decodedImage = imagePrefs.getString("image_user", null);
RecyclerDrawerHolder holder = null;
if (!decodedImage.equalsIgnoreCase("") || !decodedImage.isEmpty()){
byte[] bytes = Base64.decode(decodedImage, Base64.DEFAULT);
Bitmap userBitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
if (userBitmap != null){
holder.menuIcon.setImageBitmap(userBitmap);
} else {
holder.menuIcon.setImageDrawable(context.getResources().getDrawable(R.drawable.person_avatar));
}
}
}
}
我的活动代码,用于设置recyclerview和处理点击事件:
private void setUpDrawer() {
recyclerDrawer.setHasFixedSize(true);
recyclerDrawer.setLayoutManager(new LinearLayoutManager(this));
//DrawerItems drawerItems = new DrawerItems(R.drawable.send_round, "Profile", 99);
DrawerItems drawerItems = new DrawerItems(R.drawable.send_round, "Profile", 99);
itemsList.add(drawerItems);
DrawerItems drawerItems2 = new DrawerItems(R.drawable.chats, "Chats", 80);
itemsList.add(drawerItems2);
DrawerItems drawerItems3 = new DrawerItems(R.drawable.hugs, "Hugs",90);
itemsList.add(drawerItems3);
DrawerItems drawerItems4 = new DrawerItems(R.drawable.chat_circles, "Chat Circles", 67);
itemsList.add(drawerItems4);
DrawerItems drawerItems5 = new DrawerItems(R.drawable.settings, "Settings", 0);
itemsList.add(drawerItems5);
drawerAdapter = new RecyclerDrawerAdapter(this, itemsList);
recyclerDrawer.setAdapter(drawerAdapter);
toolbar.setNavigationIcon(R.drawable.burger_menu);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
} else {
//setClickListener();
drawerLayout.openDrawer(GravityCompat.START);
}
}
});
setDrawerClickListener();
}
private void setDrawerClickListener() {
recyclerDrawer.addOnItemTouchListener(new RecyclerItemTouchListener(this, recyclerDrawer, new RecyclerClickListener() {
@Override
public void onClick(View view, int position) {
switch (position){
case 0:
Toast.makeText(HomeActivity.this, "Profile Activity", Toast.LENGTH_LONG).show();
break;
case 1:
Toast.makeText(HomeActivity.this, "PersonalChat Activity", Toast.LENGTH_LONG).show();
//drawerLayout.closeDrawer(GravityCompat.START);
break;
case 2:
Toast.makeText(HomeActivity.this, "My Hugs Activity", Toast.LENGTH_LONG).show();
//drawerLayout.closeDrawer(GravityCompat.START);
break;
case 3:
Toast.makeText(HomeActivity.this, "ChatCircles Activity", Toast.LENGTH_LONG).show();
//drawerLayout.closeDrawer(GravityCompat.START);
break;
case 4:
Toast.makeText(HomeActivity.this, "Settings Activity", Toast.LENGTH_LONG).show();
//drawerLayout.closeDrawer(GravityCompat.START);
break;
}
}
@Override
public void onLongClick(View view, int position) {
}
}));
}
private void initWidgets() {
toolbar = findViewById(R.id.toolbar);
drawerLayout = findViewById(R.id.drawerLayout);
displayMsgsRV = findViewById(R.id.home_messagesRV);
recyclerDrawer = findViewById(R.id.recyclerDrawer);
home_inputMsgTET = findViewById(R.id.home_inputMsgTET);
send_msg_iv = findViewById(R.id.send_msg_iv);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("");
setUpDrawer();
send_msg_iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendMessage();
}
});
//getPublicMessages();
}
这是我建议的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerDrawer"
android:layout_width="150dp"
android:layout_height="match_parent"
android:background="@android:color/black" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar"
android:id="@+id/containerFrag"/>
</android.support.v4.widget.DrawerLayout>
DrawerItems是我的模型类。请大家帮忙,我似乎不知道为什么会这样。感谢。