我试着在框架中实现可扩展列表视图,我已经测试了所有设置的值,它运行fine.But我的可扩展列表视图,而不是Dispaly.I,我没有在我使用的代码下面找到任何Error.please。
package com.test.expandablelistView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.example.tesfragement.R;
import com.example.tesfragement.R.layout;
import android.os.Bundle;
import android.app.Fragment;
import android.database.DataSetObserver;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.Toast;
/**
* A simple {@link android.support.v4.app.Fragment} subclass.
*
*/
public class ExpandableListFragment extends Fragment {
View v;
ExpandableListAdapter mAdapter;
List<String> _listDataHeader;
HashMap<String, List<String>> _listDataChild;
private Parent parent;
private Child child;
ExpandableListView lv;
public ExpandableListFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
v= inflater.inflate(R.layout.expandable_fragements,
container, false);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
parent=new Parent();
child=new Child();
ExpandableListView lv = (ExpandableListView) v.findViewById(R.id.expandableListView1);
//here setting all the values to Parent and child classes
setDataValues();
prepareListData();//here get the values and set this values to adoptor and set it visible
mAdapter=new ExpandableListAdapter() {
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(),"hello", Toast.LENGTH_LONG).show();
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(),"hello1", Toast.LENGTH_LONG).show();
}
@Override
public void onGroupExpanded(int groupPosition) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(),"hello2", Toast.LENGTH_LONG).show();
}
@Override
public void onGroupCollapsed(int groupPosition) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(),"hello3", Toast.LENGTH_LONG).show();
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
Toast.makeText(getActivity(),"hello4", Toast.LENGTH_LONG).show();
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(),"hello5", Toast.LENGTH_LONG).show();
return false;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
Toast.makeText(getActivity(),"hello6", Toast.LENGTH_LONG).show();
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
Toast.makeText(getActivity(),"hello7", Toast.LENGTH_LONG).show();
// TODO Auto-generated method stub
return v;
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(),"hello8", Toast.LENGTH_LONG).show();
return 0;
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
Toast.makeText(getActivity(),"hello9", Toast.LENGTH_LONG).show();
return 0;
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(),"hello10", Toast.LENGTH_LONG).show();
return null;
}
@Override
public long getCombinedGroupId(long groupId) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(),"hello11", Toast.LENGTH_LONG).show();
return 0;
}
@Override
public long getCombinedChildId(long groupId, long childId) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(),"hello12", Toast.LENGTH_LONG).show();
return 0;
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(),"hello13", Toast.LENGTH_LONG).show();
return 0;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(),"hello14", Toast.LENGTH_LONG).show();
return v;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(),"hello15", Toast.LENGTH_LONG).show();
return 0;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(),"hello16", Toast.LENGTH_LONG).show();
return null;
}
@Override
public boolean areAllItemsEnabled() {
// TODO Auto-generated method stub
Toast.makeText(getActivity(),"hello17", Toast.LENGTH_LONG).show();
return false;
}
};
// mAdapter = new ExpandableListAdapter(this, _listDataHeader, _listDataChild);
// setting list adapter
lv.setAdapter(mAdapter);
}
public void prepareListData()
{
// testing purpose
_listDataHeader = new ArrayList<String>();
_listDataChild = new HashMap<String, List<String>>();
// declare the references
//add the parent values to List
_listDataHeader.add(parent.getCardName());
_listDataHeader.add(String.valueOf(parent.getMinimum_salary()));
_listDataHeader.add(String.valueOf(parent.getInterest_rate()));
//set Child views to parent
List<String> cardDetails=new ArrayList<String>();
cardDetails.add("");
List<String> mininum_sal_details=new ArrayList<String>();
mininum_sal_details.add(child.GetMinimumSalDetails());
List<String> interest_details=new ArrayList<String>();
interest_details.add(child.get_interest_rate_details());
//set to adoptor
_listDataChild.put(_listDataHeader.get(0), cardDetails);
_listDataChild.put(_listDataHeader.get(1),mininum_sal_details);
//
for(int i = 0; i < _listDataHeader.size(); i++) //cars name of arraylist
{
String value=_listDataHeader.get(i);
Toast toast = Toast.makeText(getActivity(),value, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
}
public void setDataValues()
{
//set Parent values
parent.setCardName("Platinum credit Card");
parent.setMinimum_salary(15000.00);
parent.setInterest_Rate(1.2);
//set Child values
child.set_card_details("You require minimum salary of 1500 per month");
child.set_interest_rate_details("interest rate is 2.0%");
}
}发布于 2014-02-28 06:52:19
package com.test.expandablelistView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.example.tesfragement.R;
import com.example.tesfragement.R.layout;
import android.os.Bundle;
import android.app.Fragment;
import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Typeface;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
import android.widget.Toast;
/**
* A simple {@link android.support.v4.app.Fragment} subclass.
*
*/
public class ExpandableListFragment extends Fragment {
View v;
ExpandableListAdapter mAdapter;
List<String> _listDataHeader;
HashMap<String, List<String>> _listDataChild;
private Parent parent;
private Child child;
ExpandableListView lv;
Context con;
public ExpandableListFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
v= inflater.inflate(R.layout.expandable_fragements,
container, false);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
parent=new Parent();
child=new Child();
ExpandableListView lv = (ExpandableListView) v.findViewById(R.id.expandableListView1);
//here setting all the values to Parent and child classes
setDataValues();
prepareListData();//here get the values and set this values to adoptor and set it visible
con=getActivity();
mAdapter=new ExpandabelListAdoptor(con,_listDataHeader, _listDataChild) ; //here i didnt set list values to this adoptor
// mAdapter = new ExpandableListAdapter(this, _listDataHeader, _listDataChild);
// setting list adapter
lv.setAdapter(mAdapter);
}
public void prepareListData()
{
// testing purpose
_listDataHeader = new ArrayList<String>();
_listDataChild = new HashMap<String, List<String>>();
// declare the references
//add the parent values to List
_listDataHeader.add(parent.getCardName());
_listDataHeader.add(String.valueOf(parent.getMinimum_salary()));
_listDataHeader.add(String.valueOf(parent.getInterest_rate()));
//set Child views to parent
List<String> cardDetails=new ArrayList<String>();
cardDetails.add("");
List<String> mininum_sal_details=new ArrayList<String>();
mininum_sal_details.add(child.GetMinimumSalDetails());
List<String> interest_details=new ArrayList<String>();
interest_details.add(child.get_interest_rate_details());
//set to adoptor
_listDataChild.put(_listDataHeader.get(0), cardDetails);
_listDataChild.put(_listDataHeader.get(1),mininum_sal_details);
//
for(int i = 0; i < _listDataHeader.size(); i++) //cars name of arraylist
{
String value=_listDataHeader.get(i);
Toast toast = Toast.makeText(getActivity(),value, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
}
public void setDataValues()
{
//set Parent values
parent.setCardName("Platinum credit Card");
parent.setMinimum_salary(15000.00);
parent.setInterest_Rate(1.2);
//set Child values
child.set_card_details("You require minimum salary of 1500 per month");
child.set_interest_rate_details("interest rate is 2.0%");
}
}
class ExpandabelListAdoptor extends BaseExpandableListAdapter
{
private Context _context;
private List<String> _listDataHeader;
private HashMap<String, List<String>> _listDataChild;
ExpandabelListAdoptor(Context con,List<String> listDataHeader ,HashMap<String, List<String>> listDataChild )
{
this._context=con;
this._listDataChild=listDataChild;
this._listDataHeader=listDataHeader;
}
@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) {
// TODO Auto-generated method stub
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) {
// TODO Auto-generated method stub
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
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() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
}发布于 2014-02-27 12:34:38
注意:在我的例子中,我看不到片段中可扩展列表视图的文本.除了所有功能之外,其他功能都可以正常工作,比如单击和展开。
如果情况相同,则需要为文件中的每个文本视图赋予文本颜色。请马上试一试。因为。我使用了可扩展的列表视图和片段,它的工作。
我刚刚更新了每个文本视图的文本颜色。
这是密码。
class ExpandableAdapters extends BaseExpandableListAdapter
{
LayoutInflater mInflater;
DisplayImageOptions options;
ArrayList<Taxons> localArr;
AnimateFirstDisplayListener animateFirstListener;
public ExpandableAdapters(ArrayList<Taxons> arrTaxon)
{
localArr = arrTaxon;
mInflater = LayoutInflater.from(view.getContext());
options = new DisplayImageOptions.Builder().bitmapConfig(Bitmap.Config.RGB_565).imageScaleType(ImageScaleType.EXACTLY).cacheInMemory(true)
.cacheOnDisc(true).resetViewBeforeLoading(true).build();
}
@Override
public Object getChild(int groupPosition, int childPosition)
{
if (localArr.get(groupPosition - 1).arr_Taxons != null && groupPosition > 0)
return localArr.get(groupPosition - 1).arr_Taxons.get(childPosition);
else
return null;
}
@Override
public long getChildId(int groupPosition, int childPosition)
{
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
{
if (convertView == null)
{
LayoutInflater infalInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.lyt_subcategory, null);
}
TextView tvSubcatname = (TextView) convertView.findViewById(R.id.subcat_tvName);
tvSubcatname.setText(localArr.get(groupPosition - 1).arr_Taxons.get(childPosition).name);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition)
{
if (localArr.get(groupPosition - 1).arr_Taxons != null && groupPosition > 0)
{
return localArr.get(groupPosition - 1).arr_Taxons.size();
}
else
return 0;
}
@Override
public Object getGroup(int groupPosition)
{
return localArr.get(groupPosition - 1);
}
@Override
public int getGroupCount()
{
return localArr.size() + 1;
}
@Override
public long getGroupId(int groupPosition)
{
return groupPosition;
}
@Override
public int getGroupTypeCount()
{
return 2;
}
@Override
public int getGroupType(int groupPosition)
{
return (groupPosition == 0) ? 1 : 0;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
{
final ViewHolder holder;
int theType = getGroupType(groupPosition);
if (convertView == null)
{
holder = new ViewHolder();
if (theType == 0)
{
// inflate the search row
convertView = mInflater.inflate(R.layout.lyt_category, null);
holder.ivCategory = (ImageView) convertView.findViewById(R.id.home_ivCategory);
holder.ivCategory.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, screenHeight / 2));
holder.ivCategory.setScaleType(ScaleType.CENTER_CROP);
holder.tvCategory = (TextView) convertView.findViewById(R.id.home_tvCategoryName);
holder.progress = (ProgressBar) convertView.findViewById(R.id.home_progressbar1);
holder.ivCategory.setTag(holder.progress);
holder.tvCategory.setPadding((int) (screenWidth * 0.07), 0, 0, 0);
}
else if (theType == 1)
{
// except zero index it will execute this code.
convertView = mInflater.inflate(R.layout.lyt_search_home, null);
holder.rlSearchmain = (RelativeLayout) convertView.findViewById(R.id.home_rlsearch);
holder.rlSearchmain.setLayoutParams(new AbsListView.LayoutParams(screenWidth, (int) (screenHeight * 0.07)));
holder.ivSearch = (ImageButton) convertView.findViewById(R.id.home_Btn_search);
holder.etSearch = (EditText) convertView.findViewById(R.id.home_edt_search);
}
convertView.setTag(holder);
}
else
holder = (ViewHolder) convertView.getTag();
//set value here.
return convertView;
}
@Override
public boolean hasStableIds()
{
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return true;
}
private class ViewHolder
{
ImageView ivCategory;
TextView tvCategory;
ProgressBar progress;
EditText etSearch;
ImageButton ivSearch;
RelativeLayout rlSearchmain;
}
}请忽略额外的代码..。
发布于 2014-02-27 12:39:00
因为您没有返回实际的子类或组计数,所以也不应该在getGroup或getChild方法上返回null。试着遵循这个示例
https://stackoverflow.com/questions/22067471
复制相似问题