可展开的子列表视图项单击图像

时间:2015-09-28 15:29:02

标签: java android xml listview expandablelistview

在上图中,我想在按下加号按钮时将textview(红色)值增加1,在子列表视图项目中按下减号时减少1。我不想单击整个列表项,只需要加上和减去图像视图。现在发生了什么,我必须双击列表项来增加或减少textView(红色)的值。此外,当我滚动列表视图时,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");


            }
    }

ExpandableListAdapter.java

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;
    }

}

0 个答案:

没有答案