【问题标题】:my dashboard page lisview is overlap when i click on list items in android当我单击 android 中的列表项时,我的仪表板页面列表视图重叠
【发布时间】:2021-03-27 16:18:44
【问题描述】:

这是仪表板页面的代码,当我单击列表项时..它将使用导航抽屉活动与其他..iam 重叠一个片段 在此列表视图中与回收站视图重叠,我也共享一个图像文件

this is problem in my app of overlapping

android


public class DashboardFragment extends Fragment {
    FragmentManager mFragmentManager;
    FragmentTransaction mFragmentTransaction;
    ListView simpleList;
    TextView textView;
    String List[] = {"RELIGION", "CASTE", "SUBCASTE"};

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
       simpleList=root.findViewById(R.id.simplelist);
       textView=root.findViewById(R.id.item);

        mFragmentManager = getActivity().getSupportFragmentManager();
        mFragmentTransaction = mFragmentManager.beginTransaction();

        ArrayAdapter adapter = new ArrayAdapter(getActivity(),R.layout.list_item_lisview,R.id.item, List);

         simpleList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
             @Override
             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                 adapter.getItem(position);

                 if(position==0)
                 {
                     if (savedInstanceState == null) {

                         FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();

                         fragmentTransaction.replace(android.R.id.content, new ReligionFragment()).commit();
                     }
                 }
                 else
                 if(position==1) {
                     loadFragment(new CasteFragment());
                 }
                 else if(position==2) {

                     loadFragment(new SubCasteFragment());
                 }

             }

         });
         simpleList.setAdapter(adapter);

         return  root;
    }
    public void loadFragment(Fragment fragment) {
        FragmentTransaction transaction =  getActivity().getSupportFragmentManager().beginTransaction();
        transaction.replace(android.R.id.content, fragment);
        transaction.commit();
    }


    }

【问题讨论】:

  • 在这个页面中,我有两种方法适用于 if 部分直接调用片段管理器,另一种是 loadfragment() 方法......但还没有得到答案......请帮我解决这个问题,我是新手在安卓中
  • 我可以看看 R.layout.fragment_dashboard 里面的代码吗?
  • yes....//Fragment_dashboard.xml schemas.android.com/apk/res/android" android:id= "@+id/simplelist" android:layout_width="match_parent" android:layout_height="wrap_content" android:divider="#f00" android:dividerHeight="1dp" />
  • 只有一个列表视图吗?那么,什么是 R.id.container1 ?
  • 我昨天正在使用 framelayout,因此 framelayout id 是 R.id .Container1 但它不起作用,所以我删除了 FrameLayout

标签: android


【解决方案1】:

我使用导航组件在片段之间导航,这在如今的 Android 中很常见。当您在更新的 android studio 版本中创建新的 Navigation Drawer Activity 或 Bottom Navigation Activity 时,您会看到创建了导航文件夹。让我们一一来看。第一件事:

DashboardFragment.java

public class DashboardFragment extends Fragment {

public View onCreateView(@NonNull LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {

    return inflater.inflate(R.layout.fragment_dashboard, container, false);
   }
}

fragment_dashboard.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.dashboard.DashboardFragment">

<fragment
    android:id="@+id/nav_host_dashboard_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:defaultNavHost="true"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:navGraph="@navigation/dashboard_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>

现在在导航文件夹中创建一个名为 dashboard_navigation 的文件。

dashboard_navigation.xml

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/dashboard_navigation"
app:startDestination="@id/listFragment">

<fragment
    android:id="@+id/listFragment"
    android:name="com.pagingdemo.ui.dashboard.ListFragment"
    android:label="fragment_list"
    tools:layout="@layout/fragment_list" >
    <action
        android:id="@+id/action_listFragment_to_religionFragment"
        app:destination="@id/religionFragment" />
    <action
        android:id="@+id/action_listFragment_to_casteFragment"
        app:destination="@id/casteFragment" />
    <action
        android:id="@+id/action_listFragment_to_subCasteFragment"
        app:destination="@id/subCasteFragment" />
</fragment>
<fragment
    android:id="@+id/religionFragment"
    android:name="com.pagingdemo.ui.dashboard.ReligionFragment"
    android:label="fragment_religion"
    tools:layout="@layout/fragment_religion" />
<fragment
    android:id="@+id/casteFragment"
    android:name="com.pagingdemo.ui.dashboard.CasteFragment"
    android:label="fragment_caste"
    tools:layout="@layout/fragment_caste" />
<fragment
    android:id="@+id/subCasteFragment"
    android:name="com.pagingdemo.ui.dashboard.SubCasteFragment"
    android:label="fragment_sub_caste"
    tools:layout="@layout/fragment_sub_caste" />
我刚刚创建了 4 个片段。 1、ListFragment 显示宗教、种姓和子种姓列表。 2nd,ReligionFragment 用于宗教回收器视图。第三,种姓回收者视图的CasteFragment。 4、SubCasteFragment 用于子种姓回收站视图。

查看我的包名与您的包名不同。因此,如果您要复制代码,请确保您创建了 4 个具有正确名称的片段,然后在此导航 xml 文件中替换。 我只是发布上面提到的 4 个片段中的每一个的 onCreateView() 代码。

ListFragment 的 onCreateView 看起来像:

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    String[] list = {"RELIGION", "CASTE", "SUBCASTE"};
    View rootView =  inflater.inflate(R.layout.fragment_list, container, false);
    final ListView simpleList = rootView.findViewById(R.id.list_dashboard);
    ArrayAdapter adapter = new ArrayAdapter(getActivity(),R.layout.list_item_view,R.id.list_item_text_view, list);

    simpleList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            switch (position){
                case 0 : {
                    Navigation.findNavController(rootView).navigate(R.id.religionFragment);
                    break;
                }
                case 1 : {
                    Navigation.findNavController(rootView).navigate(R.id.casteFragment);
                    break;
                }
                case 2 : {
                    Navigation.findNavController(rootView).navigate(R.id.subCasteFragment);
                    break;
                }
            }
        }
    });
    simpleList.setAdapter(adapter);
    return rootView;
}

ListFragment 的布局,即 fragment_list.xml 看起来像:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".ui.dashboard.ListFragment">

<ListView
    android:id="@+id/list_dashboard"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

ReligionFragment 的 onCreateView 看起来像:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    ArrayList<String> religionList = new ArrayList();
    religionList.add("Hindu");
    religionList.add("Islam");
    religionList.add("Christianity");
    religionList.add("Budhisism");
    religionList.add("Jainisism");
    // Inflate the layout for this fragment
    View view =  inflater.inflate(R.layout.fragment_religion, container, false);
    RecyclerView recyclerView = view.findViewById(R.id.religion_list_view);
    recyclerView.setAdapter(new MyRecyclerAdapter(religionList));
    return view;
}

同样,您可以将其他两个片段(例如 CasteFragment 和 SubCasteFragment)与 RecyclerView 一起使用。

【讨论】:

  • 这是完美的答案先生..对于片段重叠问题..
  • 我不是天才,只是个程序员。无论如何,如果我有能力解决您的问题并且如果我有时间,那么我一定会帮助您。快乐编码!!!
  • stackoverflow.com/questions/67986971/… 先生,如果可能请检查一下
【解决方案2】:

请在您的 XML 父小部件中设置 android:background="@color/white"。它对我有用,因为总是片段设置默认颜色透明。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-15
    • 1970-01-01
    相关资源
    最近更新 更多