Java 可展开的子listview项目单击图像

Java 可展开的子listview项目单击图像,java,android,xml,listview,expandablelistview,Java,Android,Xml,Listview,Expandablelistview,在上图中,我想在子listview项中按下加号按钮时将textview(红色)值增加1,按下减号时将textview(红色)值减少1。我不想单击整个列表项,只想单击正负图像视图。现在发生的事情是,我必须双击列表项来增加或减少textView(红色)的值。另外,当我滚动listview时,listitems上的值会自动消失。如何解决这两个问题。请帮忙 listitem.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout

在上图中,我想在子listview项中按下加号按钮时将textview(红色)值增加1,按下减号时将textview(红色)值减少1。我不想单击整个列表项,只想单击正负图像视图。现在发生的事情是,我必须双击列表项来增加或减少textView(红色)的值。另外,当我滚动listview时,listitems上的值会自动消失。如何解决这两个问题。请帮忙

listitem.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="wrap_content"
    android:padding="10dp"
    android:orientation="vertical" >



<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:layout_marginBottom="5dp"
         android:text="tv_name"
        android:textColor="#000000"
         />
     <TextView
        android:id="@+id/tv_price"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="15sp"
                        android:layout_marginBottom="5dp"
        android:gravity="right"
               android:text="tv_price"
        android:textColor="#000000"
         />

</LinearLayout>

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:orientation="horizontal" >

<TextView
        android:id="@+id/tv_desp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="12sp"
                         android:layout_weight="1"

                     android:text="tv_desp"
        android:textColor="@android:color/darker_gray"
         />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="right"
    android:orientation="horizontal" >

<ImageView
    android:id="@+id/iv_minus"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
    android:padding="5dp"
    android:src="@drawable/minus" />
<TextView
        android:id="@+id/tv_no_items"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
                     android:text="0"
                     android:textColor="@android:color/holo_red_dark"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
         />
<ImageView
    android:id="@+id/iv_plus"
    android:focusable="false"
        android:padding="5dp"
    android:descendantFocusability="blocksDescendants"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/plus" />

</LinearLayout>
</LinearLayout>

</LinearLayout>

fragment_subheaders.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="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/lvItems"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:dividerHeight="2dp"
        android:focusable="true" />

    <TextView
        android:id="@+id/tv_items_wo_sub_headers"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="tv_sub_headers"
        android:textSize="15sp" />

</LinearLayout>

