列表视图项目在导航抽屉中不起作用
listview item not working in Navigation Drawer
请原谅我的英语不好,我是 android 的新手并正在创建应用程序。一切正常,但我有一个非常小的问题是,在导航抽屉中,当我单击项目时,只是导航抽屉被关闭,这没有错误,logcat 上没有任何错误,我真的不知道,但我猜我是来自布局 。
我的布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
tools:context=".MyPayamak"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null"
android:background="#ffffff"
android:dividerHeight="10sp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
</ListView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tt"/>
<ProgressBar
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_gravity="center"
android:id="@+id/progressBar"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@drawable/bar"
android:orientation="vertical">
<!-- Side navigation drawer UI -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="کاربر میهمان"
android:textColor="#3D3D3D"
android:background="#ffffff"
android:layout_gravity="center"
android:gravity="center"
android:padding="15dp"
android:id="@+id/ShowFakenameOfUser"/>
<ImageView
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/usericon"/>
<ListView
android:id="@+id/navList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:divider="@null"
android:dividerHeight="10dp"
android:layout_marginTop="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
还有一件事,我的导航抽屉代码在所有 activity 中都是相同的并且工作正常但是 2 activity 有这个问题使用这个布局。
在我的 oncreate 中有这个代码:
mDrawerList = (ListView)findViewById(R.id.navList);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
mActivityTitle = getTitle().toString();
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (parent.getItemAtPosition(position).toString().equalsIgnoreCase("ورود")) {
Intent it = new Intent(List_Payamak_ha.this, loginUser.class);
startActivity(it);
}else if (parent.getItemAtPosition(position).toString().equalsIgnoreCase("ثبت نام")){
Intent it = new Intent(List_Payamak_ha.this, RegisterUser.class);
startActivity(it);
}else if (parent.getItemAtPosition(position).toString().equalsIgnoreCase("کاربران فعال")) {
Intent it = new Intent(List_Payamak_ha.this, ActiveUsers.class);
startActivity(it);
}else if (parent.getItemAtPosition(position).toString().equalsIgnoreCase("کاربران برتر")) {
Intent it = new Intent(List_Payamak_ha.this, TopUsers.class);
startActivity(it);
}else if (parent.getItemAtPosition(position).toString().equalsIgnoreCase("درباره نرم افزار")) {
Intent it = new Intent(List_Payamak_ha.this, AboutPage.class);
startActivity(it);
}else if (parent.getItemAtPosition(position).toString().equalsIgnoreCase("خروج")) {
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
finish();
System.exit(0);
}
}
});
addDrawerItems();
setupDrawer();
完整代码:
private void addDrawerItems() {
String[] osArray = getResources().getStringArray(R.array.SlideMenu);
mAdapterCopy = new MenuSideAdapterItem(this,
android.R.layout.simple_list_item_1,
R.id.textViewMenuSide,
osArray);
mDrawerList.setAdapter(mAdapterCopy);
}
private void setupDrawer() {
;
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.string.drawer_open, R.string.drawer_close) {
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View view) {
super.onDrawerOpened(view);
getSupportActionBar().setTitle("منو");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getSupportActionBar().setTitle(mActivityTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
private class MenuSideAdapterItem extends ArrayAdapter<String> {
public MenuSideAdapterItem(Context context, int resource, int textViewResourceId, String[] objects) {
super(context, resource, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View Row = inflater.inflate(R.layout.menu_item,parent,false);
String[] items = getResources().getStringArray(R.array.SlideMenu);
ImageView IconView = (ImageView) Row.findViewById(R.id.imageViewMenuSide);
TextView textMenuSide = (TextView) Row.findViewById(R.id.textViewMenuSide);
textMenuSide.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/Far_TitrDF.ttf"));
textMenuSide.setText(items[position]);
if (items[position].equalsIgnoreCase("ورود")){
IconView.setImageResource(R.drawable.loginicon);
}else if (items[position].equalsIgnoreCase("ثبت نام")){
IconView.setImageResource(R.drawable.registericoon);
}else if (items[position].equalsIgnoreCase("کاربران فعال")){
IconView.setImageResource(R.drawable.activeuser);
}else if (items[position].equalsIgnoreCase("کاربران برتر")){
IconView.setImageResource(R.drawable.topuser);
}else if (items[position].equalsIgnoreCase("درباره نرم افزار")){
IconView.setImageResource(R.drawable.about);
}else if (items[position].equalsIgnoreCase("خروج")){
IconView.setImageResource(R.drawable.exit);
}
return Row;
}
}
我认为控制不会进入你的 if 条件,所以请按照以下步骤操作
1 使用
在每个 if 语句中打印消息
Log.i("tag","any_message");
或
System.out.println("any_message");
2 同时打印带有正确消息的 add else 语句
并通过这种方式检查您的logcat,您将了解控制是否在 if 语句中。
问题来自另一个片段,用于在屏幕底部显示图像,当我点击抽屉时片段位于顶部抽屉实际上是我点击了片段所以我从来没有点击过按钮因为我替换 fragment
用 RelativeLayout
解决我的问题。
在你的情况下,使用 fragment
确实是个坏主意,但现在你可以使用 NavigationView and this tutorial
请原谅我的英语不好,我是 android 的新手并正在创建应用程序。一切正常,但我有一个非常小的问题是,在导航抽屉中,当我单击项目时,只是导航抽屉被关闭,这没有错误,logcat 上没有任何错误,我真的不知道,但我猜我是来自布局 。 我的布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
tools:context=".MyPayamak"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null"
android:background="#ffffff"
android:dividerHeight="10sp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
</ListView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tt"/>
<ProgressBar
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_gravity="center"
android:id="@+id/progressBar"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@drawable/bar"
android:orientation="vertical">
<!-- Side navigation drawer UI -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="کاربر میهمان"
android:textColor="#3D3D3D"
android:background="#ffffff"
android:layout_gravity="center"
android:gravity="center"
android:padding="15dp"
android:id="@+id/ShowFakenameOfUser"/>
<ImageView
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/usericon"/>
<ListView
android:id="@+id/navList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:divider="@null"
android:dividerHeight="10dp"
android:layout_marginTop="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
还有一件事,我的导航抽屉代码在所有 activity 中都是相同的并且工作正常但是 2 activity 有这个问题使用这个布局。
在我的 oncreate 中有这个代码:
mDrawerList = (ListView)findViewById(R.id.navList);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
mActivityTitle = getTitle().toString();
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (parent.getItemAtPosition(position).toString().equalsIgnoreCase("ورود")) {
Intent it = new Intent(List_Payamak_ha.this, loginUser.class);
startActivity(it);
}else if (parent.getItemAtPosition(position).toString().equalsIgnoreCase("ثبت نام")){
Intent it = new Intent(List_Payamak_ha.this, RegisterUser.class);
startActivity(it);
}else if (parent.getItemAtPosition(position).toString().equalsIgnoreCase("کاربران فعال")) {
Intent it = new Intent(List_Payamak_ha.this, ActiveUsers.class);
startActivity(it);
}else if (parent.getItemAtPosition(position).toString().equalsIgnoreCase("کاربران برتر")) {
Intent it = new Intent(List_Payamak_ha.this, TopUsers.class);
startActivity(it);
}else if (parent.getItemAtPosition(position).toString().equalsIgnoreCase("درباره نرم افزار")) {
Intent it = new Intent(List_Payamak_ha.this, AboutPage.class);
startActivity(it);
}else if (parent.getItemAtPosition(position).toString().equalsIgnoreCase("خروج")) {
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
finish();
System.exit(0);
}
}
});
addDrawerItems();
setupDrawer();
完整代码:
private void addDrawerItems() {
String[] osArray = getResources().getStringArray(R.array.SlideMenu);
mAdapterCopy = new MenuSideAdapterItem(this,
android.R.layout.simple_list_item_1,
R.id.textViewMenuSide,
osArray);
mDrawerList.setAdapter(mAdapterCopy);
}
private void setupDrawer() {
;
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.string.drawer_open, R.string.drawer_close) {
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View view) {
super.onDrawerOpened(view);
getSupportActionBar().setTitle("منو");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getSupportActionBar().setTitle(mActivityTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
private class MenuSideAdapterItem extends ArrayAdapter<String> {
public MenuSideAdapterItem(Context context, int resource, int textViewResourceId, String[] objects) {
super(context, resource, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View Row = inflater.inflate(R.layout.menu_item,parent,false);
String[] items = getResources().getStringArray(R.array.SlideMenu);
ImageView IconView = (ImageView) Row.findViewById(R.id.imageViewMenuSide);
TextView textMenuSide = (TextView) Row.findViewById(R.id.textViewMenuSide);
textMenuSide.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/Far_TitrDF.ttf"));
textMenuSide.setText(items[position]);
if (items[position].equalsIgnoreCase("ورود")){
IconView.setImageResource(R.drawable.loginicon);
}else if (items[position].equalsIgnoreCase("ثبت نام")){
IconView.setImageResource(R.drawable.registericoon);
}else if (items[position].equalsIgnoreCase("کاربران فعال")){
IconView.setImageResource(R.drawable.activeuser);
}else if (items[position].equalsIgnoreCase("کاربران برتر")){
IconView.setImageResource(R.drawable.topuser);
}else if (items[position].equalsIgnoreCase("درباره نرم افزار")){
IconView.setImageResource(R.drawable.about);
}else if (items[position].equalsIgnoreCase("خروج")){
IconView.setImageResource(R.drawable.exit);
}
return Row;
}
}
我认为控制不会进入你的 if 条件,所以请按照以下步骤操作
1 使用
在每个 if 语句中打印消息Log.i("tag","any_message");
或
System.out.println("any_message");
2 同时打印带有正确消息的 add else 语句
并通过这种方式检查您的logcat,您将了解控制是否在 if 语句中。
问题来自另一个片段,用于在屏幕底部显示图像,当我点击抽屉时片段位于顶部抽屉实际上是我点击了片段所以我从来没有点击过按钮因为我替换 fragment
用 RelativeLayout
解决我的问题。
在你的情况下,使用 fragment
确实是个坏主意,但现在你可以使用 NavigationView and this tutorial