1.在主页面中添加Recyclerview
2.新建两个布局文件,一个是标题栏布局,一个做内容布局
Adapter参照这里
package com.example.test.jiecaoplayer.adapter;
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.TextView;
import com.example.test.jiecaoplayer.R;
import java.util.ArrayList;
import java.util.List;
/**
* Created by app-i on 2018/3/23.
*/
public class AddHeadRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private int TITLE = 0; //标题要在第0行
private int CONTENT = 1;
private Context context;
private final List<String> data;
public AddHeadRvAdapter(Context context) {
this.context = context;
data = new ArrayList<>();
for (int i = 0; i < 8; i++) {
data.add("" + i);
}
}
@Override
public int getItemViewType(int position) {
if (position == TITLE) { //只在第一行显示标题栏
return TITLE;
}
return CONTENT; //其他的全部返回内容
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TITLE) {
View view = LayoutInflater.from(context).inflate(R.layout.item_rv_title, parent, false);
return new TitleViewHolder(view);
} else {
View view = LayoutInflater.from(context).inflate(R.layout.item_rv_content, parent, false);
return new ContentViewHolder(view);
}
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof TitleViewHolder) {
((TitleViewHolder) holder).head1.setText("1标题" + data.get(position));
((TitleViewHolder) holder).head2.setText("2标题" + data.get(position));
} else if (holder instanceof ContentViewHolder) {
((ContentViewHolder) holder).conten1.setText("1内容" + data.get(position - 1));
((ContentViewHolder) holder).conten2.setText("2内容" + data.get(position - 1));
}
}
@Override
public int getItemCount() {
return data.size();
}
public class TitleViewHolder extends RecyclerView.ViewHolder {
public TextView head1;
public TextView head2;
public TitleViewHolder(View itemView) {
super(itemView);
head1 = itemView.findViewById(R.id.head1);
head2 = itemView.findViewById(R.id.head2);
}
}
public class ContentViewHolder extends RecyclerView.ViewHolder {
private TextView conten1;
private TextView conten2;
public ContentViewHolder(View itemView) {
super(itemView);
conten1 = itemView.findViewById(R.id.conten1);
conten2 = itemView.findViewById(R.id.conten2);
}
}
}