在滚动Receierview Android的同时滚动父布局
问题说明
请帮助我解决以下情况。
我正在开发一个应用程序,在其中一个页面中,我有一个带有背景图像的LinearLayout和带有名称列表的RecillerView。我需要的是,当我向上滚动RecclerView时,我需要上面的LinearLayout也向上移动,以便回收视图中的列表不会位于上面的LinearLayout下面,而当我向下滚动ReccyerView时,我需要向下滚动上面的LinearLayout,以便我们可以完整地看到图像。
我已经做过的事情是使用Rececumerview的setOnScrollListener,在onScrolLED()函数中,我得到了向下和向上滚动的事件。但现在我遇到了如何继续下去的问题。
下面是布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<include layout="@layout/toolbar"
android: />
<android.support.v7.widget.CardView
android:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@ id/toolbar"
android:layout_margin="8dp"
card_view:cardBackgroundColor="@android:color/white"
card_view:cardCornerRadius="8dp">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="5"
android:orientation="vertical">
<LinearLayout
android:
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:background="@drawable/hydeparkmap"
android:layout_weight="3" ></LinearLayout>
<android.support.v7.widget.RecyclerView
android:
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:scrollbars="vertical" />
</LinearLayout>
</ScrollView>
</android.support.v7.widget.CardView>
</RelativeLayout>
以下是我在相应的Java类中使用的代码:
@InjectView(R.id.scrollview)
ScrollView scrollview;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shoplist);
ButterKnife.inject(this);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setTitle("ShopList");
scrollview.setVerticalScrollBarEnabled(true);
lm=new LinearLayoutManager(ShopListActivity.this);
recyclerView.setLayoutManager(lm);
firstVisibleInListview = lm.findFirstVisibleItemPosition();
adapter = new RecyclerViewAdapter(ShopListActivity.this, getData());
recyclerView.setAdapter(adapter);
recyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(ShopListActivity.this, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Intent intent1 = new Intent(ShopListActivity.this, ShopProductListActivity.class);
intent1.putExtra("position", String.valueOf(position));
intent1.putExtra("shopname", it.get(position).getTitle());
intent1.putExtra("shopimage", String.valueOf(it.get(position).getImgIcon()));
intent1.putExtra("subcategory", subcategory);
startActivity(intent1);
}
})
);
recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int currentFirstVisible = lm.findFirstVisibleItemPosition();
if(currentFirstVisible > firstVisibleInListview){
Toast.makeText(getBaseContext(),"Scrolled up ",Toast.LENGTH_SHORT).show();
scrollview.fullScroll(ScrollView.FOCUS_UP);
}
else
Toast.makeText(getBaseContext(),"Scrolled down ",Toast.LENGTH_SHORT).show();
firstVisibleInListview = currentFirstVisible;
}
});
}
正确答案
如果我完全理解您的问题,您应该滚动的某个视图 RCyclerView。 如果是这样的话,在这种情况下,你应该删除你的ScrollView,因为循环视图本身就有它。我在这里写的是一步一步的指南,将标题(不是粘性的)放在您的Recloop View的顶部,以便它们可以一起滚动:
1-首先,您需要创建一个包含页眉的布局,在我的例子中,我有一个页眉图像,因此它看起来如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="https://www.swvq.com/@mipmap/winter"/>
</LinearLayout>
我们将其命名为Header_Layout
2-您需要实现另一个名为getItemViewType的RecrecerAdapter方法,它的输出将作为第二个参数放在CreateViewHolder(ViewGroup父级,int viewType)中。这里,您可以为所需的每种视图扩展布局(对我来说,这两种视图如下所示):
@Override
public int getItemViewType(int position){
if(position == 0){
return 0;
} else {
return 1;
}
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView;
if(viewType==1){
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.report_row_layout, parent, false);
} else {
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.report_header_layout, parent, false);
}
return new MyViewHolder(itemView);
}
请注意,我的第一个职位(表头)不同,其他职位相同,您可以为多个职位提供多个视图。
3-如果需要,您应该更改您的onBindViewHolder,在我的情况下,我需要让它对我的第一个职位没有任何作用
4-移除ScrollView并按位置实现布局,您的主布局应该如下所示:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<include layout="@layout/toolbar"
android: />
<android.support.v7.widget.CardView
android:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@ id/toolbar"
android:layout_margin="8dp"
card_view:cardBackgroundColor="@android:color/white"
card_view:cardCornerRadius="8dp">
<android.support.v7.widget.RecyclerView
android:
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v7.widget.CardView>
</RelativeLayout>
我不知道您的CardView在做什么,但如果您认为不需要,请将其删除。如果需要,您可以将页眉设置为LinearLayout或其他任何形式。
希望这能有所帮助。
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanfcajh
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
检查邮件是否发送成功
it1352 07-25 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20