如何在Android的添加监听器排? [英] how to add listener in android on row?

查看:113
本文介绍了如何在Android的添加监听器排?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让使用自定义适配器和片段的自定义列表。我能够做一个列表视图。但有一个问题,我有一个形象按钮,并在一排文本视图。我想听听该行的单击事件,以及图像按钮的单击事件。换句话说,我需要添加一个监听单击图像按钮,这是该行present当点击该行时将听取和

下面是我的code:

自定义适配器

 公共类CustomAdapter扩展BaseAdapter实现View.OnClickListener {    私人活动活动;
    私人ArrayList的数据;
    私有静态LayoutInflater吹气= NULL;
    公共CustomAdapter(活动,ArrayList的D){        / **********以传递的值********** /
        活性=一个;
        数据= D;
        吹气=(LayoutInflater)活动。
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @覆盖
    公众诠释的getCount(){
        如果(data.size()&下; = 0)
            返回1;
        返回data.size();
    }    @覆盖
    公共对象的getItem(INT位置){
        返回的位置;
    }    @覆盖
    公共无效的onClick(视图v){        Log.d(行点击,排点击);    }    / *********
     *创建一个holder类包含虚增xml文件的元素
     ********* /
    公共静态类ViewHolder {        公众的TextView文本;        公众的ImageButton ImageButton的;    }    @覆盖
    众长getItemId(INT位置){
        返回0;
    }    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){        查看VI = convertView;
        ViewHolder持有人;        如果(convertView == NULL){            / ******充气的每一行tabitem.xml文件(下面定义)******* /
            VI = inflater.inflate(R.layout.row_layout,NULL);            / ******查看Holder对象包含tabitem.xml文件元素****** /            持有人=新ViewHolder();
            holder.text =(TextView中)vi.findViewById(R.id.station_name);            holder.imageButton =(的ImageButton)vi.findViewById(R.id.favorite);            / ************组保持与LayoutInflater ************ /
            vi.setTag(保持器);
        }其他
            支架=(ViewHolder)vi.getTag();        如果(data.size()&下; = 0){
            holder.text.setText(无数据);        }其他{            串串=(字符串)data.get(位置);            / ************在保持元件集模型值*********** /            holder.text.setText(字符串);
        }
        返回VI;
    }
}

row_layout.xml

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=横向
    机器人:descendantFocusability =blocksDescendants>    <的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:ID =@ + ID / STATION_NAME
        机器人:填充=10dp
        机器人:文字颜色=#eee345
        机器人:textAppearance =?机器人:textAppearanceLarge
        />    < ImageButton的机器人:ID =@ + ID /最爱
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =FILL_PARENT
        机器人:SRC =@绘制/启动
        机器人:背景=#00FFFFFF
     />
< / LinearLayout中>

fragmentone.xml

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直
    机器人:背景=#325633
   >    < ListView控件
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:ID =@ + ID / list_view>
    < /&的ListView GT;< / LinearLayout中>

fragmentone.java

 公共类Fragmentone扩展片段{    ArrayList的<串GT;名称;
    布尔值pressed = FALSE;    @Nullable
    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
        查看查看= inflater.inflate(R.layout.fragment_one,集装箱,FALSE);
        NAME =新的ArrayList<串GT;();
        name.add(第一站);
        name.add(第二站);        ListView控件的ListView =(ListView控件)view.findViewById(R.id.list_view);        CustomAdapter customAdapter =新CustomAdapter(getActivity(),名);
        listView.setAdapter(customAdapter);        返回视图。
    }
}


解决方案

检查出来这个code,如果您有任何麻烦明白注释

 公共类CustomAdapter延伸BaseAdapter {    私人活动活动;
    私人ArrayList的数据;
    私有静态LayoutInflater吹气= NULL;    公共CustomAdapter(活动,ArrayList的D){        / **********以传递的值********** /
        活性=一个;
        数据= D;
        吹气=(LayoutInflater)活动
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);    }    @覆盖
    公众诠释的getCount(){
        如果(data.size()&下; = 0)
            返回1;
        返回data.size();
    }    @覆盖
    公共对象的getItem(INT位置){
        返回的位置;
    }    / *********
     *创建一个holder类包含虚增xml文件的元素
     ********* /
    公共静态类ViewHolder {        公众的TextView文本;        公众的ImageButton ImageButton的;    }    @覆盖
    众长getItemId(INT位置){
        返回0;
    }    @覆盖
    公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){        查看VI = convertView;
        ViewHolder持有人;        如果(convertView == NULL){            / ******充气的每一行tabitem.xml文件(下面定义)******* /
            VI = inflater.inflate(R.layout.row_layout,NULL);            / ******查看Holder对象包含tabitem.xml文件元素****** /            持有人=新ViewHolder();
            holder.text =(TextView中)vi.findViewById(R.id.station_name);            holder.imageButton =(的ImageButton)vi.findViewById(R.id.favorite);            / ************组保持与LayoutInflater ************ /
            vi.setTag(保持器);
        }其他
            支架=(ViewHolder)vi.getTag();        如果(data.size()&下; = 0){
            holder.text.setText(无数据);        }其他{            串串=(字符串)data.get(位置);            / ************在保持元件集模型值*********** /            holder.text.setText(字符串);        }        //这是整体排点击
        convertView.setOnClickListener(新View.OnClickListener(){            @覆盖
            公共无效的onClick(视图v){            }
        });
        //这是图像按钮的onclick
        holder.imageButton.setOnClickListener(新View.OnClickListener(){            @覆盖
            公共无效的onClick(视图v){            }
        });
        ;        返回VI;
    }
}

