【问题标题】:android Expandable ListView on ChildClick Listner is not workingChild Click Listener 上的 android Expandable ListView 不起作用
【发布时间】:2017-12-16 03:20:13
【问题描述】:

我有一个ExpandableListView 列表运行良好,但我想当我点击 Childs 时,时钟图标改变颜色并做某事并保存,但是当我点击孩子时,第一个时钟第一个孩子做这项工作,但是当我向下滚动一个随机孩子或其中 2 o 3 个也会改变颜色时,如果我关闭应用程序这个操作系统我的适配器代码,它将被保存

public class exTourListAdapter extends BaseExpandableListAdapter {

private Context context;
private ArrayList<GroupTour> groups;
private int count;
SharedPreferences preferences;
AppCompatTextView timeTour ,tvPrize ,tvGameMode,tvBuyIn,tvLateReg
,tvReBuy,tvDays;
RelativeLayout childToursLayout;


Clock clock ;
public exTourListAdapter(Context context , ArrayList<GroupTour> groups,int count) {
    this.context = context;
    this.groups = groups;
    this.count = count;
}

@Override
public ChildTour getChild(int groupPosition, int childPosition) {
    GroupTour group = groups.get(groupPosition);
    exChild exchild = group.getChildren().get(groupPosition);
    ChildTour child = exchild.getChildTours().get(childPosition);
    return child;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    ChildTour childTour= getChild(groupPosition,childPosition);
    int id= childTour.getTourId();
    return id;
}

@Override
public View getChildView(int groupPosition, int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {

    ChildTour child =  getChild(groupPosition,
            childPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.ex_list_tours_child, null);
    }
    preferences = context.getSharedPreferences("timeonoff",Context.MODE_PRIVATE);

    timeTour = (AppCompatTextView) convertView.findViewById(R.id.time_tour);
    tvPrize = (AppCompatTextView) convertView.findViewById(R.id.tour_prize);
    tvGameMode = (AppCompatTextView) convertView.findViewById(R.id.tour_gamemode);
    tvBuyIn = (AppCompatTextView) convertView.findViewById(R.id.tour_buy_in);
    tvLateReg = (AppCompatTextView) convertView.findViewById(R.id.tour_late_reg);
    tvReBuy = (AppCompatTextView) convertView.findViewById(R.id.tour_rebuy);
    tvDays = (AppCompatTextView) convertView.findViewById(R.id.tour_days);
    clock = (Clock) convertView.findViewById(R.id.clock_layour);
    tvDays.setTag(child.getTourId());
    clock.setHours(child.getHour());
    clock.setMinutes(child.getMin());

    tvLateReg.setText("Late Register: "+child.getLateReg()+" min");
    int id = (int) getChildId(groupPosition,childPosition);
    if (preferences.getBoolean("time"+id,false) == true){
        clock.setClockColor(R.color.material_green_500);
    }else if (preferences.getBoolean("time"+id,true)==false){
        clock.setClockColor(R.color.material_brown_700);
    }
    if (child.getReBuy().equals("")){
        tvReBuy.setText("Rebuy: ?");
    }else {
        tvReBuy.setText("Rebuy: "+child.getReBuy());
    }
    tvDays.setText(child.getDay());
    tvBuyIn.setText("BuyIn: "+child.getBuyIn());
    tvGameMode.setText(child.getGameMode());
    tvPrize.setText(child.getPrize());
    timeTour.setText(child.getTime());
    return convertView;

}

@Override
public int getChildrenCount(int groupPosition) {
   int number = groups.get(groupPosition).getChildren().get(groupPosition).getChildNumbers();

    return number;
}

@Override
public GroupTour getGroup(int groupPosition) {
    // TODO Auto-generated method stub
    return groups.get(groupPosition);
}

@Override
public int getGroupCount() {
    // TODO Auto-generated method stub
    return groups.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) {
    GroupTour group =  getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater inf = (LayoutInflater) context
                .getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = inf.inflate(R.layout.list_header_groups_tours, null);
    }
    TextView tv = (TextView) convertView.findViewById(R.id.tours_site_name);
    tv.setText(group.getName());
    // TODO Auto-generated method stub
    return convertView;
}

@Override
public boolean hasStableIds() {
    // TODO Auto-generated method stub
    return true;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    return true;
}
public void onClockClick(View view){
    clock.setClockColor(R.color.material_red_300);
}

这是我的片段代码,时钟类是时钟图标

public class ListToursexFragment extends Fragment {
SwipeRefreshLayout refresh;
ExpandableListView listView;
SharedPreferences.Editor editor;
SharedPreferences pref;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater,
                         @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {
    View view= inflater.inflate(R.layout.activity_tours,container,false);
    pref= getActivity().getSharedPreferences("timeonoff", Context.MODE_PRIVATE);
    editor =pref.edit();
    refresh = (SwipeRefreshLayout) view.findViewById(R.id.refresh_tours);
    listView = (ExpandableListView) view.findViewById(R.id.list_tours);
    refresh.setColorSchemeColors(
            R.color.material_green_200,
            R.color.material_green_400,
            R.color.material_green_600,
            R.color.material_green_800
    );
    refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            prepareData(url,listView);
        }
    });
    prepareData(url,listView);
    return view;
}

private void prepareData(String url, final ExpandableListView listView) {
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {

                JSONArray array = response.getJSONArray("sites");
                int count = response.getInt("sitescount");
                exTourParser parser = new exTourParser();
                final ArrayList<GroupTour> items = parser.Parse(array);

                exTourListAdapter adapter = new exTourListAdapter(getContext(), items,count);
                listView.setAdapter(adapter);
                listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
                    @Override
                    public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long id) {
                        Clock clock = (Clock) view.findViewById(R.id.clock_layour);
                        boolean b= pref.getBoolean("time"+id,false);
                        if (b ==true){
                            editor.putBoolean("time"+id,false);
                            editor.commit();
                            clock.setClockColor(R.color.material_brown_700);
                        }else if (b == false){
                            editor.putBoolean("time"+id,true);
                            editor.commit();
                            clock.setClockColor(R.color.material_green_700);
                        }
                        return false;

                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getContext(), "ridiok", Toast.LENGTH_SHORT).show();
        }
    });
    RequestQueue quew = Volley.newRequestQueue(getActivity().getApplicationContext());
    quew.add(request);
}

这个屏幕截图 firstsecond只点击第一个孩子然后滚动几次然后这个黑色是默认颜色

【问题讨论】:

    标签: android listview expandablelistview


    【解决方案1】:

    当您在适配器的 getView 方法中工作并且您在其中使用 IF 条件时,请尝试同时使用“else”。 在你的情况下,我可以看到 'if' 和 'else if',而

    其他

    不见了。

    if (b ==true)
    {
                 editor.putBoolean("time"+id,false);
                 editor.commit();
                 clock.setClockColor(R.color.material_brown_700);
    }
    else if (b == false)
    {
              editor.putBoolean("time"+id,true);
              editor.commit();
              clock.setClockColor(R.color.material_green_700);
    }
    

    可以转换成

     if (b)
        {
                     editor.putBoolean("time"+id,false);
                     editor.commit();
                     clock.setClockColor(R.color.material_brown_700);
        }
        else 
        {
                  editor.putBoolean("time"+id,true);
                  editor.commit();
                  clock.setClockColor(R.color.material_green_700);
        }
    

    【讨论】:

      猜你喜欢
      • 2013-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多