早上好!
有必要创建一个列表树,大致如图所示:
https://joshsmithonwpf.wordpress.com/2008/08/01/article-about-checkboxes-in-a-wpf-treeview/
请原谅,网站本身不允许我插入图片
树由复选框组成,单击父元素时,将显示/删除子元素中的复选框。除了复选框之外,树的每个元素都有自己的参数(字节数组)。
实际上,有必要在按下复选框(例如,单击一个按钮)并获取值(相同的字节数组)、名称、选项卡名称并将其写入另一个数组/列表等后读取复选框的状态。
主要的问题是如何创建一个树视图,其中子元素的状态取决于父元素,以及如何分配它们,以及值。
我看到了很多例子,但我没有找到。只是遇到断开的链接。
我只能自己实现复选框列表,但子元素不会对更改父元素的状态作出反应。
代码如下所示。
ExpandableListAdapter。Java语言
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> listDataHeader;
private HashMap<String, List<String>> listDataChild;
public ExpandableListAdapter(Context context, List<String> listHeaderData, HashMap<String, List<String>> listChildData){
this.context = context;
this.listDataHeader = listHeaderData;
this.listDataChild = listChildData;
}
@Override
public int getGroupCount() {
return this.listDataHeader.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return this.listDataChild.get(this.listDataHeader.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return this.listDataHeader.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return this.listDataChild.get(this.listDataHeader.get(groupPosition)).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.group_row, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.child_row, null);
}
TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
child\u行。xml
<?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="55dip"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:orientation="horizontal">
<TextView
android:id="@+id/lblListItem"
android:layout_width="308dp"
android:layout_height="match_parent"
android:paddingBottom="5dp"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
android:paddingTop="5dp"
android:text="asd"
android:textSize="20dip" />
<CheckBox
android:id="@+id/lblListChildCheckbox"
android:layout_width="72dp"
android:layout_height="match_parent"
android:focusable="false"
android:textSize="17dp" />
</LinearLayout>
group\u行。xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:orientation="horizontal"
android:padding="8dp">
<TextView
android:id="@+id/lblListHeader"
android:layout_width="306dp"
android:layout_height="match_parent"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:textSize="20dp" />
<CheckBox
android:id="@+id/lblListHeaderCheckbox"
android:layout_width="50dp"
android:layout_height="match_parent"
android:focusable="false"
android:text=""
android:textSize="17dp" />
</LinearLayout>
列表xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andoid="http://schemas.android.com/apk/res/android"
andoid:layout_width="match_parent"
andoid:layout_height="match_parent"
andoid:orientation="vertical">
<Button
andoid:id="@+id/add_template"
style="@style/Widget.AppCompat.Button.Colored"
andoid:layout_width="match_parent"
andoid:layout_height="wrap_content"
andoid:layout_gravity="center"
andoid:layout_marginTop="10dp"
andoid:text="ÐобавиÑÑ Ñаблон" />
<ExpandableListView
andoid:id="@+id/listing"
andoid:layout_width="match_parent"
andoid:layout_height="wrap_content"></ExpandableListView>
</LinearLayout>
Choise\u列表。Java语言
public class Choise_List extends Activity {
private Button add_template;
class Lists{
String page;
String name;
String type;
byte[] val;
}
Context context;
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
HashMap<String, List<String>> listDataChild;
List<String> pages;
ArrayList< Map<String, String> > childDataItem = new ArrayList<>();
List<Lists> structure_list = new ArrayList<>();
public void createListsList(ArrayList<byte[]> ch_check) throws UnsupportedEncodingException {
for (int i = 0; i < ch_check.size(); i++){
if(Arrays.equals( ch_check.get(i), "PAGE".getBytes( Charset.forName("UTF-8") ) ) ){
Lists lists = new Lists();
int state = i;
lists.page = new String( ch_check.get(++state), "UTF-8" );
lists.name = new String( ch_check.get(++state), "UTF-8" );
lists.type = new String( ch_check.get(++state), "UTF-8" );
lists.val = ch_check.get(++state);
i = state;
structure_list.add(lists);
}
}
}
private void prepareListData(){
HashSet<String> used = new HashSet<>();
int count = 0;
pages = new ArrayList<>();
listDataChild = new HashMap<String, List<String>>();
for(int i = 0; i < structure_list.size(); i++){
if(used.contains(structure_list.get(i).page)){
continue;
} else {
used.add(structure_list.get(i).page);
}
String new_page_name = "";
ArrayList<Integer> positions = new ArrayList<>();
positions.add(i);
for(int j = i + 1; j < structure_list.size(); j++){
if(structure_list.get(i).page.equals(structure_list.get(j).page)){
positions.add(j);
}
}
childDataItem = new ArrayList<Map<String, String>>();
List<String> child = new ArrayList<String>();
for(Integer p : positions){
new_page_name = structure_list.get(p).page;
child.add(structure_list.get(p).name);
}
pages.add(new_page_name);
listDataChild.put(pages.get(count), child);
count++;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listings);
expListView = (ExpandableListView) findViewById(R.id.listing);
ArrayList<byte[]> ch_check = new ArrayList<>();
if(savedInstanceState == null){
MyObject myObj = (MyObject) getIntent().getParcelableExtra(MyObject.class.getCanonicalName());
if(myObj == null){
ch_check = null;
} else{
ch_check = myObj.choise;
}
}
if(ch_check != null){
try {
createListsList(ch_check);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
prepareListData();
expListView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
listAdapter = new ExpandableListAdapter(this, pages, listDataChild);
expListView.setAdapter(listAdapter);
expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
return false;
}
});
expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(), pages.get(groupPosition) + " Expanded", Toast.LENGTH_SHORT).show();
}
});
expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),pages.get(groupPosition) + " Collapsed", Toast.LENGTH_SHORT).show();
}
});
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText( getApplicationContext(),pages.get(groupPosition) + " : " + listDataChild.get(pages.get(groupPosition)).get( childPosition), Toast.LENGTH_SHORT).show();
CheckBox checkBox = (CheckBox) findViewById(R.id.lblListChildCheckbox);
checkBox.setChecked(true);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checkBox.setChecked(isChecked);
}
});
return false;
}
});
}
}