簡體   English   中英

Layout_behavior在另一個適配器內部

[英]Layout_behavior with an Adapter inside another one

我一直在搜索,發現了一些很接近的問題和答案,但是沒有一個起作用。

我將CoordinatorLayout用於帶有AppBarLayoutToolBar的 RecyclerView列表。

我的目標:

想法很簡單,當您首先滾動視圖時,請在工具欄中滾動AppBar折疊,然后再滾動RecyclerView。

我的層次結構:

我的片段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(Vertical)或Adapter-A Img,它可以正常工作。

但是,如果我單擊適配器B的內容(圖和文本),則會滾動兩個“回收者視圖”,而不滾動AppBar。

我在做什么:

我用

app:layout_behavior =“ @ string / appbar_scrolling_view_behavior”

recyclerList.setHasFixedSize(true)

recyclerList.setNestedScrollingEnabled(true);

在兩個“回收者視圖”中。

app:layout_scrollFlags =“ scroll | 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>

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

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

    }

}
}

找到了解決方案。

創建父VerticalListView時,將setNestedScrollingEnabled設置為true。 並且在創建子級Horizo​​ntal ListView時將setNestedScrollingEnabled設置為false。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM