单各list列表实现很简单直接使用RecyclerView填充列表即可,那么多级列表该怎样实现呢,同样很简单,只需监听第一级列表的item点击事件再填充第二级列表即可实现。
由两个RecyclerView填充布局,左右分布
"1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
.support.v7.widget.RecyclerView
android:id="@+id/rv_recyclerview_one"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="10dp">
.support.v7.widget.RecyclerView>
.support.v7.widget.RecyclerView
android:id="@+id/rv_recyclerview_two"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:padding="10dp"
>
.support.v7.widget.RecyclerView>
由于注释写的很清楚,直接贴代码给大家看了
/**
* 多列list
* @author petergee
* @date 2018/7/26
*/
public class MultiColumnListActivity extends Activity {
private List list;
private List list2;
private List list3;
private List list4;
private FirstLineAdapter firstLineAdapter,secondLineAdapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_multi_column);
initData();
initView();
}
private void initData() {
list = new ArrayList<>();
list.add("于谦");
list.add("郭德纲");
list.add("小岳岳");
list2 = new ArrayList<>();
list2.add("抽烟");
list2.add("喝酒");
list2.add("烫头");
list3 = new ArrayList<>();
list3.add("主持");
list3.add("说相声");
list3.add("拍电影");
list4 = new ArrayList<>();
list4.add("拍电影");
list4.add("唱歌");
list4.add("说相声");
list4.add("做采访");
}
private void initView() {
RecyclerView recyclerView=findViewById(R.id.rv_recyclerview_one);
final RecyclerView recyclerViewTwo=findViewById(R.id.rv_recyclerview_two);
// layoutManager
LinearLayoutManager layoutManager=new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
// set layoutManager
recyclerView.setLayoutManager(layoutManager);
recyclerViewTwo.setLayoutManager(new LinearLayoutManager(this));
// item Decoration
DividerItemDecoration dividerItemDecoration=new DividerItemDecoration(this,DividerItemDecoration.VERTICAL);
dividerItemDecoration.setDrawable(getResources().getDrawable(R.drawable.divider_line));
recyclerView.addItemDecoration(dividerItemDecoration);
recyclerViewTwo.addItemDecoration(dividerItemDecoration);
// item Animator
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerViewTwo.setItemAnimator(new DefaultItemAnimator());
// adapter
firstLineAdapter = new FirstLineAdapter(this,list);
secondLineAdapter = new FirstLineAdapter(this,list2);
recyclerView.setAdapter(firstLineAdapter);
// childAdapter
recyclerViewTwo.setAdapter(secondLineAdapter);
// add listener
firstLineAdapter.setRecyclerItemClickListener(new RecyclerItemClickListener() {
@Override
public void onItemClickListener(int position) {
if (position==0){
secondLineAdapter=new FirstLineAdapter(getApplicationContext(),list2);
recyclerViewTwo.setAdapter(secondLineAdapter);
}
if (position==1){
secondLineAdapter=new FirstLineAdapter(getApplicationContext(),list3);
recyclerViewTwo.setAdapter(secondLineAdapter);
}
if (position==2){
secondLineAdapter=new FirstLineAdapter(getApplicationContext(),list4);
recyclerViewTwo.setAdapter(secondLineAdapter);
}
}
@Override
public void onItemLongClickListener(int position) {
Toast.makeText(getApplicationContext(),"LongClicked "+list.get(position),Toast.LENGTH_SHORT).show();
}
});
}
}
由于需要使用ietm的点击事件,添加了接口,进行回调实现
/**
* @author petergee
* @date 2018/7/27
*/
public class FirstLineAdapter extends RecyclerView.Adapter<viewHolder> {
private Context mContext;
private List mList;
public RecyclerItemClickListener recyclerItemClickListener;
public FirstLineAdapter(Context context, List list) {
this.mContext = context;
this.mList = list;
}
@NonNull
@Override
public viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(mContext).inflate(R.layout.item_multi_column_list,parent,false);
return new viewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull viewHolder holder, final int position) {
String str=mList.get(position);
holder.tvTextFirstLine.setText(str);
//item listener
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (recyclerItemClickListener==null){
return;
}
recyclerItemClickListener.onItemClickListener(position);
}
});
// item LongClickListener
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (recyclerItemClickListener!=null)
recyclerItemClickListener.onItemLongClickListener(position);
return false;
}
});
}
@Override
public int getItemCount() {
return mList.size()==0?0:mList.size();
}
public void setRecyclerItemClickListener(RecyclerItemClickListener recyclerItemClickListener) {
this.recyclerItemClickListener = recyclerItemClickListener;
}
}
class viewHolder extends RecyclerView.ViewHolder {
TextView tvTextFirstLine;
public viewHolder(View itemView) {
super(itemView);
tvTextFirstLine=itemView.findViewById(R.id.item_text);
}
}
添加了条目点击和长按事件监听回调方法
public interface RecyclerItemClickListener{
void onItemClickListener(int position);
void onItemLongClickListener(int position);
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="@color/color_black"
android:layout_centerHorizontal="true"
android:layout_margin="5dp"
/>
RelativeLayout>