当前位置:网站首页>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>
边栏推荐
- Software testing learning notes - network knowledge
- [technology development -21]: rapid overview of the application and development of network and communication technology -1- Internet Network Technology
- Logging only errors to the console Set system property ‘log4j2. debug‘ to sh
- CSDN article underlined, font color changed, picture centered, 1 second to understand
- Software development life cycle -- waterfall model
- What is the MySQL column to row function
- How to hide the scroll bar of scroll view in uniapp
- [learn C and fly] 3day Chapter 2 program in C language (exercise 2.3 calculate piecewise functions)
- What is the function of the headphone driver
- Questions d'entrevue
猜你喜欢
leetcode2312. Selling wood blocks (difficult, weekly race)
Which is a good Bluetooth headset of about 300? 2022 high cost performance Bluetooth headset inventory
How to use redis ordered collection
pytest 测试框架
leetcode2312. 卖木头块(困难,周赛)
Query word weight, search word weight calculation
OpenCASCADE7.6编译
Redis环境搭建和使用的方法
Software No.1
How to batch add background and transition effects to videos?
随机推荐
2022 low voltage electrician test question simulation test question bank simulation test platform operation
【做题打卡】集成每日5题分享(第二期)
How to turn off the LED light of Rog motherboard
WebGPU(一):基本概念
Data analysis on the disaster of Titanic
how to add one row in the dataframe?
An analysis of circuit for quick understanding
leetcode2309. The best English letters with both upper and lower case (simple, weekly)
Software No.1
2022安全员-C证考试题及模拟考试
【liuyubobobo-玩转Leetcode算法面试】【00】课程概述
* and & symbols in C language
Flutter un élément au milieu, l'élément le plus à droite
CVPR 2022 | 大连理工提出自校准照明框架,用于现实场景的微光图像增强
A quick understanding of analog electricity
DNS domain name resolution
leetcode373. 查找和最小的 K 对数字(中等)
query词权重, 搜索词权重计算
How to batch add background and transition effects to videos?
【带你学c带你飞】day 5 第2章 用C语言编写程序(习题2)