SubheadersFragment.java

   public class SubheadersFragment extends Fragment {
        private int mResourceId;
         TextView tv_sub_headers;
         String sub_headers_string;
         String  sub_header,item;
         ExpandableListAdapter listAdapter;
        ExpandableListView expListView;
        List<String> listDataHeader;
        TextView tv_no_items,childPosition;
        HashMap<String, List<String>> listDataChild;
        String[] items;
        int no_of_items;

         public static SubheadersFragment newInstance(int resourceId,String tv_sub_headers ) {
             SubheadersFragment widgetFragment = new SubheadersFragment();
                widgetFragment.mResourceId = resourceId;
                widgetFragment.sub_headers_string=tv_sub_headers;

                return widgetFragment;
            }

         @Override
            public View onCreateView(LayoutInflater inflater,
                                     ViewGroup container,
                                     Bundle savedInstanceState) {
                return inflater.inflate(mResourceId, container, false);
            }
         @Override
            public void onViewCreated(View view, Bundle savedInstanceState) {   
             tv_sub_headers= (TextView) view.findViewById(R.id.tv_sub_headers);
                expListView = (ExpandableListView) view.findViewById(R.id.lvExpSH);

             tv_sub_headers.setText(sub_headers_string);
             try {
                JSONArray jArraySub_headers=new JSONArray(sub_headers_string);
                listDataHeader = new ArrayList<String>();
                listDataChild = new HashMap<String, List<String>>();

                   items=new String[jArraySub_headers.length()];

                for (int i = 0; i <jArraySub_headers.length(); i++) {//headers

                    JSONObject c = jArraySub_headers.getJSONObject(i);

                     sub_header = c.getString("sub_header"); 
                     item = c.getString("items"); 
                     items[i]=item;

                     listDataHeader.add(c.getString("sub_header"));

                }

                ///for (int j = 0; j < items.length; j++) {

                    JSONArray jArrayItemsVeg=new JSONArray(items[0]);
                    List<String> vegListString = new ArrayList<String>();

                    for (int j = 0; j <jArrayItemsVeg.length(); j++) {//headers
                        JSONObject c = jArrayItemsVeg.getJSONObject(j);

                     //   vegListString.add(c.getString("name")+"*"+c.getString("price")+"*"+c.getString("description"));
    //                 

                        if(c.has("item_types"))
                         vegListString.add(c.getString("name")+"*"+c.getString("price")+"*"+c.getString("description")+"*"+c.getString("item_types"));


                        else 
                          vegListString.add(c.getString("name")+"*"+c.getString("price")+"*"+c.getString("description"));

    //                   
    //      
                    }

                    JSONArray jArrayItemsNONVeg=new JSONArray(items[1]);
                    List<String> NonvegListString = new ArrayList<String>();

                    for (int j = 0; j <jArrayItemsNONVeg.length(); j++) {//headers
                        JSONObject c = jArrayItemsNONVeg.getJSONObject(j);
                      //  NonvegListString.add(c.getString("name"));
                 //       if(c.has("description"))
                        if(c.has("item_types"))
                            NonvegListString.add(c.getString("name")+"*"+c.getString("price")+"*"+c.getString("description")+"*"+c.getString("item_types"));

                        else 
                                                NonvegListString.add(c.getString("name")+"*"+c.getString("price")+"*"+c.getString("description"));

        //      
                        //                      else 
    //                          NonvegListString.add(c.getString("name")+"*"+c.getString("price"));

                    }
                    listDataChild.put(listDataHeader.get(0), vegListString); // Header, Child data
                    listDataChild.put(listDataHeader.get(1), NonvegListString);
            //  }
                listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild);

                expListView.setAdapter(listAdapter);
                expListView.setOnChildClickListener(new OnChildClickListener() {

                    @Override
                    public boolean onChildClick(ExpandableListView parent, View v,
                            int groupPosition, int childPosition, long id) {
                        // TODO Auto-generated method stub
                        ImageView iv_plus= (ImageView)v.findViewById(R.id.iv_plus);
                        ImageView iv_minus= (ImageView)v.findViewById(R.id.iv_minus);
                         tv_no_items= (TextView)v.findViewById(R.id.tv_no_items);

                        iv_plus.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                // TODO Auto-generated method stub
                                no_of_items=Integer.parseInt(tv_no_items.getText().toString())+1;
                                tv_no_items.setText(no_of_items+"");

                            }
                        }); 

                        iv_minus.setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                // TODO Auto-generated method stub
                                if(no_of_items>0){

                                no_of_items=Integer.parseInt(tv_no_items.getText().toString())-1;
                                tv_no_items.setText(no_of_items+"");
                                }
                            }
                        });
                        String header=listDataHeader.get(groupPosition);
                        String items=listDataChild.get(
                                listDataHeader.get(groupPosition)).get(
                                childPosition);
                        String[] itemsArray=items.split(Pattern.quote("*"));
                        Toast.makeText(
                                getActivity()
                                ,
                                itemsArray[itemsArray.length-1]
                                , Toast.LENGTH_SHORT)
                                .show();
                        return false;
                    }
                });

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }
         private void prepareListData() {
                listDataHeader = new ArrayList<String>();

                // Adding child 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");


            }
    }
