我写了一个管理我的服装导航视图的课程 在这个类中,我们有一个onItemClickListener,每个类都使用这个类 必须实现它来监听项目点击。导航视图加载没有问题,initializeComponents完成其工作,没有任何异常 但当我点击一个项目时,什么都没有。我在这段代码中的错误是什么:
public class NavigationMenuHelper extends ViewGroup implements
View.OnClickListener {
//region Constants
public static final int MENU_SETTINGS = R.id.btnSettings;
public static final int MENU_SUPPORT = R.id.btnSupport;
public static final int MENU_SANDUGH = R.id.btnSandugh;
public static final int MENU_TRANSFER = R.id.btnTransfer;
public static final int MENU_INS_PAYMENT = R.id.btnInsPayment;
public static final int MENU_TUTORIAL = R.id.btnTutorial;
public static final int MENU_LOST_CARD = R.id.btnLostCard;
//endregion
private Activity activity;
private LayoutInflater inflater;
private View container;
private ViewGroup viewGroup;
//region Components
private ImageButton btnSettings;
private ImageButton btnSupport;
private ImageButton btnSandugh;
private ImageButton btnTransfer;
private ImageButton btnInsPayment;
private ImageButton btnTutorial;
private ImageButton btnLostCard;
//endregion
public NavigationMenuHelper(Context context) {
super(context);
activity = (Activity) context;
if (!(activity instanceof OnMenuItemsClickListener))
throw new ClassCastException(activity +
" must implement NavigationMenuHelper.OnMenuItemsClickListener.");
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
container = inflater.inflate(R.layout.navigation_drawer_layout, this,true);
initializeComponents(container);
}
private void initializeComponents(View container) {
// getting view objects
btnSettings = (ImageButton) container.findViewById(R.id.btnSettings);
btnSupport = (ImageButton) container.findViewById(R.id.btnSupport);
btnSandugh = (ImageButton) container.findViewById(R.id.btnSandugh);
btnTransfer = (ImageButton) container.findViewById(R.id.btnTransfer);
btnInsPayment = (ImageButton) container.findViewById(R.id.btnInsPayment);
btnTutorial = (ImageButton) container.findViewById(R.id.btnTutorial);
btnLostCard = (ImageButton) container.findViewById(R.id.btnLostCard);
// set onClickListener
btnSettings.setOnClickListener(this);
btnSupport.setOnClickListener(this);
btnSandugh.setOnClickListener(this);
btnTransfer.setOnClickListener(this);
btnInsPayment.setOnClickListener(this);
btnTutorial.setOnClickListener(this);
btnLostCard.setOnClickListener(this);
}
@Override
public void onClick(View v) {
((OnMenuItemsClickListener) activity).onClickMenuItems(v.getId());
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
public interface OnMenuItemsClickListener {
void onClickMenuItems(int id);
}
}
我的导航布局添加到DrawerLayout 这是我的导航布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/lcGray"
android:layoutDirection="rtl"
android:orientation="vertical"
android:paddingRight="30dp"
android:paddingLeft="35dp"
android:paddingBottom="30dp">
<LinearLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:orientation="vertical">
<ImageButton android:id="@+id/btnSettings"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center"
android:background="@drawable/shape_circle"
android:src="@drawable/ic_settings" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="تنظیمات"
android:textColor="@color/milky"
android:textStyle="bold" />
</LinearLayout>
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_weight="4"
android:columnCount="3"
android:rowCount="2"
android:orientation="horizontal">
<LinearLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:orientation="vertical">
<ImageButton android:id="@+id/btnSupport"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center"
android:background="@drawable/shape_circle"
android:padding="7dp"
android:src="@drawable/ic_support" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="پشتیبانی"
android:textColor="@color/milky"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:orientation="vertical">
<ImageButton android:id="@+id/btnSandugh"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center"
android:background="@drawable/shape_circle"
android:padding="7dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="صندوق المهدی"
android:textColor="@color/milky"
android:textStyle="bold" />
</LinearLayout>
</GridLayout>
<View style="@style/HDivider" />
<GridLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="25dp"
android:layout_weight="4"
android:columnCount="3"
android:orientation="horizontal"
android:rowCount="2">
<LinearLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:orientation="vertical">
<ImageButton android:id="@+id/btnTransfer"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center"
android:background="@null"
android:src="@mipmap/ic_launcher" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="انتقال وجه"
android:textColor="@color/milky"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:orientation="vertical">
<ImageButton android:id="@+id/btnInsPayment"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center"
android:background="@null"
android:src="@drawable/ic_installment" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="پرداخت قسط"
android:textColor="@color/milky"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:orientation="vertical">
<ImageButton android:id="@+id/btnTutorial"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center"
android:background="@null"
android:src="@mipmap/ic_launcher" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="آموزش"
android:textColor="@color/milky"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:orientation="vertical">
<ImageButton
android:id="@+id/btnLostCard"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center"
android:background="@null"
android:src="@mipmap/ic_launcher" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="مفقودی کارت"
android:textColor="@color/milky"
android:textStyle="bold" />
</LinearLayout>
</GridLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableRight="@mipmap/ic_launcher"
android:gravity="center|right"
android:text="صندوق المهدی"
android:textColor="@color/milky" />
</LinearLayout>
我这样使用这个类:
public class MainActivity extends AppCompatActivity implements
NavigationMenuHelper.OnMenuItemsClickListener {
private NavigationMenuHelper navigationMenuHelper;
private void initialization() {
navigationMenuHelper = new NavigationMenuHelper(this);
}
@Override
public void onClickMenuItems(int id) {
String str = "";
switch (id){
case NavigationMenuHelper.MENU_SETTINGS:
str = "Settings";
break;
case NavigationMenuHelper.MENU_SUPPORT:
str = "Support";
break;
case NavigationMenuHelper.MENU_SANDUGH:
str = "Sandugh";
break;
case NavigationMenuHelper.MENU_TRANSFER:
str = "Transfer";
break;
case NavigationMenuHelper.MENU_INS_PAYMENT:
str = "InsPayment";
break;
case NavigationMenuHelper.MENU_TUTORIAL:
str = "Tutorial";
break;
case NavigationMenuHelper.MENU_LOST_CARD:
str = "LostCard";
break;
}
Toast.makeText(this,str,Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:0)
我认为创建NavigationMenuHelper的Activity已经夸大了导航布局,所以我必须使用活动findViweById方法来初始化我的对象。我为导航视图主要LinearLayout设置了一个Id,以检查活动是否正确填充了布局。
<LinearLayout android:id="@+id/navigationView"...
所以这是我的NavigationMenuHelper更改:
public NavigationMenuHelper(Context context) throws Exception {
activity = (Activity) context;
if(activity.findViewById(R.id.navigationView) == null) {
Log.e(TAG,activity.getLocalClassName()+" does not contain navigationView");
throw new Exception("Navigation view not found!");
}
if (!(activity instanceof OnMenuItemsClickListener))
throw new ClassCastException(activity +
" must implement NavigationMenuHelper.OnMenuItemsClickListener.");
initializeComponents(activity);
}
private void initializeComponents(Activity activity) {
// getting view objects
btnSettings = (ImageButton) activity.findViewById(R.id.btnSettings);
btnSupport = (ImageButton) activity.findViewById(R.id.btnSupport);
btnSandugh = (ImageButton) activity.findViewById(R.id.btnSandugh);
btnTransfer = (ImageButton) activity.findViewById(R.id.btnTransfer);
btnInsPayment = (ImageButton) activity.findViewById(R.id.btnInsPayment);
btnTutorial = (ImageButton) activity.findViewById(R.id.btnTutorial);
btnLostCard = (ImageButton) activity.findViewById(R.id.btnLostCard);
// set onClickListener
btnSettings.setOnClickListener(this);
btnSettings.setOnTouchListener(this);
btnSupport.setOnClickListener(this);
btnSandugh.setOnClickListener(this);
btnTransfer.setOnClickListener(this);
btnInsPayment.setOnClickListener(this);
btnTutorial.setOnClickListener(this);
btnLostCard.setOnClickListener(this);
}