如何将onClickListener添加到ListView行

时间:2016-04-08 08:18:27

标签: android listview android-fragments onclicklistener

所以我正在创建一个应用程序,其中一个功能是您可以设置警报。下面是我使用适配器放入ListView的2个示例警报。每个项目都有一个ImageView,一个TextView和另一个ImageView。

enter image description here

我的问题是我如何为不同的图像打开onClickListener,甚至可能根据我的工作方式打开文本。

例如,当您单击“x”时,它将从列表中删除该警报(向我显示如何执行并让适配器知道已删除警报会很酷)或单击警报图片将激活/停用警报(可以在看到的两个图像之间切换)。

此屏幕来自MainActivity AlertFragment。在过去,我遇到了Fragments实现内部类的问题,但我现在设置它的方式是(我认为)Fragments实现内部类。这就是我目前所知道的错误:

public class AlertsFragment extends Fragment {

    private ListView mListView;
    private List<Alert> alerts = new ArrayList<>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_alerts, container, false);

        mListView = (ListView) rootView.findViewById(R.id.alerts_list_view);

        alerts.add(new Alert(1, 5400, true));
        alerts.add(new Alert(2, 120, false));

        AlertsAdapter adapter = new AlertsAdapter(getActivity(), R.layout.alert_item_row, alerts);
        mListView.setAdapter(adapter);

        getActivity().setTitle(new AlertsItem().getLabel());

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                Log.d("matt", "list_item");
                ImageView img = (ImageView) getActivity().findViewById(R.id.active_image);
                img.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        Log.d("matt", "image");
                        alerts.get(position).setActive(!alerts.get(position).isActive());
                    }
                });
            }
        });
        return rootView;
    }
}

你们可以给予的任何帮助都将不胜感激,谢谢。

----------------------------------------------- ----------编辑--------------------------------------- ------------------

正如一些人所要求的,这是我的ArrayAdapter代码:

/**
 * Adapter for adding Alerts to ListView
 */
public class AlertsAdapter extends ArrayAdapter {

    private int resource;
    List<Alert> objects;

    public AlertsAdapter(Context context, int resource, List<Alert> objects) {
        super(context, resource, objects);
        this.resource = resource;
        this.objects = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        AlertHolder holder = null;

        if (row == null) {
            LayoutInflater inflater = ((Activity)getContext()).getLayoutInflater();
            row = inflater.inflate(resource, parent, false);

            holder = new AlertHolder();
                holder.imgActive = (ImageView)row.findViewById(R.id.active_image);
            holder.txtTitle = (TextView)row.findViewById(R.id.txt_title);
            holder.txtTime = (TextView)row.findViewById(R.id.txt_time);

            row.setTag(holder);
        }
        else {
            holder = (AlertHolder)row.getTag();
        }

        Alert alert = (Alert) getItem(position);
        int image;
        if (alert.isActive()) {
            image = R.drawable.ic_alert_active_medium;
        }
        else {
            image = R.drawable.ic_alert_inactive_medium;
        }

        holder.imgActive.setImageResource(image);
        holder.txtTitle.setText("Route " + Integer.toString(alert.getRoute()));

        int min = alert.getTime() / 60;
        int hr = min / 60;
        min = min % 60;

        holder.txtTime.setText(((hr == 0) ? "" : (Integer.toString(hr) + " hr ")) + Integer.toString(min) + " min ");

        return row;
    }

    private static class AlertHolder {
        ImageView imgActive;
        TextView txtTitle;
        TextView txtTime;
    }
}

3 个答案:

答案 0 :(得分:2)

创建自己的Adapter,扩展ArrayAdapter(请参阅this link),覆盖getView()方法,然后您可以在其中添加OnClickListenerView的孩子ListView。要通知ListView某个孩子已被删除,请致电notifyDataSetChanged() ListView上的Adapter

您可以使用ListView解决此问题,但我建议您使用RecyclerViewAndroid RecyclerView)。 RecyclerView更好地支持更新其子项(如删除,插入,更新子项),并设置了默认动画。

答案 1 :(得分:0)

您必须在AlertsAdapter类中管理单击侦听器,以获取单独的视图,例如x按钮,图像。

答案 2 :(得分:0)

您可以在onClick方法中处理此问题。例;

 public MyViewHolder(final View itemView) {
        super(itemView);

        iconImageView = (ImageView) itemView.findViewById(R.id.myRecyclerImageView);
        iconTextView = (TextView) itemView.findViewById(R.id.myRecyclerTextView);

        itemView.setOnClickListener(this);
        iconTextView.setOnClickListener(this);
        iconImageView.setOnLongClickListener(this);
    } 

    // onClick Listener for view 
    @Override 
    public void onClick(View v) {

        if (v.getId() == iconTextView.getId()){
            Toast.makeText(v.getContext(), "ITEM PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
        } else { 
            Toast.makeText(v.getContext(), "ROW PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
        } 
    }