简体   繁体   English

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

[英]How do I make a relative layout an item of my ListView? And detect gestures over my ListView?

I am trying to make a ListView that has relative layout items, one after another. 我试图使一个ListView具有相对的布局项,一个接一个。 Each relative layout consists of a Button view, followed by three TextViews aligned to the right of the Button, and below each other. 每个相对布局均包含一个Button视图,其后是三个TextView,它们分别位于Button的右侧和下方。 My RelativeLayout XML code is as follows, and it is fine, but how do I make it so that I can populate my ListView with this template after a button press, and how can I manipulate the text inside the textviews so that they say different things for each relative layout item? 我的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>

Thanks for the help 谢谢您的帮助

Edit: Someone has asked me to share how I am trying to implement this. 编辑:有人要求我分享我如何实现这一目标。 Honestly, thats the part I'm entirely confused about. 老实说,那是我完全困惑的部分。 I'm new to java and programming in general. 我是Java和一般编程的新手。 I've looked over the documentation here 我在这里查看了文档

http://developer.android.com/reference/android/widget/ListView.html http://developer.android.com/reference/android/widget/ListView.html

here is where I am trying to add an item into the listview 这是我试图在列表视图中添加项目的地方

public void startStop(View view){
    Button ssButton = (Button) findViewById(R.id.ssbutton);
    if(startStopTracker == 0){
        ssButton.setText("STOP");
        ssButton.setBackgroundColor(Color.parseColor("#F44336"));
        startStopTracker = 1;
        enableSwipe = false;
        return;
    }
    if(startStopTracker == 1){
        ssButton.setText("START");
        ssButton.setBackgroundColor(Color.parseColor("#0D47A1"));
        startStopTracker = 0;
        enableSwipe = true;

        convoLV.addView(convoButtons[0]);


        return;
    }
}

where convoLV is the listview and convoButtons is supposed to be an array of relative layouts. 其中convoLV是列表视图,而convoButtons应该是相对布局的数组。

EDIT 2: I implemented a version of Nolly's code with a change in the getListItemData() method so that only one item is adde to the list each time. 编辑2:我实现了Nolly代码的一个版本,其中对getListItemData()方法进行了更改,以便每次仅将一项添加到列表中。 I also initialized the List listViewItems globally in the activity so that each item would add on. 我还在活动中全局初始化了List listViewItems,以便添加每个项目。 It seems so far to be what I am looking for. 到目前为止,这似乎是我要寻找的。 However; 然而; I am running into an issue where my activity can't detect gestures over the area of my ListView. 我遇到了一个问题,我的活动无法检测到ListView区域上的手势。 This is an issue because I use the GestureDetector.onGestureListener.onFling() method to switch between displaying layouts. 这是一个问题,因为我使用GestureDetector.onGestureListener.onFling()方法在显示布局之间进行切换。 How can I detect gestures over my ListView? 如何在ListView上检测手势?

Since you have a layout you want to add to a 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>

then you have to create an Adapter that will inflates your layout file. 那么您必须创建一个适配器,以扩展您的布局文件。 It will bind your data to the item views in your layout. 它将您的数据绑定到布局中的项目视图。

The Adapter code is below 适配器代码如下

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 java.util.List;

public class ListAdapter extends BaseAdapter {

private LayoutInflater lInflater;
private List<ItemObjects> listStorage;

public ListAdapter(Context context, List<ItemObjects> customizedListView) {
    lInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    listStorage = customizedListView;
}

@Override
public int getCount() {
    return listStorage.size();
}

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

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

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

    ViewHolder listViewHolder;
    if (convertView == null) {
        listViewHolder = new ViewHolder();
        convertView = lInflater.inflate(R.layout.list_items, parent, false);

        listViewHolder.image = (Button) convertView.findViewById(R.id.buttonA);

        listViewHolder.name = (TextView) convertView.findViewById(R.id.name);
        listViewHolder.date = (TextView) convertView.findViewById(R.id.date);
        listViewHolder.duration = (TextView) convertView.findViewById(R.id.duration);

        convertView.setTag(listViewHolder);
    } else {
        listViewHolder = (ViewHolder) convertView.getTag();
    }
    listViewHolder.name.setText(listStorage.get(position).getName());
    listViewHolder.date.setText(listStorage.get(position).getDate());
    listViewHolder.duration.setText(listStorage.get(position).getDuration());

    listViewHolder.image.setText(listStorage.get(position).getImage());

    return convertView;
}

static class ViewHolder {

    Button image;
    TextView name;
    TextView date;
    TextView duration;

}
}

Create an object class that will hold data for the items in your layout file. 创建一个对象类,该类将保存布局文件中各项的数据。 See the code below. 请参见下面的代码。 You can modify it to suit your needs 您可以修改它以满足您的需求

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;
}
}

Now we will create an Activity file with its layout file. 现在,我们将使用其布局文件创建一个活动文件。 In the activity layout file, add a ListView widget in the xml file 在活动布局文件中,在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>

Then, in your activity class, the ListView is instantiated. 然后,在您的活动类中,实例化ListView。 An object of the Adapter class is also create. 还将创建Adapter类的对象。 The data object is passed as one of the parameters in the Adapter class 数据对象作为Adapter类中的参数之一传递

Finally, call the setAdapter() method of the ListView and pass the object of the adapter class as parameter to it. 最后,调用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;
}
}

This code was not tested. 此代码未经测试。 I hope it gives you an idea of what you are planning to achieve. 我希望它能使您对计划实现的目标有所了解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM