java - ListViewDragginanimation 中的自定义 ListView 项

标签 java android listview android-listview

所以我有一个 android 项目,我在其中很好地使用了排序功能。我一直在使用 DevBytes ListViewDraggingAnimation https://www.youtube.com/watch?v=_BZIvjMgH-Q

我已经让它在我的应用程序和布局中工作,但仍然存在一个大问题,我无法让它与我的自定义 ListView 项目一起工作。 DevBytes 代码中的库存项是一个普通的 TextView,如下所示:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFF"
    android:textSize="@dimen/list_text_size"
    android:gravity="center_vertical"
    android:paddingLeft="15dp"
    android:paddingRight="15dp"
    android:minHeight="@dimen/list_item_height"
    android:textColor="#000000"
/>

我的自定义 ListView 项目由一个 RelativeLayout 和四个 TextView 组成。因此:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="adress"
        android:id="@+id/item_adressView"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/item_driveTypeView"
        android:textSize="15dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="postnr, ort"
        android:id="@+id/item_zipcodePlaceView"
        android:textSize="15dp"
        android:layout_below="@+id/item_adressView"
        android:layout_toEndOf="@+id/item_driveTypeView" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="företag"
        android:id="@+id/item_companyView"
        android:textSize="15dp"
        android:layout_below="@+id/item_zipcodePlaceView"
        android:layout_toEndOf="@+id/item_driveTypeView" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="P1"
        android:id="@+id/item_driveTypeView"
        android:textSize="40dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

诚然,我对数据结构不是很了解。我为接受我的项目所做的每一次尝试都使程序崩溃了。我担心我在某些尝试中更改了比需要更多的代码部分。有人可以帮我指出我必须更改哪些代码部分才能使 ListView 接受我的自定义项吗?

发布所有 java 代码将使我的帖子超出字符数限制。

代码可以在这里找到: http://developer.android.com/shareables/devbytes/ListViewDraggingAnimation.zip

最佳答案

如果要将数据绑定(bind)到多个 View (除了简单的 TextView 之外),则需要更改适配器。阅读 ArrayAdapter reference了解这些适配器的工作原理。

基本上,您的构造函数需要接受您的复杂数据结构,然后您需要覆盖 getView() 以将数据绑定(bind)到多个 View 。使用 DevBytes 示例,尝试如下操作:

// Update the adapter to use string arrays instead of just strings
public class StableArrayAdapter extends ArrayAdapter<String[]> {

    final int INVALID_ID = -1;
    HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

    // Save these references to the layout and string data
    int mRowLayout;
    List<String[]> mStrings;

    // Constructor should accept string arrays
    public StableArrayAdapter(Context context, int rowLayout, List<String[]> objects) {

        super(context, rowLayout, objects);

        // Change this so your string array list can be indexed
        for (int i = 0; i < objects.size(); ++i) {

            // Make sure you are using a unique string for each list row
            // String[0] is just an example
            String[] stringArray = objects.get(i);
            String uniqueString = stringArray[0]; 

            mIdMap.put(uniqueString, i);
        }

        mRowLayout = rowLayout;
        mStrings = objects;
    }

    ...

    // Populate your layout text views with data from your string array
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        convertView = LayoutInflater.from(getContext()).inflate(mRowLayout, null);

        TextView tvAddress = (TextView)convertView.findViewById(R.id.item_adressView);
        TextView tvZipCode = (TextView)convertView.findViewById(R.id.item_zipcodePlaceView);
        TextView tvCompany = (TextView)convertView.findViewById(R.id.item_companyView);
        TextView tvDriveType = (TextView)convertView.findViewById(R.id.item_driveTypeView);

        tvAddress.setText(mStrings.get(position)[0]);
        tvZipCode.setText(mStrings.get(position)[1]);
        tvCompany.setText(mStrings.get(position)[2]);
        tvDriveType.setText(mStrings.get(position)[3]);

        return convertView;
    }
}

在您的 Activity 中,只需使用布局 xml 文件和字符串数组加载适配器,如下所示:

StableArrayAdapter adapter = new StableArrayAdapter(this, R.layout.list_row, mStringsList);

我还没有测试代码,但它应该可以工作。我最近使用了类似的东西。此外,您还可以在 Internet 上找到一些关于如何制作自定义适配器的教程。这是一个很好的检查:Using an ArrayAdapter with ListView

关于java - ListViewDragginanimation 中的自定义 ListView 项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30123686/

相关文章:

java - 我不能输入 Go 键

Android:BaseAdapter 不显示元素

c# - 如何使 ListView 中的项目具有不同的颜色?

java - 按代表时间的 2 列对 JTable 进行排序

Java 返回 JSONArray

java - 带有 comet 处理器的此 URL 不支持 HTTP 方法 GET

java - 在 JPanel 上绘制多个 JComponent 不起作用

Android 限制本地化

android - 在另一个 Activity 中检索共享首选项

wpf - 如何在 ListView 项中列出 UIElement?