码迷,mamicode.com
首页 > 其他好文 > 详细

使用StaggeredGridLayoutManager实现瀑布流效果

时间:2015-04-10 15:37:19      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:android   studio   瀑布流   supportv7   

开发环境: Android Studio

准备工作:打开项目的build.gradle 添加对support V7的引用 代码如下

dependencies {
    ...
    compile ‘com.android.support:appcompat-v7:21.0.3‘
    ...
}
  • 首先创建一个包含RecyclerView的XML布局文件activity_main.xml
<android.support.v7.widget.RecyclerView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/recyclerView"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

此处的RecyclerView 是纵向的

  • 在MainActivity中完成视图部分代码
public class MainActivity extends Activity {
    private RecyclerView mRecyclerView;
    private ItemAdapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private ArrayList<String> items = new ArrayList<>();

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

    private void initView() {
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        //列数为两列
        int spanCount = 2;
        mLayoutManager = new StaggeredGridLayoutManager(
            spanCount, 
            StaggeredGridLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(mLayoutManager);
        //构建一个临时数据源
        for (int i = 0; i < 100; i++) {
            items.add("i:" + i);
        }
        mAdapter = new ItemAdapter(items);
        mRecyclerView.setAdapter(mAdapter);
    }

}
  • ItemAdapter 的代码如下:
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.viewHolder> {
    private ArrayList<String> items = new ArrayList<>();

    public ItemAdapter(ArrayList<String> items) {
        this.items = items;
    }

    @Override
    public viewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_card,
                viewGroup, false);
        return new viewHolder(view);
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public void onBindViewHolder(viewHolder viewHolder, int position) {
        String info = items.get(position);
        View view = viewHolder.itemView;
        TextView textView = (TextView) view.findViewById(R.id.info_text);
        textView.setText(info);
        //手动更改高度,不同位置的高度有所不同
        textView.setHeight(100 + (position % 3) * 30);
    }

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

    class viewHolder extends RecyclerView.ViewHolder {
        public viewHolder(View itemView) {
            super(itemView);
        }
    }
}
  • item_card.xml布局文件的代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:gravity="center"
    android:layout_height="fill_parent">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_gravity="center"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        card_view:cardUseCompatPadding="true"
        card_view:cardCornerRadius="4dp">

        <TextView
            android:id="@+id/info_text"
            android:layout_width="fill_parent"
            android:gravity="center"
            android:layout_height="wrap_content" />
    </android.support.v7.widget.CardView>
</LinearLayout>

此处使用了CardView,如想正确调用到CardView,在build.gradle中添加对cardview的引用代码如下

dependencies {
    ...
    compile ‘com.android.support:cardview-v7:21.0.3‘
    ...
}
  • 最后Build项目后的运行结果如下所示:

技术分享

使用StaggeredGridLayoutManager实现瀑布流效果

标签:android   studio   瀑布流   supportv7   

原文地址:http://blog.csdn.net/duanymin/article/details/44979355

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!