gpt4 book ai didi

android - ListView 项目在抽屉导航中不起作用

转载 作者:行者123 更新时间:2023-11-29 00:12:02 25 4
gpt4 key购买 nike

对不起我的英语,我是 android 的新手并创建应用程序。一切正常,但我有一个非常小的问题是,在抽屉导航中,当我单击项目时,只是抽屉导航被关闭,这没有错误,logcat 上没有任何错误真的我不知道,但我猜 mybe 从布局。我的布局:

    <?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;
}
}

最佳答案

问题来自另一个 fragment ,用于在屏幕底部显示图像,当我点击抽屉时 fragment 位于顶部抽屉实际上是我点击了 fragment 所以我从来没有点击过按钮因为我替换了 fragment 使用 RelativeLayout 解决我的问题。

关于android - ListView 项目在抽屉导航中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29590485/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com