这是我的主要活动文件,其中包含抽屉布局。
public abstract class ParentActivity extends AppCompatActivity implements NetworkStateReceiverListener {
private final String TAG = ParentActivity.this.getClass().getSimpleName();
protected ListView mDrawerList;
protected static int position;
private static boolean isLaunch = true;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;
private boolean mSlideState;
private MenuItem drawer_btn;
private RelativeLayout subItem1;
private RelativeLayout subItem2;
private RelativeLayout subItem3;
public NetworkStateReceiver networkStateReceiver = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.navigation_drawer_base_layout);
Utils.registerNetworkStateReceiver(this, networkStateReceiver, this);
frameLayout = (FrameLayout) findViewById(R.id.content_frame);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
drawer_btn = (MenuItem) findViewById(R.id.navigation_drawer_button);
NavigationAdapter adapter = new NavigationAdapter(this);
mDrawerList.setAdapter(adapter);
mDrawerList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
openActivity(position);
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setLogo(R.drawable.logo);
getSupportActionBar().setDisplayUseLogoEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(true);
mDrawerLayout.setFocusableInTouchMode(false);
actionBarDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_launcher,
R.string.open_drawer,
R.string.close_drawer)
@Override
public void onDrawerClosed(View drawerView) {
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
super.onDrawerClosed(drawerView);
mSlideState = false;
}
@Override
public void onDrawerOpened(View drawerView) {
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
mSlideState = true;
super.onDrawerOpened(drawerView);
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
}
@Override
public void onDrawerStateChanged(int newState) {
super.onDrawerStateChanged(newState);
}
};
mDrawerLayout.setDrawerListener(actionBarDrawerToggle);
if (isLaunch) {
isLaunch = false;
openActivity(0);
}
}
protected void openActivity(int position) {
mDrawerLayout.closeDrawer(mDrawerList);
ParentActivity.position = position; //Setting currently selected position in this field so that it will be available in our child activities.
switch (position) {
case 0:
break;
case 1: {
Intent intent = new Intent(this, HotelListScreen.class);
Utils.startActivityClearTop(this, intent);
}
break;
case 2: {
Intent intent = new Intent(this, CallUsScreen.class);
Utils.startActivityClearTop(this, intent);
}
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 8: {
if (ApplicationDataLayer.getInstance().getSelectedHotel() != null) {
Intent intent = new Intent(this, BankDetails.class);
Utils.startActivityClearTop(this, intent);
} else {
dialog = MultipleDialogs.createDialogOneBtnWithoutTitle(this, this.getResources().getString(R.string.pls_select_hotel), new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
break;
default:
break;
}
Toast.makeText(this, "Selected Item Position::" + position, Toast.LENGTH_LONG).show();
}
private Dialog dialog;
private ActionBar actionBar = null;
public void setUpActionBar() {
actionBar = getSupportActionBar();
actionBar.show();
actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.title_bg));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
onBackPressed();
return true;
}
if (item.getItemId() == R.id.navigation_drawer_button) {
if (mSlideState) {
mDrawerLayout.closeDrawer(mDrawerList);
} else
mDrawerLayout.openDrawer(mDrawerList);
}
return false;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
return super.onPrepareOptionsMenu(menu);
}
@Override
public void onBackPressed() {
if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
mDrawerLayout.closeDrawer(mDrawerList);
}
super.onBackPressed();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
protected void onDestroy() {
if (null != networkStateReceiver && null != this) {
Utils.removeNetworkStateReceiverListener(this, networkStateReceiver, this);
}
super.onDestroy();
}
NavigationAdapter.java
public class NavigationAdapter extends BaseAdapter {
private Activity mActivity;
private ArrayList<DrawerHolderPOJO> mDrawerOptions = new ArrayList<DrawerHolderPOJO>();
private ImageView arrow;
private RelativeLayout subItem1;
private RelativeLayout subItem2;
private RelativeLayout subItem3;
private boolean isHelpOpen = false;
public NavigationAdapter(Activity activity) {
this.mActivity = activity;
String[] title = activity.getResources().getStringArray(R.array.drawer_options_string);
String[] subTitleHelp = activity.getResources().getStringArray(R.array.drawer_options_help_string);
TypedArray ar = activity.getResources().obtainTypedArray(R.array.drawer_options_img);
int len = ar.length();
int[] image = new int[len];
for (int i = 0; i < len; i++) {
image[i] = ar.getResourceId(i, 0);
}
ar.recycle();
int noSubcontentLayout = 0;
boolean subcontentPresent = false;
mDrawerOptions.add(new DrawerHolderPOJO(title[0], noSubcontentLayout, image[0], subcontentPresent));
mDrawerOptions.add(new DrawerHolderPOJO(title[1], noSubcontentLayout, image[1], subcontentPresent));
if (!(activity instanceof HotelListScreen || activity instanceof HotelDetailScreen)) {
mDrawerOptions.add(new DrawerHolderPOJO(title[2], noSubcontentLayout, image[2], subcontentPresent));
mDrawerOptions.add(new DrawerHolderPOJO(title[3], noSubcontentLayout, image[3], subcontentPresent));
mDrawerOptions.add(new DrawerHolderPOJO(title[4], noSubcontentLayout, image[4], subcontentPresent));
mDrawerOptions.add(new DrawerHolderPOJO(title[5], noSubcontentLayout, image[5], subcontentPresent));
mDrawerOptions.add(new DrawerHolderPOJO(title[6], noSubcontentLayout, image[6], subcontentPresent));
mDrawerOptions.add(new DrawerHolderPOJO(title[7], noSubcontentLayout, image[7], subcontentPresent));
mDrawerOptions.add(new DrawerHolderPOJO(title[8], noSubcontentLayout, image[8], subcontentPresent));
}
mDrawerOptions.add(new DrawerHolderPOJO(title[9], noSubcontentLayout, image[9], subcontentPresent));
//HELP TODO Need to give noSubcontentLayout
mDrawerOptions.add(new DrawerHolderPOJO(title[10], R.layout.drawer_list_sub_item, image[10], true));
mDrawerOptions.add(new DrawerHolderPOJO(title[11], noSubcontentLayout, image[11], subcontentPresent));
mDrawerOptions.add(new DrawerHolderPOJO(title[12], noSubcontentLayout, image[12], subcontentPresent));
}
private class ViewHolder {
TextView optionName;
ImageView imageOption;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater)
mActivity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.drawer_list_item, null);
holder = new ViewHolder();
holder.optionName = (TextView) convertView.findViewById(R.id.option_name);
holder.imageOption = (ImageView) convertView.findViewById(R.id.option_img);
arrow = (ImageView) convertView.findViewById(R.id.arrow);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
DrawerHolderPOJO rowItem = (DrawerHolderPOJO) getItem(position);
holder.optionName.setText(rowItem.getItemName());
holder.imageOption.setImageResource(rowItem.getItemImage());
if (rowItem.isSubcontent()) {
arrow.setVisibility(View.VISIBLE);
arrow.setTag(convertView);
arrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View cView = (View) v.getTag();
subItem1 = (RelativeLayout) cView.findViewById(R.id.sub_item1);
subItem2 = (RelativeLayout) cView.findViewById(R.id.sub_item2);
subItem3 = (RelativeLayout) cView.findViewById(R.id.sub_item3);
arrow = (ImageView) cView.findViewById(R.id.arrow);
TextView subText1 = (TextView) subItem1.findViewById(R.id.option_name);
TextView subText2 = (TextView) subItem2.findViewById(R.id.option_name);
TextView subText3 = (TextView) subItem3.findViewById(R.id.option_name);
if (!isHelpOpen) {
isHelpOpen = true;
arrow.setImageResource(R.drawable.up_arrow_white);
subText1.setText("Tutorial Video");
subText2.setText("FAQs");
subText3.setText("Feedback");
subItem1.setVisibility(View.VISIBLE);
subItem2.setVisibility(View.VISIBLE);
subItem3.setVisibility(View.VISIBLE);
} else {
isHelpOpen = false;
arrow.setImageResource(R.drawable.down_arrow_white);
subItem1.setVisibility(View.GONE);
subItem2.setVisibility(View.GONE);
subItem3.setVisibility(View.GONE);
}
}
});
}
return convertView;
}
@Override
public int getCount() {
return mDrawerOptions.size();
}
@Override
public Object getItem(int position) {
return mDrawerOptions.get(position);
}
@Override
public long getItemId(int position) {
return mDrawerOptions.indexOf(getItem(position));
}
}
drawer_list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list_item"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="@dimen/hotel_detail_item_padding"
>
<ImageView
android:id="@+id/option_img"
android:layout_width="39dp"
android:layout_height="39dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerInParent="true"
android:layout_marginTop="@dimen/hotel_detail_item_margin_top_bottom"
android:layout_marginBottom="@dimen/hotel_detail_item_margin_top_bottom"
android:layout_marginRight="@dimen/hotel_detail_item_margin_left_right"/>
<ImageView
android:id="@+id/arrow"
android:layout_width="39dp"
android:layout_height="39dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_gravity="right"
android:src="@drawable/down_arrow_white"
android:layout_marginLeft="@dimen/hotel_detail_image_item_margin_left_right"
android:visibility="gone"/>
<TextView
android:id="@+id/option_name"
style="@style/TextFontStyleNavigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_toLeftOf="@id/arrow"
android:layout_toRightOf="@id/option_img"
android:text="Large Text"
android:singleLine="true"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#b3b3b3">
<include
android:id="@+id/sub_item1"
layout="@layout/drawer_list_sub_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<include
android:id="@+id/sub_item2"
layout="@layout/drawer_list_sub_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<include
android:id="@+id/sub_item3"
layout="@layout/drawer_list_sub_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
----------------------子布局--------------------
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="@dimen/hotel_detail_item_padding"
android:visibility="gone">
<TextView
android:id="@+id/option_name"
style="@style/TextFontStyleNavigationSubItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:paddingLeft="@dimen/padding_left_drawer_sub_item"
android:singleLine="true"
android:text="Large Text" />
现在,当打开或单击或关闭导航栏时,我无法单击屏幕上的任何位置。除抽屉以外的主要内容冻结,无法识别任何点击。
答案 0 :(得分:0)
代码工作正常......请检查您的其他相关代码。
签入演示代码。 :)
感谢好榜样。