公共类子标题片段扩展片段{
私有资源;
TextView tv_sub_标题;
字符串子标题\u字符串;
字符串子标题,项目;
可扩展列表适配器;
ExpandableListView解释视图;
列表列表数据头;
TextView tv_no_项目,儿童位置;
HashMapListDataChild;
字符串[]项;
int no_of_项目;
公共静态子标题Fragment newInstance(int resourceId、字符串tv\u子标题){
SubheadersFragment widgetFragment=新的SubheadersFragment();
widgetFragment.mResourceId=resourceId;
widgetFragment.sub_headers_string=tv_sub_headers;
返回widgetFragment;
}
@凌驾
创建视图上的公共视图(更平坦的充气机,
视图组容器,
Bundle savedInstanceState){
返回充气机。充气(mResourceId,容器,假);
}
@凌驾
已创建视图上的公共无效(视图,捆绑包savedInstanceState){
tv_sub_headers=(TextView)view.findviewbyd(R.id.tv_sub_headers);
expListView=(ExpandableListView)view.findViewById(R.id.lvExpSH);
tv_sub_headers.setText(sub_headers_string);
试一试{
JSONArray jArraySub_headers=新的JSONArray(sub_headers_字符串);
listDataHeader=新的ArrayList();
listDataChild=newHashMap();
items=新字符串[jArraySub_headers.length()];
对于(int i=0;i 4){
tv_desp.setText(“”);
}
其他的
tv_desp.setText(itemsArray[2]);
}
其他的
tv_desp.setText(“”);
//ImageView iv_plus=(ImageView)convertView.findViewById(R.id.iv_plus);
//ImageView iv_减号=(ImageView)convertView.findViewById(R.id.iv_减号);
//tv_no_items=(TextView)convertView.findViewById(R.id.tv_no_items);
//iv_plus.setOnClickListener(新视图.OnClickListener(){
//@覆盖
//公共void onClick(视图v){
////TODO自动生成的方法存根
//no_of_items=Integer.parseInt(tv_no_items.getText().toString())+1;
//tv_no_items.setText(no_of_items+);
//              
//          }
//      }); 
//      
//iv_减去.setOnClickListener(新视图.OnClickListener(){
//          
//@覆盖
//公共void onClick(视图v){
////TODO自动生成的方法存根
//如果(没有项目>0的项目){
//
//no_of_items=Integer.parseInt(tv_no_items.getText().toString())-1;
//tv_no_items.setText(no_of_items+);
//              }
//          }
//      });
返回视图;
}
@凌驾
公共整数getChildrenCount(整数组位置){
返回此。_listDataChild.get(此。_listDataHeader.get(groupPosition))
.size();
}
@凌驾
公共对象getGroup(int-groupPosition){
返回此。\u listDataHeader.get(groupPosition);
}
@凌驾
public int getGroupCount(){
返回此值。_listDataHeader.size();
}
@凌驾
公共长getGroupId(int-groupPosition){
返回组位置;
}
@凌驾
公共视图getGroupView(int-groupPosition,布尔值isExpanded,
视图(视图、视图组父级){
字符串头文件=(字符串)getGroup(groupPosition);
if(convertView==null){
LayoutInflater infalInflater=(LayoutInflater)this.\u上下文
.getSystemService(上下文布局\充气机\服务);
convertView=infalInflater.充气(R.layout.list_组,空);
}
TextView lblistheader=(TextView)convertView
.findviewbyd(R.id.lblistheader);
lblListHeader.setTypeface(null,Typeface.BOLD);
lblListHeader.setText(标题);
返回视图;
}
@凌驾
公共布尔表ID(){
返回false;
}
@凌驾
公共布尔值isChildSelectable(int-groupPosition,int-childPosition){
返回true;
}
}

使用适配器中注释掉的代码时发生了什么?这就是我应该做的。SubheadersFragment.java expListView.setOnChildClickListener(新的OnChildClickListener()是的,我在适配器中尝试了该代码,但首先单击了整个项目。只有在启用加减按钮后,您才可以使用ViewHolder模式查看,这可能有助于更好地处理单击。@AndroidSeverythingForme这是listview的回收问题您必须使用ViewHolder才能获得更好的性能和解决您的问题你必须保持视图的状态,因为它在滚动后可能会改变,所以你可以搜索保持列表视图状态以进行滚动,这样你就可以更好地了解它
public class ExpandableListAdapter extends BaseExpandableListAdapter {
    int no_of_items;
    private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<String>> _listDataChild;
    TextView     tv_no_items;
    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);
         String[] itemsArray=childText.split(Pattern.quote("*"));
    //  if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);
        //}


        TextView tv_name = (TextView) convertView
                .findViewById(R.id.tv_name);
        tv_name.setText(itemsArray[0]);

        TextView tv_price = (TextView) convertView
                .findViewById(R.id.tv_price);
        tv_price.setText("\u20B9 "+itemsArray[1]);

        TextView tv_desp = (TextView) convertView
                .findViewById(R.id.tv_desp);
        String fontPath = "fonts/RobotoCondensed-Regular.ttf";
        Typeface tf = Typeface.createFromAsset(_context.getAssets(), fontPath);  
        tv_name.setTypeface(tf);
        tv_price.setTypeface(tf);
        tv_desp.setTypeface(tf);

        //Toast.makeText(_context, Arrays.toString(itemsArray), Toast.LENGTH_LONG).show();
        if(itemsArray.length>=3){
            ;
            if(itemsArray[2].contains("{") && itemsArray[2].indexOf("{")>4 ){
                tv_desp.setText("");


            }

            else
            tv_desp.setText(itemsArray[2]);


        }
        else
            tv_desp.setText("");

//      ImageView iv_plus= (ImageView)convertView.findViewById(R.id.iv_plus);
//      ImageView iv_minus= (ImageView)convertView.findViewById(R.id.iv_minus);
//           tv_no_items= (TextView)convertView.findViewById(R.id.tv_no_items);
//        iv_plus.setOnClickListener(new View.OnClickListener() {
//          @Override
//          public void onClick(View v) {
//              // TODO Auto-generated method stub
//              no_of_items=Integer.parseInt(tv_no_items.getText().toString())+1;
//              tv_no_items.setText(no_of_items+"");
//              
//          }
//      }); 
//      
//        iv_minus.setOnClickListener(new View.OnClickListener() {
//          
//          @Override
//          public void onClick(View v) {
//              // TODO Auto-generated method stub
//              if(no_of_items>0){
//
//              no_of_items=Integer.parseInt(tv_no_items.getText().toString())-1;
//              tv_no_items.setText(no_of_items+"");
//              }
//          }
//      });
        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;
    }

}