我想开发一个列表视图,当从左到右滑动时-在左角显示一个接受(真)图标(不可单击-从左到右滑动时仅显示一个颜色更改),如以下屏幕截图:
当我从左向右滑动时,它将接受(调用“accept”api)。
当我从右向左滑动时,它将显示如下内容:
这是在ios中完成的,但我找不到如何在android中完成,我尝试了google,但找不到我想要的东西。
我尝试了以下示例:http://www.tutecentral.com/android-swipe-listview/
但是在这个例子中,当我从左到右和从右到左滑动时,会调用同一个onopened(..)方法,所以很难知道何时调用accept和何时调用reject api,因为在任何类型的滑动上都会调用同一个方法。
我还希望只在我左右或左右滑动时显示接受(左侧)和拒绝(右侧)图像,但当我抬起手指时,它们必须消失,并且应显示整个列表视图(永远不要同时显示两侧图像)。
所以请任何人帮我理解怎么做。
我的问题有点混乱,但我不知道如何解释整个动画,所以我试图像上面这样解释。
如果有人能帮助我,我将不胜感激。
最佳答案
解决方案1:
你要想接近你的功能,你必须做如下的事情,
包装ListView的适配器
如下所示:
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);
}
为每个滑动方向创建背景布局
如下所示:
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);
}
这里有库和示例:
https://github.com/wdullaer/SwipeActionAdapter
这并不是你想要的那样,但是我希望这能帮助你实现你的功能。
解决方案2:
解决方案2是更改已用代码中的某些代码:http://www.tutecentral.com/android-swipe-listview/
我从linkhttp://www.tutecentral.com/android-swipe-listview/中尝试了您的代码,并更改了一些代码,成功地解决了您关于onopened(..)方法的问题(您的困惑)(从左到右调用,反之亦然)
第一次更改:
custom_row.xml布局文件有三个按钮:从1到3,
去掉中间部分。
复制粘贴以下代码以保留两个按钮:
<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" />
此布局的代码保持不变。
第二次更改:
所以现在您的接受和拒绝列表代码已经准备好了,现在讨论onopened(..)方法。
混淆解决方案1-你说过,你的onopend(…)方法调用了相同的从左到右和从左到右的顺序
->只需更改以下内容:
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);
}
}
混淆解决方案2当我拿起手指时,它必须显示整个listview或者listview的边应该保留它的角,这样它就不工作了
->我已经在上面回答过了
称为swipelistview.closeanimate(position);否则,当您左右滑动时,它会隐藏左接受和右拒绝图像。
因此,最终代码如下:
整个cutom_row.xml布局
<?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>
整个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;
}
}
其他代码和库保持不变,它对您和其他人也有帮助,所以请享受。