在Recyclerview适配器中获取项目是否完全可见

时间:2018-04-25 08:59:44

标签: android android-layout android-recyclerview

我正在制作一个Android应用程序,其中RecyclerView包含GIF列表,现在我想开发这样的功能,例如当GIF项目对用户完全可见时然后它开始加载,一旦它稍微失去了用户的焦点应该停止加载。

我已经在recyclerview中显示了GIF,现在剩下的唯一部分就是如何让这样的GIF完全可见或失去焦点。我想在适配器中使用这样的代码,表明这个项目是完全可见的,这个项目失去了焦点。

MY Adapter类

public class FeedAdapter extends RecyclerView.Adapter<FeedAdapter.MyViewHolder> {


private List<FeedModel> feedList;
Context context;

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.single_item, parent, false);

    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
    holder.setData(feedList.get(position));

    new GifDataDownloader() {
        @Override
        protected void onPostExecute(final byte[] bytes) {
            holder.gifImageView.setBytes(bytes);
            holder.gifImageView.startAnimation();
        }
    }.execute(feedList.get(position).getUrl());


}

public FeedAdapter(Context context, List<FeedModel> feedList) {
    this.feedList = feedList;
    this.context = context;
}

@Override
public int getItemCount() {
    return feedList.size();
}

public void setGridData(ArrayList<FeedModel> feedList) {
    this.feedList = feedList;
    notifyDataSetChanged();
}

public class MyViewHolder extends RecyclerView.ViewHolder  {

    FeedModel item;
    GifImageView gifImageView;
    public MyViewHolder(View itemView) {
        super(itemView);
        gifImageView = itemView.findViewById(R.id.gifImageView);
    }

    public void setData(FeedModel item) {
        this.item = item;
    }
}

我的活动

public class MainActivity extends AppCompatActivity {
ImageView imageView;
ImageView imageView1;
private PendingIntent pendingIntent;
RecyclerView recyclerView;
FeedAdapter mAdapter;
ProgressBar progressBar;
GridLayoutManager manager;
Spinner countrySpinner;
ArrayList<FeedModel> feedList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    feedList = new ArrayList<>(10);
    for (int i = 0; i < 10; i++) {
        FeedModel feedModel = new FeedModel();
        feedModel.setUrl("https://media.tenor.com/images/925bfbaad2f947987bcf18b9167b3326/tenor.gif");
        feedList.add(feedModel);
    }


    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    mAdapter = new FeedAdapter(getApplicationContext(), feedList);
    manager = new GridLayoutManager(getApplicationContext(), 1, GridLayoutManager.VERTICAL, false);
    recyclerView.setLayoutManager(manager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setVisibility(View.VISIBLE);
    recyclerView.setAdapter(mAdapter);

}

2 个答案:

答案 0 :(得分:0)

更好的方法是使用滑动来显示图像和gif等,这需要你定义的所有高级。

将以下依赖项添加到应用程序级别的gradle文件

    implementation 'com.github.bumptech.glide:glide:4.7.1'

然后像这样在适配器中使用..

private Context context;
  // define context into constructor 
  public RecyclerviewAdapter(List<StepsItem> stepsItems, Context context) {
    this.stepsItems = stepsItems;
    this.context = context;
}

 Glide.with(context).load(yoururl).into(holder.mIvIcon);

答案 1 :(得分:-1)

onstart()方法中创建视图后启动gif 如果您想在用户不再与应用互动时停止gif,请停用onpause()方法中的gif。