美文网首页
2020-05-20 ListView的Item中含有Butto

2020-05-20 ListView的Item中含有Butto

作者: xiaohuage | 来源:发表于2020-05-20 18:11 被阅读0次

    场景:ListView的Item中包含Button控件,会出现Item点击失效,不响应,Button可点击,响应。

    解决方法:在Button的xml属性中加入这两句代码解决,

    android:focusable="false"

    android:focusableInTouchMode="false"

    实现:Item可点击,响应,Button可点击,响应

    代码:

    主页面:

    package www.yalong.com.listview_button;

    import android.support.v7.app.AppCompatActivity;

    import android.os.Bundle;

    import android.view.View;

    import android.widget.Adapter;

    import android.widget.AdapterView;

    import android.widget.ListView;

    import android.widget.Toast;

    import java.util.ArrayList;

    import java.util.List;

    public class MainActivity extends AppCompatActivity  implements AdapterView.OnItemClickListener{

    ListView listView;

        List list;

        @Override

        protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_main);

            listView=findViewById(R.id.listView);

            list=new ArrayList();

            for (int i=0;i<10;i++){

    list.add("第"+i+"行数据");

            }

    MyAdapter adapter=new MyAdapter(MainActivity.this,list);

            listView.setAdapter(adapter);

            listView.setOnItemClickListener(this);

        }

    @Override

        public void onItemClick(AdapterView adapterView, View view, int i, long l) {

    Toast.makeText(MainActivity.this,"点击了第"+i+"行",Toast.LENGTH_LONG).show();

        }

    }

    适配器:

    package www.yalong.com.listview_button;

    import android.content.Context;

    import android.view.LayoutInflater;

    import android.view.View;

    import android.view.ViewGroup;

    import android.widget.BaseAdapter;

    import android.widget.Button;

    import android.widget.TextView;

    import android.widget.Toast;

    import java.util.List;

    /**

    * Created by Administrator on 2020/5/20 0020.

    */

    public class MyAdapter extends BaseAdapter  {

    Context  context;

        List list;

        public MyAdapter(Context context, List list) {

            this.context = context;

            this.list = list;

        }

    @Override

        public int getCount() {

    return list.size();

        }

    @Override

        public ObjectgetItem(int i) {

    return list.get(i);

        }

    @Override

        public long getItemId(int i) {

    return i;

        }

    @Override

        public View  getView(final int i, View view, ViewGroup viewGroup) {

    ViewHolder viewHolder;

            if (view ==null) {

    viewHolder =new ViewHolder();

                view = LayoutInflater.from(context).inflate(R.layout.layout_item, viewGroup, false);

                viewHolder.textView = view.findViewById(R.id.item_txt);

                viewHolder.button = view.findViewById(R.id.item_btn);

                view.setTag(viewHolder);

            }else {

    viewHolder = (ViewHolder) view.getTag();

            }

    String string ="按钮" + i;

            viewHolder.textView.setText(list.get(i).toString());

            viewHolder.button.setText(string);

            viewHolder.button.setOnClickListener(new View.OnClickListener() {

    @Override

                public void onClick(View view) {

    Toast.makeText(context,"点击了第"+i+"个按钮",Toast.LENGTH_LONG).show();

                }

    });

            return view;

        }

    static class ViewHolder {

           TextView textView;

            Button button;

        }

    }

    xml:

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

        xmlns:tools="http://schemas.android.com/tools"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:orientation="vertical"

        tools:context="www.yalong.com.listview_button.MainActivity">

    <ListView

        android:id="@+id/listView"

        android:layout_width="match_parent"

        android:layout_height="match_parent"/>

    </LinearLayout>

    layout_item.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">

    <TextView

            android:id="@+id/item_txt"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content" />

    <android.support.v4.widget.Space

            android:layout_width="0dp"

            android:layout_height="1dp"

            android:layout_weight="1" />

    <Button

            android:id="@+id/item_btn"

            android:focusable="false"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content" />

    </LinearLayout>

    实现效果视屏链接:ListView控件Item有Button点击事件冲突解决后效果

    相关文章

      网友评论

          本文标题:2020-05-20 ListView的Item中含有Butto

          本文链接:https://www.haomeiwen.com/subject/pbwnohtx.html