Layout_behavior与另一个内部的适配器

问题描述 投票:0回答:1

我一直在寻找并发现一些接近的问题和答案,但没有一个有效。

我正在使用CoordinatorLayout获取带有AppBarLayout和ToolBar的RecyclerView列表。

我的目标:

当您首先在工具栏中滚动视图AppBar Collapse时,ideia非常简单,然后RecyclerView开始滚动。

我的层次结构:

My Fragment RecyclerView(垂直)有一个带有新布局的Adapter-A,这个适配器为RecyclerView(水平)调用第二个适配器-B。

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

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

问题是什么:

如果我点击RecyclerView(垂直)或适配器-Img它可以正常工作。

但是,如果我单击适配器B内容(Img和文本),它会滚动两个Recycler Views而不是AppBar。

我在做什么:

我用

应用程式:layout_behavior = “@串/ appbar_scrolling_view_behavior”

recyclerList.setHasFixedSize(真)

recyclerList.setNestedScrollingEnabled(真);

在两个Recycler视图中。

应用:layout_scrollFlags = “滚动| exitUntilCollapsed | enterAlways”

在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

<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>

main home.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

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());
            }
        });

    }

}
}
android android-recyclerview material-design android-adapter android-collapsingtoolbarlayout
1个回答
0
投票

找到了解决方案。

创建父Vertical ListView时,将setNestedScrollingEnabled设置为true。并且在创建子级时,Horizo​​ntal ListView将setNestedScrollingEnabled设置为false。

© www.soinside.com 2019 - 2024. All rights reserved.