Android 两个recyclerview当我向下滚动时,第一个要滚动的recyclerview,如何滚动?
我希望有两个网格图像一个垂直,第二个水平(recyclerview1和recyclerview2),第一个我希望它始终位于顶部并左右滚动,但是当我滚动第二个网格视图时,我希望第一个也能滚动,因为它们在一个滚动器中 例如: 当我在第二个recyclerview(recyclerview2)中向下滚动时,我希望也滚动第一个回收器。怎么做? 结果: 这是我的代码: 主要活动代码:Android 两个recyclerview当我向下滚动时,第一个要滚动的recyclerview,如何滚动?,android,Android,我希望有两个网格图像一个垂直,第二个水平(recyclerview1和recyclerview2),第一个我希望它始终位于顶部并左右滚动,但是当我滚动第二个网格视图时,我希望第一个也能滚动,因为它们在一个滚动器中 例如: 当我在第二个recyclerview(recyclerview2)中向下滚动时,我希望也滚动第一个回收器。怎么做? 结果: 这是我的代码: 主要活动代码: protected void onCreate(Bundle savedInstanceState) {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// initToolbars();
recyclerView = (RecyclerView) findViewById(R.id.recyclerview1);
recylerViewLayoutManager = new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false);
recyclerView.setLayoutManager(recylerViewLayoutManager);
recyclerViewAdapter = new RecyclerViewAdapter(MainActivity.this, subjects,images);
recyclerView.setAdapter(recyclerViewAdapter);
MainGridRecView = (RecyclerView) findViewById(R.id.maingridrc);
MainGridRecViewLayoutManager = new GridLayoutManager(context,2);
MainGridRecView.setLayoutManager(MainGridRecViewLayoutManager);
MainGridRecViewAdapter = new MainGridRecyclerViewAdapter(MainActivity.this, subjects,images);
MainGridRecView.setAdapter(MainGridRecViewAdapter);
活动xml:
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/liner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_above="@+id/recyclerview1"
android:gravity="center"
android:background="#10bcc9"
android:textColor="@android:color/white"
android:text="New Games." />
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview1"
android:layout_width="wrap_content"
android:layout_height="140dp"
android:scrollbars="horizontal" />
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_below="@+id/recyclerview1"
android:gravity="center"
android:background="#10bcc9"
android:textColor="@android:color/white"
android:text="Used Games." />
<android.support.v7.widget.RecyclerView
android:id="@+id/maingridrc"
android:layout_width="wrap_content"
android:layout_height="330dp"
android:layout_marginTop="5dp"
android:scrollbars="vertical" />
<!-- <android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />-->
</LinearLayout>
适配器
public class RecyclerViewAdapter extends RecyclerView.Adapter<Viewholder>{
String[] SubjectValues;
String[] imageValues;
Context mContext;
View view1;
ViewHolder viewHolder1;
// TextView textView;
public RecyclerViewAdapter(Context context,String[] SubjectValues1, String[] images){
SubjectValues = SubjectValues1;
imageValues= images;
mContext = context;
}
public static class ViewHolder extends RecyclerView.ViewHolder{
public ViewHolder(View v){
super(v);
}
}
@Override
public Viewholder onCreateViewHolder(ViewGroup parent, int viewType){
view1 = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items,parent,false);
// viewHolder1 = new Viewholder(view1);
Viewholder vh = new Viewholder(view1, new Viewholder.IMyViewHolderClicks() {
public void onPotato(View caller) { Log.d("VEGETABLES", "Poh-tah-tos");
Intent intent = new Intent(mContext,SingleObjectActivity.class);
mContext.startActivity(intent);
};
public void onTomato(ImageView callerImage) {
Log.d("VEGETABLES", "To-m8-tohs");
// v1 CategoryList mDataset = new CategoryList(getOrder(), getId(), item.getUrl(), item.getUserName(), item.getLikes());
Intent intent = new Intent(mContext,SingleObjectActivity.class);
mContext.startActivity(intent);
}
});
return vh;
}
@Override
public void onBindViewHolder(Viewholder holder, int position){
holder.txtViewTitle.setText(SubjectValues[position]);
holder.imgViewIcon.setImageResource(R.drawable.ghost_recon);
}
@Override
public int getItemCount(){
return SubjectValues.length;
}
}
公共类RecycleServiceAdapter扩展了RecyclerView.Adapter{
字符串[]主题值;
字符串[]图像值;
语境;
视图1;
视窗持有者视窗持有者1;
//文本视图文本视图;
公共RecycleServiceAdapter(上下文上下文,字符串[]主题值1,字符串[]图像){
SubjectValues=SubjectValues1;
图像值=图像;
mContext=上下文;
}
公共静态类ViewHolder扩展了RecyclerView.ViewHolder{
公共视图持有者(视图v){
超级(五);
}
}
@凌驾
public Viewholder onCreateViewHolder(视图组父级,int-viewType){
view1=LayoutInflater.from(parent.getContext()).flate(R.layout.recyclerview_items,parent,false);
//viewHolder1=新的Viewholder(view1);
Viewholder vh=新的Viewholder(view1,新的Viewholder.IMyViewHolderClicks(){
公共空间(查看调用方){Log.d(“蔬菜”、“Poh tah tos”);
意向意向=新意向(mContext,SingleObjectActivity.class);
mContext.startActivity(意图);
};
public void onTomato(图像视图调用图像){
日志d(“蔬菜”、“To-m8-tohs”);
//v1 CategoryList mDataset=新的CategoryList(getOrder(),getId(),item.getUrl(),item.getUserName(),item.getLikes());
意向意向=新意向(mContext,SingleObjectActivity.class);
mContext.startActivity(意图);
}
});
返回vh;
}
@凌驾
公共无效onBindViewHolder(Viewholder,int位置){
holder.txtViewTitle.setText(SubjectValues[位置]);
holder.imgViewIcon.setImageResource(R.drawable.ghost\u侦察);
}
@凌驾
public int getItemCount(){
返回SubjectValues.length;
}
}
回收器xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/imagecateg"
android:layout_width="180dp"
android:layout_height="160dp"
android:layout_alignParentTop="true"
/>
<TextView
android:id="@+id/txtview"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="@+id/imagecateg"
android:paddingLeft="35dp"
android:textSize="15dp"
android:textStyle="bold"
android:background="#f4e04c"
android:textColor="@color/colorPrimary"
/>
</RelativeLayout>
如果要在滚动第二个回收器视图时滚动整个布局->可以将第一个回收器视图添加为第二个回收器视图的第一项,在这种情况下,它们的行为类似于一个回收器视图 我能够实现一个快速的示例,代码并不出色,但它反映了总体思路
这是第一个RecyclerView适配器中的逻辑。在布局文件中执行类似操作
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/horizenatlScrollView"
android:layout_width="match_parent"
android:layout_height="100dp" />
<android.support.v7.widget.RecyclerView
android:id="@+id/verticalScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
谢谢你的回答,你能添加一个例子或一个链接吗?你能给我一个例子或一个链接或任何东西吗?@Moudiz实现了一个快速的例子,请试着像我误解了你一样。您能描述一下top recyler视图的预期行为吗?无论第二个recyclerview如何滚动,它是否应该始终保持在顶部?还是应该从屏幕上滚动出去?@YahorPaulavets当我滚动第二个recyclerview项目时,应该滚动顶部recyclerview视图。就像两个回收器都在一个回收器中查看一样。明白吗?当我添加你提到的属性时,我根本无法搜索第二个回收器。请在设置适配器之前将这些属性设置为垂直回收视图。请检查我是否为你准备了代码示例。我跟踪了你的链接,但我在哪里找到了代码?在应用程序内部?
CustomAdapterHorizenatol horizontalAdapter = new
CustomAdapterHorizenatol(horizontalList);
LinearLayoutManager horizontalLayoutManagaer
= new LinearLayoutManager(MainActivity.this,
LinearLayoutManager.HORIZONTAL, false);
horizontal_recycler_view.setLayoutManager(horizontalLayoutManagaer);
horizontal_recycler_view.setAdapter(horizontalAdapter);
CustomAdapterVertical vertical = new
CustomAdapterVertical(horizontalList);
verticalScrollView.setNestedScrollingEnabled(false);
verticalScrollView.setHasFixedSize(false);
LinearLayoutManager verticallLayoutManagaer
= new LinearLayoutManager(MainActivity.this,
LinearLayoutManager.VERTICAL, false);
verticalScrollView.setLayoutManager(verticallLayoutManagaer);
verticalScrollView.setAdapter(vertical);