Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.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的一项?并在我的ListView上检测手势?_Android_Android Layout_Listview_Android Studio_Gesture - Fatal编程技术网

Android 如何使相对布局成为ListView的一项?并在我的ListView上检测手势?

Android 如何使相对布局成为ListView的一项?并在我的ListView上检测手势?,android,android-layout,listview,android-studio,gesture,Android,Android Layout,Listview,Android Studio,Gesture,我试图制作一个列表视图,其中包含相对的布局项,一个接一个。每个相对布局都由一个按钮视图组成,后面是三个文本视图,这些文本视图与按钮右侧对齐,彼此下方对齐。我的RelativeLayout XML代码如下所示,这很好,但我如何使其在按下按钮后使用此模板填充ListView,以及如何操作TextView中的文本,以便它们为每个相对布局项显示不同的内容 <RelativeLayout android:layout_width="wrap_content"

我试图制作一个列表视图,其中包含相对的布局项,一个接一个。每个相对布局都由一个按钮视图组成,后面是三个文本视图,这些文本视图与按钮右侧对齐,彼此下方对齐。我的RelativeLayout XML代码如下所示,这很好,但我如何使其在按下按钮后使用此模板填充ListView,以及如何操作TextView中的文本,以便它们为每个相对布局项显示不同的内容

<RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <Button
                android:id="@+id/buttonA"
                android:layout_width="100sp"
                android:layout_height="100sp"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:gravity="center_vertical"
                android:text="SomeText"
                android:textColor="#fff"
                android:onClick="butt"/>
            <TextView
                android:id="@+id/name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Name:"
                android:textSize="30sp"
                android:textColor="#000000"
                android:layout_toRightOf="@+id/buttonA"/>
            <TextView
                android:id="@+id/date"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Date:"
                android:textSize="30sp"
                android:textColor="#000000"
                android:layout_toRightOf="@+id/buttonA"
                android:layout_below="@+id/name"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Duration:"
                android:textSize="30sp"
                android:textColor="#000000"
                android:layout_toRightOf="@+id/buttonA"
                android:layout_below="@+id/date"/>
        </RelativeLayout>
其中,convalv是listview,convalButtons应该是一个相对布局的数组

编辑2:
我实现了Nolly代码的一个版本,其中更改了getListItemData()方法,以便每次只向列表中添加一项。我还全局初始化了活动中的列表listViewItems,以便每个项目都可以添加。到目前为止,这似乎是我正在寻找的。然而;我遇到了一个问题,我的活动无法检测到ListView区域的手势。这是一个问题,因为我使用GestureDetector.OnGetureListener.onFling()方法在显示布局之间切换。如何在我的ListView上检测手势?

因为您有一个要添加到ListView的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<Button
    android:id="@+id/buttonA"
    android:layout_width="100sp"
    android:layout_height="100sp"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:gravity="center_vertical"
    android:onClick="butt"
    android:text="SomeText"
    android:textColor="#fff" />

<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/buttonA"
    android:text="Name:"
    android:textColor="#000000"
    android:textSize="30sp" />

<TextView
    android:id="@+id/date"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/name"
    android:layout_toRightOf="@+id/buttonA"
    android:text="Date:"
    android:textColor="#000000"
    android:textSize="30sp" />

<TextView
    android:id="@+id/duration"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/date"
    android:layout_toRightOf="@+id/buttonA"
    android:text="Duration:"
    android:textColor="#000000"
    android:textSize="30sp" />
</RelativeLayout>
现在,我们将创建一个活动文件及其布局文件。在活动布局文件中,在xml文件中添加ListView小部件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

<ListView
    android:id="@+id/mechanics"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:dividerHeight="1.0sp"
    android:scrollbars="none"
    android:background="@android:color/transparent"
    android:cacheColorHint="@android:color/transparent"
    android:layout_centerHorizontal="true"/>

</RelativeLayout>

然后,在活动类中,ListView被实例化。适配器类的一个对象也是create。数据对象作为适配器类中的一个参数传递

最后,调用ListView的setAdapter()方法,并将适配器类的对象作为参数传递给它

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class TestingListView extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_testing_list_view);

    ListView mListView = (ListView)findViewById(R.id.mechanics);
    ListAdapter listAdapter = new ListAdapter(this, getListItemData());
    mListView.setAdapter(listAdapter);
}

private List getListItemData() {
    List<ItemObjects> listViewItems = new ArrayList<ItemObjects>();
    listViewItems.add(new ItemObjects("Good", "Peter", "12-03-1989", "400"));
    listViewItems.add(new ItemObjects("Good", "John", "10-04-1999", "560"));
    listViewItems.add(new ItemObjects("Good", "James", "01-09-1970", "320"));
    return listViewItems;
}
}
导入android.support.v7.app.app活动;
导入android.os.Bundle;
导入android.widget.ListView;
导入java.util.ArrayList;
导入java.util.List;
公共类TestingListView扩展了AppCompatActivity{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u testing\u list\u视图);
ListView mListView=(ListView)findViewById(R.id.mechanics);
ListAdapter ListAdapter=新的ListAdapter(这个,getListItemData());
mListView.setAdapter(listAdapter);
}
私有列表getListItemData(){
List listViewItems=new ArrayList();
添加(新项目对象(“好”、“彼得”、“12-03-1989”、“400”));
添加(新项目对象(“好”、“约翰”、“10-04-1999”、“560”));
添加(新项目对象(“好”、“詹姆斯”、“01-09-1970”、“320”);
返回listViewItems;
}
}

此代码未经测试。我希望它能让您了解您计划实现的目标。

您正在尝试的代码是什么?请分享你的代码?老实说,这是我完全困惑的部分。我对java和编程基本上是新手。我已经查看了这里的文档,我正在尝试将一个项目添加到ListView中。非常感谢!我将对此进行实验,并让您知道它是如何工作的。首先,我实现了您建议的代码的一个版本,更改了getListItemData()方法,以便每次只向列表中添加一项。我还全局初始化了活动中的列表listViewItems,以便每个项目都可以添加。到目前为止,这似乎是我正在寻找的。然而;我遇到了一个问题,我的活动无法检测到ListView区域上的手势。这是一个问题,因为我使用GestureDetector.OnGetureListener.onFling()方法在显示布局之间切换。你对此有什么建议吗?
public class ItemObjects {

private String image;

private String name;
private String date;
private String duration;

public ItemObjects(String image, String name, String date, String duration) {
    this.image = image;
    this.name = name;
    this.date = date;
    this.duration = duration;
}

public String getImage() {
    return image;
}

public String getName() {
    return name;
}

public String getDate() {
    return date;
}

public String getDuration() {
    return duration;
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

<ListView
    android:id="@+id/mechanics"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:dividerHeight="1.0sp"
    android:scrollbars="none"
    android:background="@android:color/transparent"
    android:cacheColorHint="@android:color/transparent"
    android:layout_centerHorizontal="true"/>

</RelativeLayout>
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class TestingListView extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_testing_list_view);

    ListView mListView = (ListView)findViewById(R.id.mechanics);
    ListAdapter listAdapter = new ListAdapter(this, getListItemData());
    mListView.setAdapter(listAdapter);
}

private List getListItemData() {
    List<ItemObjects> listViewItems = new ArrayList<ItemObjects>();
    listViewItems.add(new ItemObjects("Good", "Peter", "12-03-1989", "400"));
    listViewItems.add(new ItemObjects("Good", "John", "10-04-1999", "560"));
    listViewItems.add(new ItemObjects("Good", "James", "01-09-1970", "320"));
    return listViewItems;
}
}