Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ListView项目上的删除按钮(如iPhone)_Android - Fatal编程技术网

Android ListView项目上的删除按钮(如iPhone)

Android ListView项目上的删除按钮(如iPhone),android,Android,我有一个ListView(带有我自己的适配器),我想通过在ListView上交换一个项目来添加一个删除按钮(就像在iPhone上一样) 我真的不知道怎么做,从哪里开始 你能给我一些提示吗 谢谢您想要实现的是一个自定义ListView。您需要为行设置布局,下面是一个示例 res/layout/row.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientat

我有一个ListView(带有我自己的适配器),我想通过在ListView上交换一个项目来添加一个删除按钮(就像在iPhone上一样)

我真的不知道怎么做,从哪里开始

你能给我一些提示吗


谢谢

您想要实现的是一个自定义ListView。您需要为行设置布局,下面是一个示例

res/layout/row.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/transparent">

    <TextView android:id="@+id/Browse_DateTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent" />


    <ImageButton android:id="@+id/delete"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent" />

</LinearLayout>

然后需要覆盖适配器的getView()方法,有点像这样:

setListAdapter(new ArrayAdapter<Object>(this, R.layout.row, R.id.Browse_DateTime, ourRows) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)BrowseActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.row, null);  
            }    
            TextView time = (TextView) v.findViewById (R.id.Browse_DateTime);
            time.setText(ourRows[position].dateTime);
            ImageButton delete = (ImageButton) v.findViewById(R.id.delete);
            delete.setFocusable(false);
            delete.setImageDrawable(BrowseActivity.this.getResources().getDrawable(R.drawable.deletebutton));  
            delete.setOnClickListener(BrowseActivity.this);
            delete.setId(position);
            return v;
        }
    };)
setListAdapter(新的ArrayAdapter(this,R.layout.row,R.id.Browse\u DateTime,ourRows){
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
如果(v==null){
LayoutInflater vi=(LayoutInflater)浏览活动。this.getSystemService(Context.LAYOUT\u充气机\u服务);
v=vi.充气(R.布局.行,空);
}    
TextView时间=(TextView)v.findViewById(R.id.Browse\u DateTime);
time.setText(ourRows[position].dateTime);
ImageButton delete=(ImageButton)v.findViewById(R.id.delete);
delete.setFocusable(false);
delete.setImageDrawable(BrowseActivity.this.getResources().getDrawable(R.drawable.deletebutton));
delete.setOnClickListener(BrowseActivity.this);
删除.setId(位置);
返回v;
}
};)
致以最良好的祝愿


PS:这是从我的代码中剪切粘贴的,BrowseActivity只是代码所在活动的名称,R.layout.row是我的row.xml文件,您可以任意命名,只需将其放入/res/layout/,如果您的删除按钮是imagebutton,则需要delete.setFocusable(false);(在不使用的情况下尝试一下,看看原因)。

您想要实现的是自定义ListView。您需要为行设置布局,下面是一个示例

res/layout/row.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/transparent">

    <TextView android:id="@+id/Browse_DateTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent" />


    <ImageButton android:id="@+id/delete"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent" />

</LinearLayout>

然后需要覆盖适配器的getView()方法,有点像这样:

setListAdapter(new ArrayAdapter<Object>(this, R.layout.row, R.id.Browse_DateTime, ourRows) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)BrowseActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.row, null);  
            }    
            TextView time = (TextView) v.findViewById (R.id.Browse_DateTime);
            time.setText(ourRows[position].dateTime);
            ImageButton delete = (ImageButton) v.findViewById(R.id.delete);
            delete.setFocusable(false);
            delete.setImageDrawable(BrowseActivity.this.getResources().getDrawable(R.drawable.deletebutton));  
            delete.setOnClickListener(BrowseActivity.this);
            delete.setId(position);
            return v;
        }
    };)
setListAdapter(新的ArrayAdapter(this,R.layout.row,R.id.Browse\u DateTime,ourRows){
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
如果(v==null){
LayoutInflater vi=(LayoutInflater)浏览活动。this.getSystemService(Context.LAYOUT\u充气机\u服务);
v=vi.充气(R.布局.行,空);
}    
TextView时间=(TextView)v.findViewById(R.id.Browse\u DateTime);
time.setText(ourRows[position].dateTime);
ImageButton delete=(ImageButton)v.findViewById(R.id.delete);
delete.setFocusable(false);
delete.setImageDrawable(BrowseActivity.this.getResources().getDrawable(R.drawable.deletebutton));
delete.setOnClickListener(BrowseActivity.this);
删除.setId(位置);
返回v;
}
};)
致以最良好的祝愿


PS:这是从我的代码中剪切粘贴的,BrowseActivity只是代码所在活动的名称,R.layout.row是我的row.xml文件,您可以任意命名,只需将其放入/res/layout/,如果您的删除按钮是imagebutton,则需要delete.setFocusable(false);(试一下,看看原因)。

希望你已经解决了你的问题issues@DilSe你知道怎么做吗?希望你已经解决了你的问题issues@DilSe你知道怎么做吗?谢谢pouzzler但实际上我更想找一款更接近iphone风格的手机,喜欢按钮直接出现在项目的文本视图中,而不是在其右侧,文本视图之外。谢谢pouzzler,但实际上我更喜欢接近iphone风格的东西,喜欢按钮直接出现在项目的文本视图中,而不是在其右侧,文本视图之外。