Lösung Nummer 1:
Sie mögen folgende zu tun haben "in der Nähe", um Ihre Funktionalität zu erreichen,
Wickeln Sie den Adapter Ihrer Listview
wie folgt vor:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create an Adapter for your content
String[] content = new String[20];
for (int i=0;i<20;i++) content[i] = "Row "+(i+1);
ArrayAdapter<String> stringAdapter = new ArrayAdapter<String>(
this,
R.layout.row_bg,
R.id.text,
new ArrayList<String>(Arrays.asList(content))
);
// Wrap your content in a SwipeActionAdapter
mAdapter = new SwipeActionAdapter(stringAdapter);
// Pass a reference of your ListView to the SwipeActionAdapter
mAdapter.setListView(getListView());
// Set the SwipeActionAdapter as the Adapter for your ListView
setListAdapter(mAdapter);
}
Erstellen Sie ein Hintergrund-Layout für jede Wischrichtung
wie folgend:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create an Adapter for your content
String[] content = new String[20];
for (int i=0;i<20;i++) content[i] = "Row "+(i+1);
ArrayAdapter<String> stringAdapter = new ArrayAdapter<String>(
this,
R.layout.row_bg,
R.id.text,
new ArrayList<String>(Arrays.asList(content))
);
// Wrap your content in a SwipeActionAdapter
mAdapter = new SwipeActionAdapter(stringAdapter);
// Pass a reference of your ListView to the SwipeActionAdapter
mAdapter.setListView(getListView());
// Set the SwipeActionAdapter as the Adapter for your ListView
setListAdapter(mAdapter);
// Set backgrounds for the swipe directions
mAdapter.addBackground(SwipeDirections.DIRECTION_FAR_LEFT,R.layout.row_bg_left_far)
.addBackground(SwipeDirections.DIRECTION_NORMAL_LEFT,R.layout.row_bg_left)
.addBackground(SwipeDirections.DIRECTION_FAR_RIGHT,R.layout.row_bg_right_far)
.addBackground(SwipeDirections.DIRECTION_NORMAL_RIGHT,R.layout.row_bg_right);
}
Sie haben beide Bibliotheken und Beispiel aus der hier: https://github.com/wdullaer/SwipeActionAdapter
Dies wird nicht genau wie Sie wollen, aber ich hoffe, dass dies Ihnen helfen, Ihre Funktionalität zu erfüllen.
Lösung Nummer 2:
Lösung 2 ist etwas Code in der Eingesetzte Code zu ändern: http://www.tutecentral.com/android-swipe-listview/
i Code aus dem Link versuchen hatte http://www.tutecentral.com/android-swipe-listview/ und einige Codes zu ändern und erfolgreich Lösen Sie Ihr Problem (Ihre Verwirrung) über onOpened (..) Methode (genannt von links nach rechts und umgekehrt)
Erste Änderung:
custom_row.xml Layout-Datei hat drei Schaltfläche swipe_button1 bis 3, entfernen Mitte von ihnen.
Copy Paste für folgenden Code bleiben zwei Tasten:
<Button
android:id="@+id/swipe_button1"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@drawable/your_accept_image" />
<Button
android:id="@+id/swipe_button3"
style="@style/MyListButtonAction"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@drawable/your_reject_image" />
bleibt Code gleiche für dieses Layout.
Zweite Änderung:
So, jetzt Ihr akzeptieren und ablehnen und Listencode ist bereit, jetzt über onOpened (..) Methode diskutieren.
Verwirrung Lösung 1 - Sie sagten, Ihre onOpend (...) Methode gleichnamige sowohl von links nach rechts und umge Verse
-> nur folgende Änderungen:
public void onOpened(int position, boolean toRight) {
if(toRight)
{
// for left to right your api calling here
swipelistview.closeAnimate(position);
}
else
{
// for right to left your api calling here
swipelistview.closeAnimate(position);
}
}
Verwechslungslösung 2 wenn ich finger nehmen muss es muss gesamte listeansicht oder listview seiten sollten es bleiben ecken so funktioniert es nicht
-> ich bereits beantwortet es in über
genannt swipelistview.closeAnimate (position); in beiden wenn es sonst links verstecken akzeptieren und rechts ablehnen Bild, wenn Sie Ihren Swipe nach links und rechts über.
So endgültige Code folgt:
Entire cutom_row.xml Layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="@+id/back"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:tag="back" >
<Button
android:id="@+id/swipe_button1"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@drawable/accept_image" />
<Button
android:id="@+id/swipe_button3"
style="@style/MyListButtonAction"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@drawable/reject_image" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/front"
style="@style/MyListFrontContent"
android:orientation="vertical"
android:tag="front" >
<ImageView
android:id="@+id/example_image"
style="@style/MyListImage" />
<TextView
android:id="@+id/example_itemname"
style="@style/MyListTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/example_image" />
</RelativeLayout>
</FrameLayout>
Entire MainActivity.java
public class MainActivity extends Activity {
SwipeListView swipelistview;
ItemAdapter adapter;
List<ItemRow> itemData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipelistview=(SwipeListView)findViewById(R.id.example_swipe_lv_list);
itemData=new ArrayList<ItemRow>();
adapter=new ItemAdapter(this,R.layout.custom_row,itemData);
swipelistview.setSwipeListViewListener(new BaseSwipeListViewListener() {
@Override
public void onOpened(int position, boolean toRight) {
if(toRight)
{
// for left to right your api calling here
swipelistview.closeAnimate(position);
}
else
{
// for right to left your api calling here
swipelistview.closeAnimate(position);
}
}
@Override
public void onClosed(int position, boolean fromRight) {
// close list slide
}
@Override
public void onListChanged() {
}
@Override
public void onMove(int position, float x) {
}
@Override
public void onStartOpen(int position, int action, boolean right) {
Log.d("swipe", String.format("onStartOpen %d - action %d", position, action));
}
@Override
public void onStartClose(int position, boolean right) {
Log.d("swipe", String.format("onStartClose %d", position));
}
@Override
public void onClickFrontView(int position) {
Log.d("swipe", String.format("onClickFrontView %d", position));
}
@Override
public void onClickBackView(int position) {
Log.d("swipe", String.format("onClickBackView %d", position));
swipelistview.closeAnimate(position);//when you touch back view it will close
}
@Override
public void onDismiss(int[] reverseSortedPositions) {
}
});
//These are the swipe listview settings. you can change these
//setting as your requirement
swipelistview.setSwipeMode(SwipeListView.SWIPE_MODE_BOTH); // there are five swiping modes
// swipelistview.setSwipeActionLeft(SwipeListView.SWIPE_ACTION_DISMISS); //there are four swipe actions
swipelistview.setSwipeActionRight(SwipeListView.SWIPE_ACTION_REVEAL);
swipelistview.setOffsetLeft(convertDpToPixel(0f)); // left side offset
swipelistview.setOffsetRight(convertDpToPixel(80f)); // right side offset
swipelistview.setAnimationTime(500); // Animation time
swipelistview.setSwipeOpenOnLongPress(true); // enable or disable SwipeOpenOnLongPress
swipelistview.setAdapter(adapter);
for(int i=0;i<10;i++)
{
itemData.add(new ItemRow("Swipe Item"+ i,getResources().getDrawable(R.drawable.ic_launcher)));
}
adapter.notifyDataSetChanged();
}
public int convertDpToPixel(float dp) {
DisplayMetrics metrics = getResources().getDisplayMetrics();
float px = dp * (metrics.densityDpi/160f);
return (int) px;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Andere Code und Bibliothek bleibt gleich, es für Sie hilfreich und andere auch, also genieße.
bro, hast du es funktioniert..wenn ja bitte teilen Sie die Lösung. Ich habe genau die gleiche Anforderung. – dreamdeveloper
Hellow @dreamdeveloper, Sehen Sie die akzeptierte Lösung, die unten für mich funktioniert. –
haben Sie die in http://www.tutecentral.com/android-swipe-listview/ erwähnte Lösung verwendet? und dann die Modifikation wie in der akzeptierten Lösung beschrieben? – dreamdeveloper