我是靠谱客的博主 现代路人,最近开发中收集的这篇文章主要介绍Android使用RecyclerView实现瀑布流,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

@Override

public void onBindViewHolder(MyViewHolder viewHolder, int i) {

super.onBindViewHolder(viewHolder,i);

Log.i(TAG, “onBindViewHolder:” + i);

//竖直瀑布流

if(isVertical){

ViewGroup.LayoutParams params 《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》无偿开源 徽信搜索公众号【编程进阶路】 = viewHolder.itemView.getLayoutParams();

int temp = mDatas.get(i).id;

Log.i(TAG, “temp:” + temp +“mHeights:”+mHeights);

int temp2 =  mHeights.get(temp);

params.height =temp2;

viewHolder.itemView.setLayoutParams(params);

}

//竖直瀑布流

//水平瀑布流

else{

ViewGroup.LayoutParams params = viewHolder.itemView.getLayoutParams();

params.width = mWidths.get(mDatas.get(i).id);

viewHolder.itemView.setLayoutParams(params);

}

//水平瀑布流

}

//重写父类 否则增加后会越界异常

@Override

public void addData(int pos, DataBean data) {

super.addData(pos, data);

onDataChanged();

}

@Override

public void removeData(int pos) {

super.removeData(pos);

onDataChanged();

}

}

Activity:

package com.mcxtzhang.demo;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.support.v7.widget.DefaultItemAnimator;

import android.support.v7.widget.RecyclerView;

import android.support.v7.widget.StaggeredGridLayoutManager;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.Toast;

import java.util.ArrayList;

import java.util.List;

public class StaggeredActivity extends AppCompatActivity {

private RecyclerView mRecyclerView;

private List mDatas;

private StaggeredAdapter mAdapter;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initDatas();

initViews();

}

private void initViews() {

mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);

mAdapter = new StaggeredAdapter(StaggeredActivity.this,mDatas);

mRecyclerView.setAdapter(mAdapter);

//设置REcyclerVIew布局管理

//三个参数context,水平/竖直排列数据 ,是否反向显示。

/*RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(StaggeredActivity.this, LinearLayoutManager.VERTICAL, true);

mRecyclerView.setLayoutManager(layoutManager);*/

//设置RecyclerView的Item分割线

//想改变分割线的样式 ,要定义一个drawable,然后在style里覆盖android:listDivider 属性

//mRecyclerView.addItemDecoration(new DividerItemDecoration(MainActivity.this, DividerItemDecoration.VERTICAL_LIST));

//瀑布流

mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL));

//设置动画

mRecyclerView.setItemAnimator(new DefaultItemAnimator());

//设置点击事件

mAdapter.setOnClickEventListener(new MyAdapter.onClickEventListener() {

@Override

public void onClickListener(View v, int pos) {

Toast.makeText(StaggeredActivity.this, “pos:” + pos, Toast.LENGTH_SHORT).show();

}

@Override

public void onLongClickListener(View v, int pos) {

Toast.makeText(StaggeredActivity

.this,“long pos:”+pos,Toast.LENGTH_SHORT).show();

mAdapter.removeData(pos);

}

});

//下面的方法不行 必须用上面的layoutmanager

//mRecyclerView.setLayoutManager(new GridLayoutManager(StaggeredActivity.this,3));

}

private void initDatas() {

mDatas = new ArrayList();

for (int i = ‘A’; i <= ‘z’; i++) {

mDatas.add(new DataBean(i,“” + (char) i));

}

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.menu_staggered, menu);

return true;

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

int id = item.getItemId();

//noinspection SimplifiableIfStatement

switch (id) {

case R.id.action_staggered_vertical:

mAdapter.setIsVertical(true);

mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL));

break;

case R.id.action_staggered_horizontal:

//水平瀑布流

mAdapter.setIsVertical(false);

mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(4,StaggeredGridLayoutManager.HORIZONTAL));

break;

//增加一个选项:

case R.id.action_add:

mAdapter.addData(1,new DataBean(1,“刁”));

break;

//删除一个选项:

case R.id.action_delete:

mAdapter.removeData(1);

break;

default:

break;

}

return super.onOptionsItemSelected(item);

}

}

Bean:

最后

以上就是现代路人为你收集整理的Android使用RecyclerView实现瀑布流的全部内容,希望文章能够帮你解决Android使用RecyclerView实现瀑布流所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(31)

评论列表共有 0 条评论

立即
投稿
返回
顶部