I am trying to make a custom list using a custom adapter and a fragment. I am able to make a list view. But there is one problem, I have an image button and a text view in a row. I want to listen to the click event of the row, as well as the Image button's click event. In other words, I need to add a listener that will listen when the row is clicked and when the image button that is present on the row is clicked.

Here is my code:

custom adapter

public class CustomAdapter extends BaseAdapter implements View.OnClickListener {

    private Activity activity;
    private ArrayList data;
    private static LayoutInflater inflater = null;


    public CustomAdapter(Activity a, ArrayList d) {

        /********** Take passed values **********/
        activity = a;
        data = d;
        inflater = (LayoutInflater) activity.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);


    }


    @Override
    public int getCount() {
        if (data.size() <= 0)
            return 1;
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public void onClick(View v) {

        Log.d("row is click","row click");

    }

    /*********
     * Create a holder Class to contain inflated xml file elements
     *********/
    public static class ViewHolder {

        public TextView text;

        public ImageButton imageButton;

    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View vi = convertView;
        ViewHolder holder;

        if (convertView == null) {

            /****** Inflate tabitem.xml file for each row ( Defined below ) *******/
            vi = inflater.inflate(R.layout.row_layout, null);

            /****** View Holder Object to contain tabitem.xml file elements ******/

            holder = new ViewHolder();
            holder.text = (TextView) vi.findViewById(R.id.station_name);

            holder.imageButton = (ImageButton) vi.findViewById(R.id.favorite);

            /************  Set holder with LayoutInflater ************/
            vi.setTag(holder);
        } else
            holder = (ViewHolder) vi.getTag();

        if (data.size() <= 0) {
            holder.text.setText("No Data");

        } else {

            String string = (String) data.get(position);

            /************  Set Model values in Holder elements ***********/

            holder.text.setText(string);


        }
        return vi;
    }
}

row_layout.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="horizontal"
    android:descendantFocusability="blocksDescendants">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/station_name"
        android:padding="10dp"
        android:textColor="#eee345"
        android:textAppearance="?android:textAppearanceLarge"
        />

    <ImageButton android:id="@+id/favorite"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:src="@drawable/start"
        android:background="#00ffffff"
     />


</LinearLayout>

fragmentone.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"
    android:background="#325633"
   >

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/list_view">
    </ListView>

</LinearLayout>

fragmentone.java

public class Fragmentone  extends Fragment{

    ArrayList<String> name;
    boolean isPressed=false;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_one, container, false);
        name=new ArrayList<String>();
        name.add("First Station");
        name.add("Second Station");





        ListView listView = (ListView) view.findViewById(R.id.list_view);

        CustomAdapter customAdapter =new CustomAdapter(getActivity(),name);
        listView.setAdapter(customAdapter);



        return view;
    }


}

解决方案

Check it out this code, if you have any trouble to understand comment

public class CustomAdapter extends BaseAdapter {

    private Activity activity;
    private ArrayList data;
    private static LayoutInflater inflater = null;

    public CustomAdapter(Activity a, ArrayList d) {

        /********** Take passed values **********/
        activity = a;
        data = d;
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public int getCount() {
        if (data.size() <= 0)
            return 1;
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    /*********
     * Create a holder Class to contain inflated xml file elements
     *********/
    public static class ViewHolder {

        public TextView text;

        public ImageButton imageButton;

    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        View vi = convertView;
        ViewHolder holder;

        if (convertView == null) {

            /****** Inflate tabitem.xml file for each row ( Defined below ) *******/
            vi = inflater.inflate(R.layout.row_layout, null);

            /****** View Holder Object to contain tabitem.xml file elements ******/

            holder = new ViewHolder();
            holder.text = (TextView) vi.findViewById(R.id.station_name);

            holder.imageButton = (ImageButton) vi.findViewById(R.id.favorite);

            /************ Set holder with LayoutInflater ************/
            vi.setTag(holder);
        } else
            holder = (ViewHolder) vi.getTag();

        if (data.size() <= 0) {
            holder.text.setText("No Data");

        } else {

            String string = (String) data.get(position);

            /************ Set Model values in Holder elements ***********/

            holder.text.setText(string);

        }

        // this is for overall row click
        convertView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

            }
        });
        // this is for image button onclick
        holder.imageButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

            }
        });
        ;

        return vi;
    }
}

这篇关于如何在Android的添加监听器排?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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