RecyclerView多条目加载

来源:互联网 发布:淘宝贷款不还会怎么样 编辑:程序博客网 时间:2024/06/04 18:21

废话不多说,直接上代码

ManiActivity里代码

public class MainActivity extends AppCompatActivity {    private RecyclerView recycler_view;    private List<Bean> list;    private int[] icons={R.drawable.m,R.drawable.mm,R.drawable.mmm,R.drawable.mmmm,R.drawable.mmmmm};    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //初始化控件        initView();        //初始化数据        initData();        //List效果展示        //initViewOper();        //瀑布流展示        initViewStaggered();    }    //初始化控件    private void initView() {        recycler_view = (RecyclerView) findViewById(R.id.recycler_view);    }    //初始化数据    private void initData() {        //创建集合        list = new ArrayList<>();        //随机数        Random random = new Random();        //填充数据        for (int i = 0; i < icons.length ; i++) {            Bean bean = new Bean();            bean.pic=icons[i];            bean.type=random.nextInt(3);            list.add(bean);        }    }    //展示效果    private void initViewOper() {        //布局管理器        LinearLayoutManager lineatlayoutmanager = new LinearLayoutManager(this);        recycler_view.setLayoutManager(lineatlayoutmanager);        //创建适配器        Recycler_adapter adapter = new Recycler_adapter(list);        //设置适配器        recycler_view.setAdapter(adapter);    }    private void initViewStaggered(){        //布局管理器        StaggeredGridLayoutManager staggerd = new StaggeredGridLayoutManager(3,LinearLayoutManager.VERTICAL);        recycler_view.setLayoutManager(staggerd);        //创建适配器        Recycler_adapter adapter = new Recycler_adapter(list);        //设置适配器        recycler_view.setAdapter(adapter);    }
}
适配器里代码
public class Recycler_adapter extends RecyclerView.Adapter{    //定义三种常量  表示三种条目类型    public static final int TYPE_PULL_IMAGE = 0;    public static final int TYPE_RIGHT_IMAGE = 1;    public static final int TYPE_THREE_IMAGE = 2;    private List<Bean> list;    public Recycler_adapter(List<Bean> list) {        this.list = list;    }    @Override    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {        View view ;        if(i == TYPE_PULL_IMAGE){            view = View.inflate(viewGroup.getContext(),R.layout.item_pull_img,null);            return new PullimageHolder(view);        }else if(i == TYPE_RIGHT_IMAGE){            view = View.inflate(viewGroup.getContext(),R.layout.item_right_img,null);            return new RightimageHolder(view);        }else{            view = View.inflate(viewGroup.getContext(),R.layout.item_three_img,null);            return new ThreeimageHolder(view);        }    }    @Override    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {    }    @Override    public int getItemCount() {        if(list != null && list.size() > 0){            return list.size();        }        return 0;    }    @Override    public int getItemViewType(int position) {        Bean bean = list.get(position);        if(bean.type==0){            return TYPE_PULL_IMAGE;        }else if(bean.type == 1){            return TYPE_RIGHT_IMAGE;        }else{            return  TYPE_THREE_IMAGE;        }    }    private class PullimageHolder extends RecyclerView.ViewHolder{        public PullimageHolder(View itemView) {            super(itemView);        }    }    private class RightimageHolder extends  RecyclerView.ViewHolder{        public RightimageHolder(View itemView) {            super(itemView);        }    }    private class ThreeimageHolder extends  RecyclerView.ViewHolder{        public ThreeimageHolder(View itemView) {            super(itemView);        }    }}
XML布局代码
item_pull_img布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:orientation="vertical">    <LinearLayout        android:layout_width="match_parent"        android:padding="7dp"        android:background="#fff"        android:orientation="vertical"        android:layout_height="wrap_content">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textColor="#000"            android:text="多种条目类型,多种条目类型,多种条目类型,多种条目类型,多种条目类型"            android:textSize="16sp"            />        <ImageView            android:layout_width="match_parent"            android:layout_height="150dp"            android:background="@drawable/m"            android:scaleType="fitXY"            />    </LinearLayout>    <View        android:layout_marginTop="3dp"        android:layout_width="match_parent"        android:layout_height="1dp"        android:background="#d3d3d3"        /></LinearLayout>
item_right_img代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="100dp"    android:orientation="vertical">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="#fff"        android:orientation="horizontal"        android:padding="7dp">        <TextView            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="多种条目类型,多种条目类型,多种条目类型,多种条目类型,多种条目类型,多种条目类型,多种条目类型,多种条目类型"            android:textColor="#000"            android:textSize="16sp" />        <ImageView            android:layout_width="120dp"            android:layout_height="90dp"            android:background="@drawable/mm" />    </LinearLayout>    <View        android:layout_marginTop="3dp"        android:layout_width="match_parent"        android:layout_height="1dp"        android:background="#d3d3d3"        /></LinearLayout>

item_three_img代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:orientation="vertical">    <LinearLayout        android:orientation="vertical"        android:layout_width="match_parent"        android:background="#fff"        android:padding="7sp"        android:layout_height="wrap_content">        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="多种条目类型,多种条目类型,多种条目类型,多种条目类型,多种条目类型,多种条目类型多种条目类型"            android:textColor="#000"            android:textSize="16sp" />        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal">            <ImageView                android:layout_width="0dp"                android:layout_height="80dp"                android:layout_weight="1"                android:scaleType="fitXY"                android:src="@drawable/mmm" />            <View                android:layout_width="6dp"                android:layout_height="0dp"/>            <ImageView                android:layout_width="0dp"                android:layout_height="80dp"                android:layout_weight="1"                android:scaleType="fitXY"                android:src="@drawable/mmmm" />            <View                android:layout_width="6dp"                android:layout_height="0dp"/>            <ImageView                android:layout_width="0dp"                android:layout_height="80dp"                android:layout_weight="1"                android:scaleType="fitXY"                android:src="@drawable/mmmmm" />        </LinearLayout>    </LinearLayout>    <View        android:layout_marginTop="3dp"        android:background="#d3d3d3"        android:layout_width="match_parent"        android:layout_height="1dp"/></LinearLayout>

ManiActivyty布局
<android.support.v7.widget.RecyclerView    android:id="@+id/recycler_view"    android:layout_width="match_parent"    android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>