当前位置:网站首页>Coordinatorlayout + tablayout + viewpager2 (there is another recyclerview nested inside), and the sliding conflict of recyclerview is solved
Coordinatorlayout + tablayout + viewpager2 (there is another recyclerview nested inside), and the sliding conflict of recyclerview is solved
2022-07-02 02:22:00 【Rannki】
One was used CoordinatorLayout Slide layout , But it needs to use TabLayout + ViewPager2 Combine , then ViewPager2 Inside is another recyclerView list , After writing the code , Find out recyclerView Cannot slide vertically , Get rid of CoordinatorLayout After the layout, you can slide , But there is demand here , It's impossible to remove it , So only from recyclerView Try to solve the sliding conflict problem here . I've been searching online for a long time , It's all about setting monitoring , It didn't work , Finally found , Only need ViewPager2 Add an attribute here , You can make ViewPager2 Inside RecyclerView Normal longitudinal sliding . This property is 【app:layout_behavior="@string/appbar_scrolling_view_behavior"】.
all test The code is as follows :
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
app:layout_scrollFlags="scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="300dp"
android:text="aaa" />
<TextView
android:layout_width="match_parent"
android:layout_height="300dp"
android:text="aaa" />
<TextView
android:layout_width="match_parent"
android:layout_height="300dp"
android:text="aaa" />
<TextView
android:layout_width="match_parent"
android:layout_height="300dp"
android:text="aaa" />
</LinearLayout>
<com.google.android.material.tabs.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tabLayout" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager2.widget.ViewPager2
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/viewPager2"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</LinearLayout>MainActivity.java:
package com.example.myapplication;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;
import android.os.Bundle;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
// recyclerView Medium item data source
private List<String> itemData = new ArrayList<>();
// tabLayout The title of the
private String[] tabTitle = {" title 1", " title 2", " title 3", " title 4", " title 5", " title 6", " title 7", " title 8", " title 9"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initItemData();
TabLayout tabLayout = findViewById(R.id.tabLayout);
// Set lateral sliding
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
ViewPager2 viewPager2 = findViewById(R.id.viewPager2);
viewPager2.setAdapter(new MyViewPager2Adapter(itemData, tabTitle, this));
TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(tabLayout, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
@Override
public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
// Set up tabLayout The title of the
tab.setText(tabTitle[position]);
}
});
// Application takes effect
tabLayoutMediator.attach();
}
/**
* initialization recyclerView Medium item data source
*/
private void initItemData() {
for (int i = 0; i < 100; i++) {
itemData.add(i + "");
}
}
}MyViewPager2Adapter.java:
package com.example.myapplication;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class MyViewPager2Adapter extends RecyclerView.Adapter<MyViewPager2Adapter.ViewHolder> {
private List<String> itemData;
private String[] TabTitle;
private Context context;
public MyViewPager2Adapter(List<String> itemData, String[] tabTitle, Context context) {
this.itemData = itemData;
TabTitle = tabTitle;
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.view_pager2_recycler_view, parent, false));
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.recyclerView.setAdapter(new MyRecyclerViewAdapter(itemData));
holder.recyclerView.setLayoutManager(new LinearLayoutManager(context));
}
@Override
public int getItemCount() {
return TabTitle.length;
}
class ViewHolder extends RecyclerView.ViewHolder {
RecyclerView recyclerView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
recyclerView = itemView.findViewById(R.id.recyclerView);
}
}
}
view_pager2_recycler_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recyclerView" />
</LinearLayout>MyRecyclerViewAdapter.java:
package com.example.myapplication;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {
List<String> itemData;
public MyRecyclerViewAdapter(List<String> itemData) {
this.itemData = itemData;
}
@NonNull
@Override
public MyRecyclerViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_item, parent, false));
}
@Override
public void onBindViewHolder(@NonNull MyRecyclerViewAdapter.ViewHolder holder, int position) {
holder.textView.setText(itemData.get(position));
}
@Override
public int getItemCount() {
return itemData.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView textView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.text);
}
}
}
recycler_view_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text" />
</LinearLayout>The last one looks like this :

If there is still sliding conflict , You can put MainActivity.xml Medium ViewPager2 The code is changed to this :
use 【NestedScrollView】 Wrap it up , Then attribute 【app:layout_behavior="@string/appbar_scrolling_view_behavior"】 Also moved to 【NestedScrollView】 In it .
<androidx.core.widget.NestedScrollView
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.viewpager2.widget.ViewPager2
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/viewPager2"/>
</androidx.core.widget.NestedScrollView>边栏推荐
- Sword finger offer 47 Maximum value of gifts
- Infix expression to suffix expression (computer) code
- Openssl3.0 learning XXI provider encoder
- es面试题
- Email picture attachment
- 剑指 Offer 42. 连续子数组的最大和
- Quality means doing it right when no one is looking
- Construction and maintenance of business websites [10]
- Software testing learning notes - network knowledge
- 大厂裁员潮不断,双非本科出身的我却逆风翻盘挺进阿里
猜你喜欢

leetcode2312. Selling wood blocks (difficult, weekly race)

JVM interview

Vsocde has cli every time it is opened js

Design and implementation of key value storage engine based on LSM tree

What are the necessary things for students to start school? Ranking list of Bluetooth headsets with good sound quality

How to build and use redis environment
![[reading notes] programmer training manual - practical learning is the most effective (project driven)](/img/13/28116a74512895ad725dffed02f8bd.png)
[reading notes] programmer training manual - practical learning is the most effective (project driven)

How to turn off the LED light of Rog motherboard
![[question] - why is optical flow not good for static scenes](/img/8d/2cf6f582bc58cc2985f50e3f85f334.jpg)
[question] - why is optical flow not good for static scenes

MySQL中一条SQL是怎么执行的
随机推荐
花一个星期时间呕心沥血整理出高频软件测试/自动化测试面试题和答案
Start from scratch - Web Host - 01
how to come in an investnent bank team
Leetcode face T10 (1-9) array, ByteDance interview sharing
C return multiple values getter setter queries the database and adds the list return value to the window
Duplicate keys detected: ‘0‘. This may cause an update error. found in
剑指 Offer 31. 栈的压入、弹出序列
An analysis of circuit for quick understanding
[opencv] - comprehensive examples of five image filters
oracle创建只读权限的用户简单四步走
From January 11, 2007 to January 11, 2022, I have been in SAP Chengdu Research Institute for 15 years
Sword finger offer 29 Print matrix clockwise
JPM 2021 most popular paper released (with download)
Bash bounce shell encoding
Sword finger offer 31 Stack push in and pop-up sequence
LeetCode刷题(十)——顺序刷题46至50
How to turn off the LED light of Rog motherboard
leetcode2305. Fair distribution of biscuits (medium, weekly, shaped pressure DP)
MySQL operates the database through the CMD command line, and the image cannot be found during the real machine debugging of fluent
2022 Q2 - Summary of skills to improve skills