在ListViewDragginganimation自定义列表视图项 [英] Custom listview item in ListViewDragginganimation
问题描述
所以,我有一个Android项目中,我有一个排序功能很好用。我一直在使用的DevBytes
ListViewDraggingAnimation
的 https://www.youtube.com/watch?v=_BZIvjMgH-Q
So I have an android project where I have good use for a sorting functionality. I've been using DevBytes
ListViewDraggingAnimation
https://www.youtube.com/watch?v=_BZIvjMgH-Q
我得到它在我的应用程序和布局工作,但一个大问题仍然存在,我不能让它与我自定义的的ListView
的项目工作。该股项的DevBytes
code是一个普通的的TextView
这样:
I have gotten it to work in my app and layout, but one big issue still remains, I cannot get it to work with my custom ListView
items. The stock item in DevBytes
code is a plain TextView
as such:
<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
里面装的。因此:
My custom ListView
items consist of a RelativeLayout
with four TextView
s inside. As such:
<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>
诚然,我不理解数据结构良好。我在改变它接受我的项目我们尽最大努力崩溃的程序。我担心我已经改变了code比需要一些尝试的更多部分。可能有人帮我指出了code我有以获得的ListView
来接受我的自定义项目变更的哪些部分?
Admittedly, I don't understand the data structure well. Every attempt I've made at changing it to accept my items has crashed the program. I fear I have changed more parts of the code than needed in some attempts. Could someone help me point out what parts of the code I have to change in order to get the ListView
to accept my custom items?
发布所有的Java code将使我的职务超出字符数限制。
Posting all java code will make my post exceed the character limit.
在code可以在这里找到:
的http://developer.android.com/shareables/devbytes/ListViewDraggingAnimation.zip
The code can be found here: http://developer.android.com/shareables/devbytes/ListViewDraggingAnimation.zip
推荐答案
您需要更改适配器,如果你想将数据绑定到多个视图(除了只是一个简单的TextView)。阅读 ArrayAdapter参考以了解这些适配器的作品。
You need to change the adapter if you want to bind data to multiple views (besides just a simple TextView). Read the ArrayAdapter reference to understand how these adapter works.
基本上,你的构造需要拿在复杂的数据结构,那么你需要重写getView()将数据绑定到多个视图。使用的DevBytes例如,尝试这样的:
Basically, your constructor needs to take in your complex data structure, then you need to override getView() to bind data to multiple views. Using the DevBytes example, try something like this:
// 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;
}
}
在您的活动,只需加载适配器布局的xml文件和字符串数组,像这样的:
In your activity, just load the adapter with your layout xml file and string arrays, like this:
StableArrayAdapter adapter = new StableArrayAdapter(this, R.layout.list_row, mStringsList);
我没有测试code,但它应该工作。我用类似的东西最近。此外,还可以在网上找如何自定义适配器上几个教程。这里有一个很好的来看看:使用与ArrayAdapter ListView控件
这篇关于在ListViewDragginganimation自定义列表视图项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!