activity_luncher
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ActivityLuncher">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linearRv"
android:dividerHeight="10dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>`
item_2_layout
`<?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="wrap_content"
android:background="@android:color/holo_blue_dark">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/item_2_tv"
android:gravity="center"
android:padding="10dp"/>
</LinearLayout>
item_layout
<?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="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/item_tv"
android:gravity="center"
android:padding="10dp"/>
</LinearLayout>
MainActivity
package com.example.luncherdemo1;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import android.os.Bundle;
import android.widget.LinearLayout;
import java.util.ArrayList;
public class ActivityLuncher extends AppCompatActivity {
private ArrayList<String> mDatas = new ArrayList<>();
private void initDatas(){
for (int i=1;i<=100;i++){
mDatas.add("第"+i+"个item");
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_luncher);
initDatas();
RecyclerView mRv = findViewById(R.id.linearRv);
//网格
// GridLayoutManager gridLayoutManager=new GridLayoutManager(this,8);
// gridLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
// mRv.setLayoutManager(gridLayoutManager);
//瀑布流
StaggeredGridLayoutManager staggeredGridLayoutManager=new StaggeredGridLayoutManager(5,StaggeredGridLayoutManager.HORIZONTAL);
staggeredGridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRv.setLayoutManager(staggeredGridLayoutManager);
MyRecyclerAdapter adapter = new MyRecyclerAdapter(this,mDatas);
mRv.setAdapter(adapter);
}
}
MyRecyclerAdatper
package com.example.luncherdemo1;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class MyRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context mContext;
private ArrayList<String> mDatas;
public MyRecyclerAdapter(Context context, ArrayList<String> datas){
mContext = context;
mDatas = datas;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
// View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_layout,parent,false);
LayoutInflater inflater = LayoutInflater.from(mContext);
if (viewType==ITEM_TYPE.ITEM_TYPE_ITEM.ordinal()){
return new NormalHolder(inflater.inflate(R.layout.item_layout,parent,false));
}
return new SectionHolder(inflater.inflate(R.layout.item_2_layout,parent,false));
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (holder instanceof SectionHolder){
SectionHolder sectionHolder = (SectionHolder) holder;
sectionHolder.mSectionTV.setText("第"+((position/SECTION_ITEM_NUM)+1)+"组");
}else if (holder instanceof NormalHolder){
NormalHolder normalHolder=(NormalHolder) holder;
normalHolder.mTV.setText(mDatas.get(position));
}
}
public static enum ITEM_TYPE{
ITEM_TYPE_SECTION,
ITEM_TYPE_ITEM;
}
private int SECTION_ITEM_NUM = 10;
@Override
public int getItemViewType(int position) {
if (position%SECTION_ITEM_NUM==0){
return ITEM_TYPE.ITEM_TYPE_SECTION.ordinal();
}
return ITEM_TYPE.ITEM_TYPE_ITEM.ordinal();
}
@Override
public int getItemCount() {
return mDatas.size();
}
class NormalHolder extends RecyclerView.ViewHolder{
TextView mTV;
public NormalHolder(@NonNull View itemView) {
super(itemView);
mTV = (TextView) itemView.findViewById(R.id.item_tv);
mTV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(mContext,mTV.getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
class SectionHolder extends RecyclerView.ViewHolder{
TextView mSectionTV;
public SectionHolder(@NonNull View itemView) {
super(itemView);
mSectionTV = (TextView) itemView.findViewById(R.id.item_2_tv);
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.luncherdemo1">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.LuncherDemo">
<activity
android:name=".ActivityLuncher"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>