Recyclerview style
来源:互联网 发布:什么源码值得读知乎 编辑:程序博客网 时间:2024/06/05 11:40
以下是 recyclerview 的其中的一种样式如下效果:
1.首先在 app.gralde下
compile 'com.android.support:recyclerview-v7:23.2.1' compile 'com.android.support:cardview-v7:23.0.1'
导入。
2.activity.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical"></android.support.v7.widget.RecyclerView> </RelativeLayout>
3.建一个custom.xml
<?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://schemas.android.com/apk/res-auto" android:id="@+id/card_view" card_view:cardCornerRadius="4dp" card_view:cardElevation="1dp" card_view:cardMaxElevation="2dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="2dp" android:fontFamily="sans-serif-condensed" android:gravity="center" android:padding="5dp" android:textColor="#000" android:textSize="20dp"/></android.support.v7.widget.CardView>
Mactivity.java代码
package listview.mifeng.us.recyclerviewdemo;import android.content.Context;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.support.v7.widget.RecyclerView;import android.graphics.Color;import android.graphics.drawable.ColorDrawable;import android.support.v7.widget.StaggeredGridLayoutManager;import android.view.Window;import android.widget.RelativeLayout;public class MainActivity extends AppCompatActivity { private Context mContext; private RecyclerView mRecyclerView; private RecyclerView.Adapter mAdapter; private RecyclerView.LayoutManager mLayoutManager; @Override protected void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_ACTION_BAR); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = getApplicationContext(); getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.RED)); mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); String[] colors = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N"}; mLayoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL); mRecyclerView.setLayoutManager(mLayoutManager); mAdapter = new ColorAdapter(mContext, colors); mRecyclerView.setAdapter(mAdapter); }}
ColorAdpter 代码
package listview.mifeng.us.recyclerviewdemo;/** * Created by 孙贝贝 on 2016/11/23. */import android.support.v7.widget.RecyclerView;import android.content.Context;import android.graphics.Color;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;import java.util.Random;public class ColorAdapter extends RecyclerView.Adapter<ColorAdapter.ViewHolder> { private String[] mDataSet; private Context mContext; private Random mRandom = new Random(); public ColorAdapter(Context context, String[] DataSet) { mDataSet = DataSet; mContext = context; } public static class ViewHolder extends RecyclerView.ViewHolder { public TextView mTextView; public ViewHolder(View v) { super(v); mTextView = (TextView) v.findViewById(R.id.tv); } } @Override public ColorAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(mContext).inflate(R.layout.custom_view, parent, false); ViewHolder vh = new ViewHolder(v); return vh; } @Override public void onBindViewHolder(ViewHolder holder, int position) { holder.mTextView.setText(mDataSet[position]); holder.mTextView.getLayoutParams().height = getRandomIntInRange(250, 75); holder.mTextView.setBackgroundColor(getRandomHSVColor()); } @Override public int getItemCount() { return mDataSet.length; } protected int getRandomIntInRange(int max, int min) { return mRandom.nextInt((max - min) + min) + min; } protected int getRandomHSVColor() { int hue = mRandom.nextInt(361); float saturation = 1.0f; float value = 1.0f; int alpha = 255; int color = Color.HSVToColor(alpha, new float[]{hue, saturation, value}); return color; }}
如以上就可以完成效果;
1 0
- Recyclerview style
- RecyclerView添加footer实现上拉加载的自定义style
- Style
- Style
- Style
- Style
- Style
- style
- style
- Style
- style
- Style
- style
- style
- style
- RecyclerView
- RecyclerView
- RecyclerView
- Python 修饰图片
- Android WebView使用以及与JavaScript进行交互实例
- java定义静态变量时合并多个List
- 第十三周项目3-Dijkstra算法验证
- Spring(一)——总体介绍
- Recyclerview style
- (0010) iOS 开发之UI布局兼容 4s/5/6/7 屏幕解决方案
- leetcode 404 Sum of Left Leaves
- 修改linux的文件权限命令 chmod
- 上楼梯---动态规划问题
- 第十二周项目5--迷宫问题之图深度优先遍历解法
- Spring集成HttpClient实现跨域请求
- spring框架,可以没有spring.xml(即配置<beans>的文件吗?即不使用xml方式配置,而是只使用注解的方式配置
- session 过期设置