Я пытаюсь создать ListView с помощью настраиваемого адаптера и добавить функцию onClickListener и перетаскивание для сортировки списка.Android Drag & Drop в ListView с пользовательским адаптером
Я искал множество примеров и подобных проблем, но я не мог заставить его работать, поэтому я думаю, что моя проблема может быть конкретной, потому что я использую весь макет как элемент списка, но я не уверен. Если я могу предоставить любую дополнительную информацию, пожалуйста, скажите мне.
Вы можете найти whole project here
Реализация моего ListActivity является:
package com.jimdo.welcomeghosts.cipherrsa;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class TabKeyActivity extends ListActivity {
//Defining an adapter which will handle the data of the listview
KeyAdapter adapter;
private ListView list;
//dummy data field TODO fill this shit up by using ArrayList
Key key_data[] = new Key[]
{
new Key(1, R.drawable.iconpair1, "This"),
new Key(2, R.drawable.iconpair2, "This'"),
new Key(3, R.drawable.iconpair1, "That"),
new Key(4, R.drawable.iconpair2, "That'")
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.keymanager);
//Setting an ADD button as last item of the ListView
list = (ListView) findViewById(android.R.id.list);
list.addFooterView(getLayoutInflater().inflate(R.layout.footerview,null));
//Setting adapter for adding Items to ListView
adapter = new KeyAdapter(this, R.layout.key, key_data);
setListAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = ((TextView)view).getText().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});
}
Это мой Key.xml (строка элемента в списке)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"><RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/checkBox"
android:layout_toStartOf="@+id/checkBox"
android:id="@+id/keyRow">
<ImageView
android:id="@+id/imgIcon"
android:layout_width="50px"
android:layout_height="50px"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignWithParentIfMissing="false"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:longClickable="true"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"/>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:layout_toRightOf="@+id/imgIcon"
android:paddingLeft="5dp">
<TextView
android:id="@+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Testing the shit."
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:textSize="@dimen/abc_text_size_headline_material" />
</FrameLayout>
</RelativeLayout>
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="false"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:layout_margin="5dp" />
</RelativeLayout>
Да, я прочитал эту тему, но я все еще не мог ее использовать, так как я не знаю, как использовать ее с помощью специального адаптера. – Deckweiss