我需要在父列表和子列表中创建一个带有 3 个
ExpandableListView
的 TextView
。我有 parentlist
和 childlist
,每行有 3 个 TextView
。那我接下来该怎么办呢?
ArrayList<Contact> list = new ArrayList<>(); //main_equip
list.add(new Contact("Surdialx", "Status","Available Works"));
list.add(new Contact("Surdial", "Status","Available Works"));
list.add(new Contact("Surdialx", "Status","Available Works"));
list.add(new Contact("Surdial", "Status","Available Works"));
list.add(new Contact("Surdialx", "Status","Available Works"));
list.add(new Contact("Surdial", "Status","Available Works"));
ArrayList<Contact1> childlist = new ArrayList<>(); //eq_works
childlist.add(new Contact1("Work №1", "Status", "Priority"));
childlist.add(new Contact1("Work №2", "Status","Priority"));
childlist.add(new Contact1("Work №3", "Status","Priority"));
要创建可扩展的回收器视图尝试这种方式
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerview = (RecyclerView) findViewById(R.id.recyclerview);
recyclerview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
List<ExpandableListAdapter.Item> data = new ArrayList<>();
data.add(new ExpandableListAdapter.Item(ExpandableListAdapter.HEADER, "Fruits"));
data.add(new ExpandableListAdapter.Item(ExpandableListAdapter.CHILD, "Apple"));
data.add(new ExpandableListAdapter.Item(ExpandableListAdapter.CHILD, "Orange"));
data.add(new ExpandableListAdapter.Item(ExpandableListAdapter.CHILD, "Banana"));
data.add(new ExpandableListAdapter.Item(ExpandableListAdapter.HEADER, "Cars"));
data.add(new ExpandableListAdapter.Item(ExpandableListAdapter.CHILD, "Audi"));
data.add(new ExpandableListAdapter.Item(ExpandableListAdapter.CHILD, "Aston Martin"));
data.add(new ExpandableListAdapter.Item(ExpandableListAdapter.CHILD, "BMW"));
data.add(new ExpandableListAdapter.Item(ExpandableListAdapter.CHILD, "Cadillac"));
ExpandableListAdapter.Item places = new ExpandableListAdapter.Item(ExpandableListAdapter.HEADER, "Places");
places.invisibleChildren = new ArrayList<>();
places.invisibleChildren.add(new ExpandableListAdapter.Item(ExpandableListAdapter.CHILD, "Kerala"));
places.invisibleChildren.add(new ExpandableListAdapter.Item(ExpandableListAdapter.CHILD, "Tamil Nadu"));
places.invisibleChildren.add(new ExpandableListAdapter.Item(ExpandableListAdapter.CHILD, "Karnataka"));
places.invisibleChildren.add(new ExpandableListAdapter.Item(ExpandableListAdapter.CHILD, "Maharashtra"));
data.add(places);
recyclerview.setAdapter(new ExpandableListAdapter(data));
}
}
基本上,您将项目分为几个部分:
class MySection extends StatelessSection {
String header;
List<Contact> list;
boolean expanded = true;
public MySection(String header, List<Contact> list) {
// call constructor with layout resources for this Section header and items
super(R.layout.section_header, R.layout.section_item);
this.myHeader = header;
this.myList = list;
}
@Override
public int getContentItemsTotal() {
return expanded? list.size() : 0;
}
@Override
public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
return new HeaderViewHolder(view);
}
@Override
public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
final HeaderViewHolder headerHolder = (HeaderViewHolder) holder;
headerHolder.tvTitle.setText(title);
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();
}
});
}
@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).getName());
}
}
然后创建您的部分的实例并设置您的适配器:
// Create an instance of SectionedRecyclerViewAdapter
SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter();
// Add your Sections
sectionAdapter.addSection(new MySection("Surdial", surdialList);
sectionAdapter.addSection(new MySection("Surdialx", surdialXList);
sectionAdapter.addSection(new MySection("Work", workList);
// Set up your RecyclerView with the SectionedRecyclerViewAdapter
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(sectionAdapter);