扩展列表视图不扩大 [英] Expandable listview isn't expanding

查看:116
本文介绍了扩展列表视图不扩大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要这样prepare屏幕喜

Hi need to prepare screen like this

下面是我的$ C $扩展列表视图了C

Here is my code of expandable list view

适配器类: NewAdapter.java

public class NewAdapter extends BaseExpandableListAdapter {

    public ArrayList<ParentBean> groupItem;
    ArrayList<String> tempChild;
    public ArrayList<Object> Childtem = new ArrayList<Object>();
    public LayoutInflater minflater;
    public Activity activity;

    public NewAdapter(ArrayList<ParentBean> grList, ArrayList<Object> childItem) {
        groupItem = grList;
        this.Childtem = childItem;
    }

    public void setInflater(LayoutInflater mInflater, Activity act) {
        this.minflater = mInflater;
        activity = act;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        tempChild = (ArrayList<String>) Childtem.get(groupPosition);
        TextView text = null;
        if (convertView == null) {
            convertView = minflater.inflate(R.layout.childrow, null);
        }

        text = (TextView) convertView.findViewById(R.id.textView1);
        text.setText(tempChild.get(0));
        text = (TextView) convertView.findViewById(R.id.textView2);
        text.setText(tempChild.get(1));
        text = (TextView) convertView.findViewById(R.id.textView3);
        text.setText(tempChild.get(2));
        convertView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(activity, tempChild.get(childPosition),
                        Toast.LENGTH_SHORT).show();
            }
        });
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return null;
    }

    @Override
    public int getGroupCount() {
        return groupItem.size();
    }

    @Override
    public void onGroupCollapsed(int groupPosition) {
        super.onGroupCollapsed(groupPosition);
    }

    @Override
    public void onGroupExpanded(int groupPosition) {
        super.onGroupExpanded(groupPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if (convertView == null) {
            convertView = minflater.inflate(R.layout.grouprow, null);
            holder = new ViewHolder();
            holder.name=(TextView)convertView.findViewById(R.id.playername);
            holder.team = (TextView) convertView.findViewById(R.id.vs);
            holder.salary = (TextView) convertView.findViewById(R.id.salary);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        ParentBean playerdetails = groupItem.get(groupPosition);
        holder.name.setText(playerdetails.getPlayername());
        holder.team.setText(playerdetails.getPlayerteam());
        holder.salary.setText(playerdetails.getPlayersalary());
        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    private class ViewHolder {
        TextView team;
        TextView name;
        TextView salary;
    }
    class ParentBean{
        String playername,playerteam,playersalary;

        public ParentBean(String playername,String playerteam,String playersalary) {
            this.playername=playername;
            this.playersalary=playersalary;
            this.playerteam=playerteam;
        }

        /**
         * @return the playername
         */
        public String getPlayername() {
            return playername;
        }

        /**
         * @param playername the playername to set
         */
        public void setPlayername(String playername) {
            this.playername = playername;
        }

        /**
         * @return the playerteam
         */
        public String getPlayerteam() {
            return playerteam;
        }

        /**
         * @param playerteam the playerteam to set
         */
        public void setPlayerteam(String playerteam) {
            this.playerteam = playerteam;
        }

        /**
         * @return the playersalary
         */
        public String getPlayersalary() {
            return playersalary;
        }

        /**
         * @param playersalary the playersalary to set
         */
        public void setPlayersalary(String playersalary) {
            this.playersalary = playersalary;
        }

    }
}

下面是我的活动: MainActivity.java

public class MainActivity extends ExpandableListActivity implements
        OnChildClickListener {
    Context context;
    ArrayList<ParentBean> groupItem = new ArrayList<ParentBean>();
    ArrayList<Object> childItem = new ArrayList<Object>();
    NewAdapter mNewAdapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ExpandableListView expandbleLis = getExpandableListView();
        expandbleLis.setDividerHeight(2);
        expandbleLis.setGroupIndicator(null);
        expandbleLis.setClickable(true);
        expandbleLis.setFocusable(true);
        context=this;
        getData();
        mNewAdapter = new NewAdapter(groupItem, childItem);
        mNewAdapter.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE),this);
        getExpandableListView().setAdapter(mNewAdapter);

        expandbleLis.setOnChildClickListener(this);
    }

    public void getData() {
        JsonDataCallback callback=new JsonDataCallback(MainActivity.this) {

            @Override
            public void receiveData(Object object) {
                String jsonData=(String)object;
                setData(jsonData);
            }
        };
        callback.execute("http://111.93.7.119:8080/FirstPickService/Players?sportid=fc2e88e6-ac87-4d27-b6b3-863baa9f06ec",null,null);
    }

    protected void setData(String jsonData) {
        try {
            JSONObject players=new JSONObject(jsonData);
            final JSONArray playersArray=players.getJSONArray("players");
            for(int i=0;i<playersArray.length();i++)
            {
                JSONObject jsonObj=playersArray.getJSONObject(i);
                String teamplayer=jsonObj.getString("playerfullname");
                String psalary=jsonObj.getString("playerprice");
                String strTeamVS=jsonObj.getString("teamid")+"@"+jsonObj.getString("awayteam");
                groupItem.add(mNewAdapter.new ParentBean(teamplayer, psalary, strTeamVS));

                String strsalary = jsonObj.getString("playerpricelong");
                ArrayList<String> child = new ArrayList<String>();
                child.add(psalary);
                child.add(strTeamVS);
                child.add(strsalary);
                childItem.add(child);
            }
            mNewAdapter.notifyDataSetChanged();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }




    @Override
    public boolean onChildClick(ExpandableListView parent, View v,
            int groupPosition, int childPosition, long id) {
        Toast.makeText(MainActivity.this, "Clicked On Child",
                Toast.LENGTH_SHORT).show();
        return true;
    }
}

我不知道,但我已经使用了,因为扩展列表失去焦点的选择按钮,并没有显示childrow时,点击列表项......

I am not sure but I have used a button for select, because of that expandable list losing focus and is not showing childrow when click on the listitem...

推荐答案

尝试使选择按钮不可聚焦在XML。

Try making the select button non-focusable in the xml.

这篇关于扩展列表视图不扩大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