Layout_behavior,其中一个适配器位于另一个适配器中 [英] Layout_behavior with an Adapter inside another one

本文介绍了Layout_behavior,其中一个适配器位于另一个适配器中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在搜索,发现了一些很接近的问题和答案,但是没有一个起作用.

I have been searching and found some close questions and answer but none of them works.

我正在将 CoordinatorLayout 用于带有 AppBarLayout ToolBar 的列表 RecyclerView .

I'm using CoordinatorLayout for a list RecyclerView with AppBarLayout and ToolBar.

我的目标:

思想很简单,当您首先滚动视图时,在工具栏中折叠AppBar,然后再滚动RecyclerView.

The ideia is very simple, when you roll the view first the AppBar Collapse in the Toolbar and after that the RecyclerView start to scroll.

我的层次结构:

我的片段RecyclerView(垂直)有一个具有新布局的Adapter-A,此适配器为RecyclerView(水平)调用了另一个适配器B.

My Fragment RecyclerView(vertical) has a Adapter-A with a new layout, and this adapter call a second adapter-B for the RecyclerView(horizontal).

所以我有一个像这样的结构: (不是代码,只是为了展示其工作原理)

So I Have a struct like this: (is not a code, just to show how works)

Fragment AppBarLayout{..}
Fragment Recycler View (Vertical) {
    Adapter-A Text;
    Adapter-A Recycler View (Horizontal){
        Adapter-B Img;
        Adapter-B Text;
    }
}

出了什么问题

如果我单击RecyclerView(垂直)或Adapter-A图,则可以正常工作.

If I click in the RecyclerView(Vertical) or Adapter-A Img it works fine.

但是,如果我单击适配器B的内容(图和文本),则会滚动两个回收者视图",但不会滚动AppBar.

But If I click in Adapter B content(Img and Text) it scroll both Recycler Views but not the AppBar.

我在做什么:

我使用

app:layout_behavior ="@ string/appbar_scrolling_view_behavior"

app:layout_behavior="@string/appbar_scrolling_view_behavior"

recyclerList.setHasFixedSize(true)

recyclerList.setHasFixedSize(true)

recyclerList.setNestedScrollingEnabled(true);

recyclerList.setNestedScrollingEnabled(true);

在两个回收站"视图中.

in both Recycler Views.

app:layout_scrollFlags ="scroll | exitUntilCollapsed | enterAlways"

app:layout_scrollFlags="scroll|exitUntilCollapsed|enterAlways"

在CollapsingToolbarLayout中

in the CollapsingToolbarLayout

我尝试用Java代码制作此代码:

        recyclerList.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            // If AppBar is fully expanded, revert the scroll.
            if (!shouldScroll) {
                recyclerList.scrollToPosition(0);
                //Here I should make the AppBar Scroll, but AppBarLayout.scrollTo(dx, dy) don't work.
            }
        }
    });


    mainHomeAppBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
            shouldScroll = verticalOffset != 0;
        }
    });

我的代码:

mainHome.xml

mainHome.xml

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
    android:id="@+id/main_home_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

<android.support.design.widget.AppBarLayout
    android:id="@+id/main_home_app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="192dp"
    android:background="@drawable/gradient_bg">

    <android.support.design.widget.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed|enterAlways">

        <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="50dp"
            app:layout_collapseMode="pin" />

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

mainHome.java

mainHome.java

@Override
protected void assignViews() {
    mainHomeList = (RecyclerView) findViewById(R.id.main_home_list);
    mainHomeAppBarLayout = (AppBarLayout) findViewById(R.id.main_home_app_bar_layout);
}

@Override
protected void prepareViews() {
    mainHomeList.setHasFixedSize(true);
    mainHomeList.setNestedScrollingEnabled(true);
    linearLayoutManager = new LinearLayoutManager(getApplicationContext());
    mainHomeList.setLayoutManager(linearLayoutManager);
    initAdapterIfNecessary();
if (mainHomeList.getAdapter() == null)
        mainHomeList.setAdapter(adapter);
}

adapter-A.java

adapter-A.java

public class MainHomeModulesAdapter extends RecyclerView.Adapter<MainHomeModulesAdapter.GroupViewHolder> {

private OnListItemClickedListener onListItemClickedListener = null;
private OnListItemClickedTwoListener onListItemClickedTwoListener = null;
private ArrayList<JSONMainModule> mainModules = new ArrayList<>();

@Override
public MainHomeModulesAdapter.GroupViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    Context context = parent.getContext();
    View itemView = LayoutInflater.from(context).inflate(R.layout.a_main_home_module_item, parent, false);
    return (new MainHomeModulesAdapter.GroupViewHolder(itemView));
}

@Override
public void onBindViewHolder(MainHomeModulesAdapter.GroupViewHolder holder, int position) {
        //Place where put layout information
        holder.mainHomeModuleList.setLayoutManager(new GridLayoutManager(context, Utils.calcGridSpaceCount(context, 2))); //For two elements
        holder.mainHomeModuleList.setAdapter(holder.mainHomeContentAdapter);
        holder.mainHomeContentAdapter.updateListContent(mainModules.get(position).getModuleContent(), mainModules.get(position).getModule());
}

@Override
public int getItemCount() {
    return mainModules.size();
}

public void setOnListItemClickedListener(OnListItemClickedListener onListItemClickedListener) {
    this.onListItemClickedListener = onListItemClickedListener;
}

public void setOnListItemClickedTwoListener(OnListItemClickedTwoListener onListItemClickedTwoListener){
    this.onListItemClickedTwoListener = onListItemClickedTwoListener;
}

public void updateListContent(ArrayList<JSONMainModule> mainModules) {
    this.mainModules = mainModules;
    notifyDataSetChanged();
}

public JSONMainModule getListContent(int pos) {
    return mainModules.get(pos);
}

class GroupViewHolder extends ParentViewHolder {
    TextView mainHomeModuleText;
    Button mainHomeModuleBtn;
    RecyclerView mainHomeModuleList;
    MainHomeContentAdapter mainHomeContentAdapter; //Adapter-B

    private GroupViewHolder(View itemView) {
        super(itemView);
        mainHomeModuleText = (TextView) itemView.findViewById(R.id.main_home_module_title);
        mainHomeModuleBtn = (Button) itemView.findViewById(R.id.main_home_module_btn);
        mainHomeModuleBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ATUtils.isDoubleClick()) return;
                onListItemClickedListener.onClicked(getAdapterPosition());
            }
        });


        //Child - Main Contents
        mainHomeModuleList = (RecyclerView) itemView.findViewById(R.id.main_home_module_list);
        mainHomeModuleList.setHasFixedSize(true);
        mainHomeModuleList.setNestedScrollingEnabled(true);

        mainHomeContentAdapter = new MainHomeContentAdapter();

        mainHomeContentAdapter.setOnListItemClickedListener(new OnListItemClickedListener() {
            @Override
            public void onClicked(int pos) {
                onListItemClickedTwoListener.onClicked(pos, getAdapterPosition());
            }
        });

    }

}
}

推荐答案

找到了解决方案.

在创建父垂直列表视图"时,将setNestedScrollingEnabled设置为true. 并且在创建子级Horizo​​ntal ListView时将setNestedScrollingEnabled设置为false.

When create the parent Vertical ListView set setNestedScrollingEnabled as true. And when create the child Horizontal ListView set setNestedScrollingEnabled as false.

这篇关于Layout_behavior,其中一个适配器位于另一个适配器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