gpt4 book ai didi

Android 新收件箱应用样式 ListView ,左右滑动

转载 作者:IT老高 更新时间:2023-10-28 23:11:41 26 4
gpt4 key购买 nike

我试图构建 android 新的收件箱样式 ListView ,如图所示左右滑动,我尝试了 47deg swipelistview但它不是那么稳定,还有其他可用的库吗?!

right swipe left swipe

目前已尝试 47 度

 public class MainActivity extends Activity {

Listview pullToRefreshListView;
SwipeListView swipelistview;
ItemAdapter adapter;
List<ItemRow> itemData;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

pullToRefreshListView = (ListView) findViewById(R.id.example_swipe_lv_list);
swipelistview = pullToRefreshListView.getRefreshableView();
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) {
adapter.remove(position);
Toast.makeText(MainActivity.this, "Open to dismiss",
Toast.LENGTH_SHORT).show();
} // swipelistview.dismiss(position);
else {
Toast.makeText(MainActivity.this, "Open to edit",
Toast.LENGTH_SHORT).show();
}
}

@Override
public void onClosed(int position, boolean fromRight) {
}

@Override
public void onListChanged() {
}

@Override
public void onMove(int position, float x) {
}

@Override
public void onStartOpen(int position, int action, boolean right) {
if (right) {
// adapter.onRight();
swipelistview.getChildAt(position).findViewById(R.id.back)
.setBackgroundColor(Color.GREEN);

swipelistview.getChildAt(position)
.findViewById(R.id.imageViewLeft)
.setVisibility(View.VISIBLE);
swipelistview.getChildAt(position)
.findViewById(R.id.imageViewRight)
.setVisibility(View.GONE);
} else {
// adapter.onLeft();
swipelistview.getChildAt(position).findViewById(R.id.back)
.setBackgroundColor(Color.RED);
swipelistview.getChildAt(position)
.findViewById(R.id.imageViewLeft)
.setVisibility(View.GONE);
swipelistview.getChildAt(position)
.findViewById(R.id.imageViewRight)
.setVisibility(View.VISIBLE);
}
}

@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));

// swipelistview.openAnimate(position); //when you touch front
// view it will open

}

@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.setSwipeActionRight(SwipeListView.SWIPE_ACTION_REVEAL); // there
// are
// four
// swipe
// actions
swipelistview.setSwipeActionLeft(SwipeListView.SWIPE_ACTION_REVEAL);
swipelistview.setOffsetRight(convertDpToPixel(0f)); // left side
// offset
swipelistview.setOffsetLeft(convertDpToPixel(0f)); // right side
// offset
swipelistview.setAnimationTime(60); // Animation time
swipelistview.setSwipeOpenOnLongPress(false); // enable or disable
// SwipeOpenOnLongPress
swipelistview.setSwipeCloseAllItemsWhenMoveList(true);
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;
}
}

适配器类

public class ItemAdapter extends ArrayAdapter<ItemRow> {

List<ItemRow> data;
Context context;
int layoutResID;

public ItemAdapter(Context context, int layoutResourceId, List<ItemRow> data) {
super(context, layoutResourceId, data);

this.data = data;
this.context = context;
this.layoutResID = layoutResourceId;

// TODO Auto-generated constructor stub
}

NewsHolder holder = null;
View row = null;

@Override
public View getView(int position, View convertView, ViewGroup parent) {

row = convertView;
holder = null;

if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResID, parent, false);

holder = new NewsHolder();

holder.itemName = (TextView) row
.findViewById(R.id.example_itemname);
holder.icon = (ImageView) row.findViewById(R.id.example_image);
holder.imageViewRight = (ImageView) row
.findViewById(R.id.imageViewRight);
holder.imageViewLeft = (ImageView) row
.findViewById(R.id.imageViewLeft);

row.setTag(holder);
} else {
holder = (NewsHolder) row.getTag();
}

ItemRow itemdata = data.get(position);
holder.itemName.setText(itemdata.getItemName());
holder.icon.setImageDrawable(itemdata.getIcon());

return row;

}
public void remove(int pos){
data.remove(pos);

}

public void onLeft() {

holder.imageViewLeft.setVisibility(View.VISIBLE);
holder.imageViewRight.setVisibility(View.GONE);
}

public void onRight() {
holder.imageViewRight.setVisibility(View.VISIBLE);
holder.imageViewLeft.setVisibility(View.GONE);
}

static class NewsHolder {

TextView itemName;
ImageView icon;
ImageView imageViewLeft, imageViewRight;

RelativeLayout mRelativeLayout;
}

最佳答案

您可以简单地支持对列表项 onTouch 的“滑动”手势,而不是使用自定义 ListView,如下所示:

private static final int DEFAULT_THRESHOLD = 128;

row.setOnTouchListener(new View.OnTouchListener() {

int initialX = 0;
final float slop = ViewConfiguration.get(context).getScaledTouchSlop();

public boolean onTouch(final View view, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
initialX = (int) event.getX();
view.setPadding(0, 0, 0, 0);
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
int currentX = (int) event.getX();
int offset = currentX - initialX;
if (Math.abs(offset) > slop) {
view.setPadding(offset, 0, 0, 0);

if (offset > DEFAULT_THRESHOLD) {
// TODO :: Do Right to Left action! And do nothing on action_up.
} else if (offset < -DEFAULT_THRESHOLD) {
// TODO :: Do Left to Right action! And do nothing on action_up.
}
}
} else if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {
// Animate back if no action was performed.
ValueAnimator animator = ValueAnimator.ofInt(view.getPaddingLeft(), 0);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
view.setPadding((Integer) valueAnimator.getAnimatedValue(), 0, 0, 0);
}
});
animator.setDuration(150);
animator.start();
}
};

如果没有执行任何操作,我也会使用反向动画。

此解决方案是轻量级的,因此您不会遇到任何滞后。

关于Android 新收件箱应用样式 ListView ,左右滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26771651/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com