【问题标题】:ListView and onItemClick create transparent fragmentListView 和 onItemClick 创建透明片段
【发布时间】:2017-09-11 04:18:43
【问题描述】:

我有问题。我将片段用于列表视图,在单击一个项目后,它应该将此片段替换为具有此特定片段(在本例中为事件)的片段。它可以工作(显示片段)但它是透明的,我可以在背景中看到列表(即使它不可点击)。我怎样才能摆脱这个奇怪的背景?用类似方法替换的另一个片段工作正常,只需使用“onItemClick”即可。

这是我的代码:

lvEvents.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                                    long id) {
                Event currEvent = (Event) parent.getAdapter().getItem(position);
                Log.e(TAG, currEvent.getName().toString());
                replaceFragment(0, currEvent);
            }
        });

以及方法:

 private void replaceFragment(int code, Event event) {
        FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
        Bundle b = new Bundle();
        b.putParcelable("event",event);

        if(code==0){
            EventViewFragment fragment = new EventViewFragment();
            fragment.setArguments(b);
            ft.replace(R.id.fragmentFrame, fragment, EventViewFragment.TAG);
        }
        ft.commit();
    }

你有什么想法吗?

提前谢谢你!

问候,

格热戈茨

【问题讨论】:

    标签: android listview android-fragments


    【解决方案1】:

    试试这个..即使我遇到了这个问题...我浏览了几篇文章...写到我们可以在主布局中使用白色背景...在框架布局线性布局 在我的情况下。 使用 android:background="@color/white"

    <LinearLayout
    android:id="@+id/timetable_fragment"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:background="@color/white"
    android:layout_height="match_parent">
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/card_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
        </android.support.v7.widget.RecyclerView>
    </LinearLayout>
    

    你可以使用这个方法然后..它替换当前片段......使用 replace 关键字..如果你想使用 addtobackStack 关键字来堆叠片段,它就在你身上。

    public void replaceFragment(Fragment fragment) {
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.container, fragment)
                .addToBackStack(null)
                .commit();
    }
    

    【讨论】:

    • 我明白,但这不就是一种解决办法吗?为什么我的其他片段正确显示,而只有来自 listView 的片段有问题?
    • 你使用的是容器,那么你可以添加 addtobackStack 命令
    • 替换关键字以替换片段...检查我在答案中编辑的方法。
    • 当然,如果您在实现代码时遇到问题,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-05
    • 2011-08-31
    • 1970-01-01
    • 2014-04-26
    • 1970-01-01
    • 2014-04-20
    • 1970-01-01
    相关资源
    最近更新 更多