【问题标题】:Cannot scroll RecyclerView inside a Fragment?无法在 Fragment 内滚动 RecyclerView?
【发布时间】:2016-01-17 05:04:38
【问题描述】:

我在片段中集成 RecyclerView(对行使用卡片视图)时遇到问题。出于某种原因,我无法滚动卡片视图?请问有什么建议吗?

这是我的应用设计:

这是我的片段代码:

public class PageFragment2 extends Fragment {
public static final String ARG_PAGE2 = "ARG_PAGE";
private int mPage2;

public static PageFragment2 newInstance(int page) {
    Bundle args = new Bundle();
    args.putInt(ARG_PAGE2, page);
    PageFragment2 fragment = new PageFragment2();
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mPage2 = getArguments().getInt(ARG_PAGE2);

    Log.d("MainActivity", "onCreate" + mPage2);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_page2,container,false);

    RecyclerView recList = (RecyclerView) view;
    recList.setHasFixedSize(true);
    LinearLayoutManager llm = new LinearLayoutManager(getActivity());
    recList.setLayoutManager(llm);

    ArrayList<String> names = new ArrayList<>();
    names.add("Georgi Koemdzhiev");
    names.add("Mariya Menova");
    names.add("Simeon Simeonov");
    names.add("Ivan Dqkov");
    names.add("Dymityr Vasilev");
    names.add("Petar Dimov");
    CardAdapter adapter = new CardAdapter(names);

    recList.setAdapter(adapter);
    Log.d("MainActivity","onCreateView" + mPage2);
    return view;
}
}

这是我的片段 XML 代码:

<android.support.v7.widget.RecyclerView
android:id="@+id/cardList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="true"
xmlns:android="http://schemas.android.com/apk/res/android"/>

我的 activity_main 布局:

<android.support.design.widget.CoordinatorLayout
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"
android:fitsSystemWindows="true"
tools:context="koemdzhiev.com.newtablayouttest.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/sliding_tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1"
        android:background="@android:color/white" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main"/>

【问题讨论】:

  • 为什么需要 relativeLayout?摆脱它
  • @injecteer 我不知道为什么要添加它。我删除了它,但问题仍然存在。我用这些更改更新了我的问题。
  • 尝试添加llm.setOrienation( HORIZONTAL )
  • @injecteer 出于某种原因,我正在滚动。但是,我想垂直滚动。我确实尝试将它设置为垂直,但它仍然无法正常工作......
  • 有点小错误。不要用AppBarLayout 作为 sorianiv 的回答来包装你的内容区域 :)

标签: android android-fragments android-recyclerview


【解决方案1】:

“Tabbed Activity”模板中的 Activity 使用 AppBarLayout。

这个 AppBarLayout 正在窃取垂直移动,因为它不知道 ViewPager 中有一个可滚动的嵌套视图(RecyclerView)。从你的Activity的XML文件中去掉AppBarLayout(你可以把TabLayout和ToolBar带到上层)。

【讨论】:

  • 感谢您的回答。但是,我不确定您的建议是什么。我也包含了我的 activity_main xml 代码。你能告诉我具体我需要改变什么吗? :)
  • 没关系,我刚刚将 TabLayout 和 ViewPager 包装在 AppBarLayout 之外的 LinierView 中,现在可以正常工作了。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-24
  • 2017-11-24
  • 2016-10-20
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多