Android RecycleView滚动监听,以及view回收的细节
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android RecycleView滚动监听,以及view回收的细节相关的知识,希望对你有一定的参考价值。
参考技术A 主要细节是,如果要在recycleView在回收的同时,做UI调整容易出现的问题。一、xml可见的最外层设置为margin,view的回收还有添加的时间点可能不好计算,容易出现卡顿的情况,
二、如果是实用方法smoothScrollBy,第一个view刚好滚出可视区的时候是没有被回收的,技巧就是滚动距离加1
没图没真相,以下是业务需求的效果
首先想法是通过behavior解决问题,但是后来发现behavior需要好多个,各种相互观察好麻烦,所以想一下可不可以直接使用RecycleView,通过滚动监听改变图片大小实现功能,初步想法是第一个可视view不断变小,第二个可视的view不断变大,如下图
滚动的初步想法是这样的通过滚动距离dx,来改变view的大小
但是后来发现dx,效果不行,有兴趣的同学自己试一下,所以想了另外一个思路,根据下标为2的view也就是第三本书的位置,修改前面两本书的宽度,所以不需要dx了,这里顺便提一下,书本这个view是重写了ImageView的,把宽高比固定为3:4,所以只需要不断的改变书本的宽度就好了,其中bookWidth 为小书本的原始高度
到这里基本上就实现了滚动效果了,但是业务需求是当手指不在滑动的时候,不允许书本被覆盖的,所以这里就要,监听滚动状态
这个地方必须使用smoothScrollBy,不能是ScrollBy,因为ScrollBy不会回调onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy)这个方法,造成view宽度没有重新设定。
项目地址: https://gitee.com/feng87/BookShelf
RecycleView的使用+自定义监听事件
最近使用了RecycleView,发下这个控件十分好用,替代了listView和GridView,包括适配器都很方便。
效果如下:
具体使用如下所示:
1 compile ‘com.android.support:recyclerview-v7:25.3.1‘
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.bway.recycleviewtest.MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v7.widget.RecyclerView> </LinearLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="vertical" > <!--android:orientation="horizontal"--> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/txt_title" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
MainActivity
package com.bway.recycleviewtest; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private List<MyImage> mList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); RecyclerView mRecycler = (RecyclerView) findViewById(R.id.recycler); /** * listView */ LinearLayoutManager manager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false); mRecycler.setLayoutManager(manager); HomeAdapter adapter = new HomeAdapter(this,mList); mRecycler.setAdapter(adapter); /** * GridView */ /* GridLayoutManager manager1 = new GridLayoutManager(this,3,GridLayoutManager.VERTICAL,false); mRecycler.setLayoutManager(manager1); HomeAdapter adapter = new HomeAdapter(this,mList); mRecycler.setAdapter(adapter);*/ /** * 瀑布流 */ /* StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL); mRecycler.setLayoutManager(manager); HomeAdapter adapter = new HomeAdapter(this,mList);*/ /** * 设置自定义的条目点击事件 */ adapter.setOnItemClickLisitenter(new RecycleViewLisitenter.onItemClickLisitenter() { @Override public void onItemClick(View v, int position) { Toast.makeText(MainActivity.this,position+"",Toast.LENGTH_SHORT).show(); } }); /** * 设置自定义的条目长按监听 */ adapter.setOnItemLongClickLisitenter(new RecycleViewLisitenter.onItemLongClickLisitenter() { @Override public void onItemLongClick(View v, int position) { Toast.makeText(MainActivity.this,position+"long",Toast.LENGTH_SHORT).show(); } }); mRecycler.setAdapter(adapter); } /** * 初始化数据 */ private void initData() { mList = new ArrayList<>(); mList.add(new MyImage("第一张","https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1497864641&di=7a7a381bca514d830aea92d66a7f9024&src=http://pic.58pic.com/58pic/13/61/00/61a58PICtPr_1024.jpg")); mList.add(new MyImage("第二张","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1497874818536&di=6ddb63c3e66ac32feb5762d8d5c1fd42&imgtype=0&src=http%3A%2F%2Fpic.58pic.com%2F58pic%2F13%2F60%2F97%2F48Q58PIC92r_1024.jpg")); mList.add(new MyImage("第三张","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1497874818535&di=9d786693e181801447e49c37bc2bc627&imgtype=0&src=http%3A%2F%2Fi2.sanwen.net%2Fdoc%2F1608%2F704-160PQ43458.png")); mList.add(new MyImage("第四张","https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1497864813&di=64e84994c2f8864a69dab604d6f4d3a0&src=http://p4.gexing.com/qqpifu/20120821/1212/50330ab3e811f.jpg")); mList.add(new MyImage("第五张","http://up.qqjia.com/z/18/tu19139_7.jpg")); } }
HomeAdapter
package com.bway.recycleviewtest; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import com.bumptech.glide.Glide; import java.util.List; public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder> implements View.OnClickListener, View.OnLongClickListener { Context context; List<MyImage> mList; private RecycleViewLisitenter.onItemClickLisitenter onItem; private RecycleViewLisitenter.onItemLongClickLisitenter onLongItem; public HomeAdapter(Context context, List<MyImage> mList) { this.context = context; this.mList = mList; } public void setOnItemLongClickLisitenter(RecycleViewLisitenter.onItemLongClickLisitenter onLongItem){ this.onLongItem = onLongItem; } public void setOnItemClickLisitenter(RecycleViewLisitenter.onItemClickLisitenter onItem){ this.onItem = onItem; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(context, R.layout.item, null); view.setOnClickListener(this); view.setOnLongClickListener(this); ViewHolder holder = new ViewHolder(view); return holder; } @Override public void onBindViewHolder(ViewHolder holder, int position) { /** * 给itemView设置tag */ holder.itemView.setTag(position); MyImage myImage = mList.get(position); Glide.with(context).load(myImage.getUrl()).into(holder.mImage); holder.mTxt.setText(myImage.getTitle()); } @Override public int getItemCount() { return mList.size(); } /** * item点击事件 * @param v */ @Override public void onClick(View v) { if(onItem!=null){ onItem.onItemClick(v, (Integer) v.getTag()); } } @Override public boolean onLongClick(View v) { if(onLongItem!=null){ onLongItem.onItemLongClick(v, (Integer) v.getTag()); } return true; } class ViewHolder extends RecyclerView.ViewHolder{ ImageView mImage; TextView mTxt; public ViewHolder(View itemView) { super(itemView); mImage = (ImageView) itemView.findViewById(R.id.image); mTxt = (TextView) itemView.findViewById(R.id.txt_title); } } }
RecycleViewLisitenter(自定义条目点击事件)
package com.bway.recycleviewtest; import android.view.View; public class RecycleViewLisitenter { /** * RecycleView的条目点击监听 */ public interface onItemClickLisitenter{ void onItemClick(View v,int position); }; /** * RecycleView的条目长按点击监听 */ public interface onItemLongClickLisitenter{ void onItemLongClick(View v,int position); }; }
以上是关于Android RecycleView滚动监听,以及view回收的细节的主要内容,如果未能解决你的问题,请参考以下文章
向下滚动以使用 recycleview 和 SwipeRefreshLayout 加载数据