当前位置:网站首页>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>
边栏推荐
- [reading notes] programmer training manual - practical learning is the most effective (project driven)
- C # use system data. The split mixed mode assembly is generated for the "v2.0.50727" version of the runtime, and it cannot be loaded in the 4.0 runtime without configuring other information
- 2022 Q2 - 提昇技能的技巧總結
- Construction and maintenance of business websites [13]
- leetcode373. Find and minimum k-pair numbers (medium)
- es面试题
- 【liuyubobobo-玩转Leetcode算法面试】【00】课程概述
- Which is a good Bluetooth headset of about 300? 2022 high cost performance Bluetooth headset inventory
- [punch in questions] integrated daily 5-question sharing (phase II)
- Kibana controls es
猜你喜欢
What are the necessary things for students to start school? Ranking list of Bluetooth headsets with good sound quality
Build a modern data architecture on the cloud with Amazon AppFlow, Amazon lake formation and Amazon redshift
leetcode2305. Fair distribution of biscuits (medium, weekly, shaped pressure DP)
Vsocde has cli every time it is opened js
研发中台拆分过程的一些心得总结
Which is a good Bluetooth headset of about 300? 2022 high cost performance Bluetooth headset inventory
[learn C and fly] 3day Chapter 2 program in C language (exercise 2.3 calculate piecewise functions)
Software No.1
Summary of some experiences in the process of R & D platform splitting
If you want to rewind the video picture, what simple methods can you use?
随机推荐
Medical management system (C language course for freshmen)
What style of Bluetooth headset is easy to use? High quality Bluetooth headset ranking
CSDN article underlined, font color changed, picture centered, 1 second to understand
【毕业季】研究生学长分享怎样让本科更有意义
[reading notes] programmer training manual - practical learning is the most effective (project driven)
flutter 中間一個元素,最右邊一個元素
Comparative analysis of MVC, MVP and MVVM, source code analysis
CSDN insertion directory in 1 second
Questions d'entrevue
How to batch add background and transition effects to videos?
2022 Q2 - résumé des compétences pour améliorer les compétences
leetcode2305. Fair distribution of biscuits (medium, weekly, shaped pressure DP)
An analysis of circuit for quick understanding
Oracle creates a user with read-only permission in four simple steps
leetcode2309. The best English letters with both upper and lower case (simple, weekly)
[question] - why is optical flow not good for static scenes
Types of exhibition items available in the multimedia interactive exhibition hall
STM32F103——两路PWM控制电机
Additional: information desensitization;
Query word weight, search word weight calculation