任何人都可以解决我的问题我想在点击可扩展列表项时打开新片段,并回到可扩展列表我想为每个子项创建片段。
主要活动类
df = structure(list(col1 = c("", "", "56", "", "67"), col2 = c("",
"", "78", "", "76"), col3 = c("", "", "89", "", "43")), .Names = c("col1",
"col2", "col3"), row.names = c(NA, -5L), class = "data.frame")
活动xml文件
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ExpandableFragment expandableFragment = new ExpandableFragment();
getSupportFragmentManager().beginTransaction().add(R.id.main_container,expandableFragment).commit();
}}
和适配器类
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.irfanattari.explistviewfragment.MainActivity">
<FrameLayout
android:id="@+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.constraint.ConstraintLayout>
和可扩展列表片段的代码
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> listDataHeader;
private HashMap<String, List<String>> listHashMap;
public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listHashMap) {
this.context = context;
this.listDataHeader = listDataHeader;
this.listHashMap = listHashMap;
}
@Override
public int getGroupCount() {
return listDataHeader.size();
}
@Override
public int getChildrenCount(int i) {
return listHashMap.get(listDataHeader.get(i)).size();
}
@Override
public Object getGroup(int i) {
return listDataHeader.get(i);
}
@Override
public Object getChild(int i, int i1) {
return listHashMap.get(listDataHeader.get(i)).get(i1); //i=group item i1=child item
}
@Override
public long getGroupId(int i) {
return i;
}
@Override
public long getChildId(int i, int i1) {
return i1;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
String headerTitle = (String) getGroup(i);
if (view == null){
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.list_group, null);
}
TextView lblListHeader = (TextView) view.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return view;
}
@Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
final String childText = (String)getChild(i,i1);
if (view == null){
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) view.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return view;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
}
和可扩展片段布局
public class ExpandableFragment extends Fragment{
private ExpandableListView listView;
private ExpandableListAdapter listAdapter;
private List<String> listDataHeader;
private HashMap<String,List<String>> listHash;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.expandable_fragment,container,false);
listView = (ExpandableListView) rootView.findViewById(R.id.lvExp);
initData();
listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listHash);
listView.setAdapter(listAdapter);
return rootView;
}
private void initData() {
listDataHeader = new ArrayList<>();
listHash = new HashMap<>();
listDataHeader.add("اراکین کابینہ");
listDataHeader.add("ڈویژن نگران و مشاورت");
listDataHeader.add("علاقائی نگران و مشاورت");
listDataHeader.add("حلقہ نگران و مشاورت");
listDataHeader.add("ذیلی نگران و مشاورت");
List<String> ak = new ArrayList<String>();
ak.add("اراکین کابینہ لسٹ");
List<String> dnwm = new ArrayList<String>();
dnwm.add("انوار مرشد");
dnwm.add("گلزار مرشد");
dnwm.add("قرار مرشد");
dnwm.add("دیار مرشد");
List<String> enwm = new ArrayList<String>();
enwm.add("انوار مرشد");
enwm.add("گلزار مرشد");
enwm.add("قرار مرشد");
enwm.add("دیار مرشد");
List<String> hnwm = new ArrayList<String>();
hnwm.add("انوار مرشد");
hnwm.add("گلزار مرشد");
hnwm.add("قرار مرشد");
hnwm.add("دیار مرشد");
List<String> znwm = new ArrayList<String>();
znwm.add("انوار مرشد");
znwm.add("گلزار مرشد");
znwm.add("قرار مرشد");
znwm.add("دیار مرشد");
listHash.put(listDataHeader.get(0),ak);
listHash.put(listDataHeader.get(1),dnwm);
listHash.put(listDataHeader.get(2),enwm);
listHash.put(listDataHeader.get(3),hnwm);
listHash.put(listDataHeader.get(4),znwm);
}}
单击子项时打开的和ak片段
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ExpandableListView
android:id="@+id/lvExp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutDirection="rtl"
android:divider="#f00"
android:dividerHeight="1dp"
android:listSelector="#0f0"
android:childDivider="#0f0"/>
</LinearLayout>
及其布局包含文本视图显示
答案 0 :(得分:0)
您可以使用
setOnChildClickListener(ExpandableListView.OnChildClickListener onChildClickListener)
setOnGroupClickListener(ExpandableListView.OnGroupClickListener onGroupClickListener)
来自ExpandableListView