在片段情况下单击项目时,没有任何侦听器在expandablelistview中工作,但在活动中使用时一切正常。没有生成错误。视图显示正确。请解决此问题。谢谢你
**Fragment1.java**
public class Fragement1 extends Fragment{
TextView text;
Toolbar toolbar;
android.widget.ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.layout_fragment_one,container,false);
final String branch=this.getArguments().getString("branch");
toolbar=(Toolbar)view.findViewById(R.id.nav_action);
//casting is neccessary beacase getActivity returns FragmentActivity and we need appcomatact...
//((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
//setHasOptionsMenu(true);
text=(TextView)view.findViewById(R.id.textView3);
text.setText(branch);
//((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// get the listview
expListView = (ExpandableListView) view.findViewById(R.id.lvExp);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(getActivity().getApplicationContext(),listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
// Listview Group click listener
expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
Toast.makeText(getActivity(),
"Group Clicked " + listDataHeader.get(groupPosition),
Toast.LENGTH_SHORT).show();
return false;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getActivity().getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getActivity(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Log.d("Clicked","clikkkked");
// TODO Auto-generated method stub
String subject =listDataHeader.get(groupPosition);
String chapter =listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition);
ChapterContentFetcherFragment chapterContentFetcherFragment = new ChapterContentFetcherFragment();
Bundle args=new Bundle();
args.putString("branch",branch);
args.putString("subject",subject);
args.putString("chapter",chapter);
chapterContentFetcherFragment.setArguments(args);
Toast.makeText(
getActivity().getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
return view;
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding header data
listDataHeader.add("Top 250");
listDataHeader.add("Now Showing");
listDataHeader.add("Coming Soon..");
// Adding child data
List<String> top250 = new ArrayList<String>();
top250.add("The Shawshank Redemption");
top250.add("The Godfather");
top250.add("The Godfather: Part II");
top250.add("Pulp Fiction");
top250.add("The Good, the Bad and the Ugly");
top250.add("The Dark Knight");
top250.add("12 Angry Men");
List<String> nowShowing = new ArrayList<String>();
nowShowing.add("The Conjuring");
nowShowing.add("Despicable Me 2");
nowShowing.add("Turbo");
nowShowing.add("Grown Ups 2");
nowShowing.add("Red 2");
nowShowing.add("The Wolverine");
List<String> comingSoon = new ArrayList<String>();
comingSoon.add("2 Guns");
comingSoon.add("The Smurfs 2");
comingSoon.add("The Spectacular Now");
comingSoon.add("The Canyons");
comingSoon.add("Europa Report");
listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
listDataChild.put(listDataHeader.get(1), nowShowing);
listDataChild.put(listDataHeader.get(2), comingSoon);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu,menu);
}
**DashboardActivity.java**
Im replacing it with fragment fragment1.
public class DashboardActivity extends AppCompatActivity {
private TextView branchView;
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
Bundle bundle= getIntent().getExtras();
final String branch= bundle.getString("branch");
branchView =(TextView) findViewById(R.id.branch);
switch (branch)
{
case "computer":
branchView.setText(branch);
Fragement1 comp_fragment=new Fragement1();
comp_fragment.setArguments(bundle);
getFragmentManager().beginTransaction().replace(R.id.dashboard,comp_fragment).addToBackStack(null).commit();
case "electronics":
branchView.setText(branch);
Electronics_Fragment extc_frag=new Electronics_Fragment();
extc_frag.setArguments(bundle);
getFragmentManager().beginTransaction().replace(R.id.dashboard,extc_frag).commit();
case "electrical":
branchView.setText(branch);
Electrical_Fragment electrical_fragment=new Electrical_Fragment();
electrical_fragment.setArguments(bundle);
getFragmentManager().beginTransaction().replace(R.id.dashboard,electrical_fragment).commit();
case "chemical":
branchView.setText(branch);
Chemical_Fragment chemical_fragment=new Chemical_Fragment();
chemical_fragment.setArguments(bundle);
getFragmentManager().beginTransaction().replace(R.id.dashboard,chemical_fragment).commit();
case "civil":
branchView.setText(branch);
Civil_Fragment civil_fragment=new Civil_Fragment();
civil_fragment.setArguments(bundle);
getFragmentManager().beginTransaction().replace(R.id.dashboard,civil_fragment).commit();
case "mechanical":
branchView.setText(branch);
Mechanical_Fragment mechanical_fragment=new Mechanical_Fragment();
mechanical_fragment.setArguments(bundle);
getFragmentManager().beginTransaction().replace(R.id.dashboard,mechanical_fragment).commit();
}
**layout_fragment_one.xml**
<?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"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:layout_marginTop="40dp"
android:id="@+id/textView3" />
<ExpandableListView
android:id="@+id/lvExp"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:cacheColorHint="#00000000"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:paddingTop="10dp"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</LinearLayout>
//适配器类
public class ExpandableListAdapter扩展了BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return convertView;
}
@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 int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@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.list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
答案 0 :(得分:0)
试试这个
ArrayAdapter<String>adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,android.R.id.text1,values);
//assign Adapter with listview
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int itemPosition = position;
String itemValue = (String)listView.getItemAtPosition(position);
Toast.makeText(getApplicationContext(),"Position :"+itemPosition+"\nList Item :"+itemValue,Toast.LENGTH_SHORT).show();
}
});
答案 1 :(得分:0)
你可以改变这个:
listAdapter = new ExpandableListAdapter(getActivity().getApplicationContext(),listDataHeader, listDataChild);
到此:
listAdapter = new ExpandableListAdapter(getActivity(),listDataHeader, listDataChild);