The earlier chapters and two are actually used in almost all things are almost the same just a little small change
The first renderings
The structure of each presentation and the previous is the same as everyone interested can go to the next article on one of the following paste Portal
RecyclerView study concluded (B) - to scroll horizontally, grid layout
He started writing a cascade of small modules
First create a new name is PuRecyclerActivity.java Activity
then
acticity_recycler_view.xml document which add a jump button Button used to make the code as follows
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:id="@+id/btn_linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="列表视图"/>
<Button
android:id="@+id/btn_hor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="水平滚动"/>
<Button
android:id="@+id/btn_grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="网格视图"/>
<Button
android:id="@+id/btn_pu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="瀑布流"/>
</LinearLayout>
Jump in and then write the code inside RecyclerViewActivity.java
package lee.example.com.test.recycleview;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import lee.example.com.test.R;
import lee.example.com.test.gridview.GridViewActivity;
//进入各个RecyclerView项目Activity跳转界面!
public class RecyclerViewActivity extends AppCompatActivity {
private Button mBtnLinear,mBtnHor,mBtnGrid,mBtnPu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler_view);
//找到控件
mBtnGrid = findViewById(R.id.btn_grid);
mBtnLinear = findViewById(R.id.btn_linear);
mBtnHor = findViewById(R.id.btn_hor);
mBtnPu = findViewById(R.id.btn_pu);
//各个跳转界面
mBtnLinear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(RecyclerViewActivity.this,LinearRecyclerViewActivity.class);
startActivity(intent);
}
});
mBtnHor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(RecyclerViewActivity.this,HorRecyclerViewActivity.class);
startActivity(intent);
}
});
mBtnGrid.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(RecyclerViewActivity.this,GridRecyclerViewActivity.class);
startActivity(intent);
}
});
mBtnPu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(RecyclerViewActivity.this,PuRecyclerViewActivity.class);
startActivity(intent);
}
});
}
}
And then write the code in this acticity inside PuRecyclerActivity.java
package lee.example.com.test.recycleview;
import android.annotation.SuppressLint;
import android.graphics.Rect;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.Toast;
import lee.example.com.test.R;
public class PuRecyclerViewActivity extends AppCompatActivity {
private RecyclerView mRvPu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pu_recycler_view);
mRvPu = findViewById(R.id.rv_pu);
//瀑布流布局管理器 第一个是列 第二个是水平方向
mRvPu.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
//绘制边框
mRvPu.addItemDecoration(new MyDecoration());
mRvPu.setAdapter(new StaggeredGridAdapter(PuRecyclerViewActivity.this, new StaggeredGridAdapter.OnItemClickListener() {
@Override
public void onClick(int pos) {
Toast.makeText(PuRecyclerViewActivity.this,"click"+pos,Toast.LENGTH_SHORT).show();
}
}));
}
//设置一个边框线
class MyDecoration extends RecyclerView.ItemDecoration{
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
//使用视图为dimen里面都dividerHeight2属性
int gap = getResources().getDimensionPixelSize(R.dimen.dividerHeight2);
//设置边框属性
outRect.set(gap,gap,gap,gap);
}
}
}
Then write the code in the activity_pu_recycler_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_pu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorGrayDark"/>
</LinearLayout>
Then write the code in this Activity in PuRecyclerViewActivity
package lee.example.com.test.recycleview;
import android.annotation.SuppressLint;
import android.graphics.Rect;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.Toast;
import lee.example.com.test.R;
public class PuRecyclerViewActivity extends AppCompatActivity {
private RecyclerView mRvPu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pu_recycler_view);
mRvPu = findViewById(R.id.rv_pu);
//瀑布流布局管理器 第一个是列 第二个是水平方向
mRvPu.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
//绘制边框
mRvPu.addItemDecoration(new MyDecoration());
mRvPu.setAdapter(new StaggeredGridAdapter(PuRecyclerViewActivity.this, new StaggeredGridAdapter.OnItemClickListener() {
@Override
public void onClick(int pos) {
Toast.makeText(PuRecyclerViewActivity.this,"click"+pos,Toast.LENGTH_SHORT).show();
}
}));
}
//设置一个边框线
class MyDecoration extends RecyclerView.ItemDecoration{
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
//使用视图为dimen里面都dividerHeight2属性
int gap = getResources().getDimensionPixelSize(R.dimen.dividerHeight2);
//设置边框属性
outRect.set(gap,gap,gap,gap);
}
}
}
The inside should be noted that this and previous Manager has changed there to explain comments
Then write a Adapter to create a new StaggeredGridAdapter.java
Content
package lee.example.com.test.recycleview;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import lee.example.com.test.R;
/***
*Create By Lee On 2019/1/9
**/
public class StaggeredGridAdapter extends RecyclerView.Adapter<StaggeredGridAdapter.LinearViewHolder> {
private Context mContext;
private OnItemClickListener mListener;
public StaggeredGridAdapter(Context context, OnItemClickListener listener) {
this.mContext = context;
this.mListener= listener;
}
@Override
public StaggeredGridAdapter.LinearViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new LinearViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_staggered_recycleview_ltem,parent,false));
}
@Override
public void onBindViewHolder(StaggeredGridAdapter.LinearViewHolder holder, final int position) {
//设置图片 if函数是基数为bg_iron_man偶数为inmage1
if(position % 2 !=0){
holder.imageView.setImageResource(R.drawable.image2);
}else {
holder.imageView.setImageResource(R.drawable.image1);
}
//设置点击事件方法
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onClick(position);
}
});
}
@Override
public int getItemCount() {
return 102;
}//list数量
//做基础工作。。。
class LinearViewHolder extends RecyclerView.ViewHolder{
private ImageView imageView;
public LinearViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.iv);
}
}
//设计点击事件接口!
public interface OnItemClickListener{
void onClick(int pos);
}
}
The Adapter which set the picture content base and then set it to display a picture of it even display pictures
The inside need to create a layout_staggered_recycleview_ltem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffff">
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"/>
</LinearLayout>
Because it is stored inside a waterfall stream so ImageView control is used to hold pictures