gpt4 book ai didi

android - 如何在 Android 上实现 Material Design 展开/折叠列表?

转载 作者:IT老高 更新时间:2023-10-28 23:07:05 27 4
gpt4 key购买 nike

我正在寻找实现这种风格的 Material list 。我怎样才能在安卓上做到这一点?我应该看什么课?是否有任何现有的库可以使实现这一点变得容易?

Material Design Expand/Collapse List

最佳答案

是的,您可以使用库 SectionedRecyclerViewAdapter 轻松实现它.有一个完整的工作示例 here .

基本上你创建一个section类:

class MySection extends StatelessSection {

String title;
List<String> list;
boolean expanded = true; // true if you want it to be displayed expanded initially

public MySection(String title, List<String> list) {
// call constructor with layout resources for this Section header, footer and items
super(R.layout.section_header, R.layout.section_item);

this.title = title;
this.list = list;
}

@Override
public int getContentItemsTotal() {
return expanded? list.size() : 0;
}

@Override
public RecyclerView.ViewHolder getItemViewHolder(View view) {
// return a custom instance of ViewHolder for the items of this section
return new MyItemViewHolder(view);
}

@Override
public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
MyItemViewHolder itemHolder = (MyItemViewHolder) holder;

// bind your view here
itemHolder.tvItem.setText(list.get(position));
}

@Override
public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
return new SimpleHeaderViewHolder(view);
}

@Override
public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
MyHeaderViewHolder headerHolder = (MyHeaderViewHolder) holder;

// bind your header view here
headerHolder.tvItem.setText(title);

// handles the click on the header to toggle the expanded variable
headerHolder.rootView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
expanded = !expanded;
headerHolder.imgArrow.setImageResource(
expanded ? R.drawable.ic_keyboard_arrow_up_black_18dp : R.drawable.ic_keyboard_arrow_down_black_18dp
);
sectionAdapter.notifyDataSetChanged();
}
});
}
}

然后你用你的部分设置 RecyclerView:

// Create an instance of SectionedRecyclerViewAdapter 
SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter();

// Create your sections with the list of data for each topic
MySection topic1Section = new MySection("Attractions", attractionsList);
MySection topic2Section = new MySection("Dining", diningList);

// Add your Sections to the adapter
sectionAdapter.addSection(topic1Section);
sectionAdapter.addSection(topic2Section);

// Set up your RecyclerView with the SectionedRecyclerViewAdapter
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(sectionAdapter);

关于android - 如何在 Android 上实现 Material Design 展开/折叠列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28389052/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com