【发布时间】:2015-09-09 08:22:41
【问题描述】:
我正在使用这个适配器 SwipeActionAdapter (https://github.com/wdullaer/SwipeActionAdapter)
我想问一下,每次我滑动选定的项目时,如何在我的自定义列表视图中获取文本视图 ID。
EG:我在每个项目上都有一个 textview,该 textview 的值为 0,然后如果我向左滑动,它将递增到 1,然后如果我向右滑动,它将减去 1。这是我的活动:
FoodList.java
public class Foodlist extends ListActivity implements SwipeActionAdapter.SwipeActionListener {
protected SwipeActionAdapter mAdapter;
Button btnBack, btnNext;
ProgressBar progressBar;
Animation animReverseScale;
int swipeCount = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.foodList);
String[] content = new String[4];
for (int i=0;i<4;i++) {
if (i == 0) {
content[i] = "Burgers";
} else if (i == 1) {
content[i] = "Chips";
} else if (i == 2) {
content[i] = "Soft Drinks";
} else if (i == 3) {
content[i] = "Hotdogs";
}
}
ArrayAdapter<String> stringAdapter = new ArrayAdapter<String>(
this,
R.layout.foods_extra,
R.id.txtName,
new ArrayList<String>(Arrays.asList(content))
);
mAdapter = new SwipeActionAdapter(stringAdapter);
mAdapter.setSwipeActionListener(this)
.setListView(getListView());
setListAdapter(mAdapter);
mAdapter.addBackground(SwipeDirections.DIRECTION_FAR_LEFT,R.layout.row_bg_left_far)
.addBackground(SwipeDirections.DIRECTION_FAR_RIGHT, R.layout.row_bg_right_far);
animReverseScale = AnimationUtils.loadAnimation(this, R.anim.anim_reverse_scale);
btnBack = (Button) findViewById(R.id.btnBack);
btnNext = (Button) findViewById(R.id.btnNext);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.getProgressDrawable().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent("com.reactivate.Z1");
startActivity(myIntent);
overridePendingTransition(0, 0);
finish();
}
});
btnBack.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
btnBack.setAlpha((float) 0.5);
return false;
}
});
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animationReverseScale(v);
Intent myIntent = new Intent("com.reactivate.SpiderGraph");
startActivity(myIntent);
overridePendingTransition(0, 0);
finish();
}
});
}
public void animationReverseScale(View view) {
view.startAnimation(animReverseScale);
}
@Override
protected void onListItemClick(ListView listView, View view, int position, long id){
Toast.makeText(
this,
"Clicked " + mAdapter.getItem(position),
Toast.LENGTH_SHORT
).show();
}
@Override
public boolean hasActions(int position){
return true;
}
@Override
public boolean shouldDismiss(int position, int direction){
return false;
}
@Override
public void onSwipe(int[] positionList, int[] directionList){
for(int i=0;i<positionList.length;i++) {
int direction = directionList[i];
int position = positionList[i];
switch (direction) {
case SwipeDirections.DIRECTION_FAR_LEFT:
//mAdapter.getItemId(getSelectedItemPosition());
Toast.makeText(
this,
"Checked " + mAdapter.getItem(position),
Toast.LENGTH_SHORT
).show();
break;
case SwipeDirections.DIRECTION_FAR_RIGHT:
Toast.makeText(
this,
"Unchecked " + mAdapter.getItem(position),
Toast.LENGTH_SHORT
).show();
break;
mAdapter.notifyDataSetChanged();
}
}
}
food_extra.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="?android:listPreferredItemHeight"
android:background="@drawable/listview_style"
android:padding="8dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@mipmap/ic_launcher"
android:layout_centerVertical="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/txtName"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/imageView"
android:layout_toStartOf="@+id/imageView"
android:layout_marginRight="30dp"
android:layout_marginEnd="30dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:id="@+id/txtNumber"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="40dp"
android:layout_marginEnd="40dp" />
</RelativeLayout>
foodList.xml
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@android:id/list"
android:layout_marginTop="20sp"
android:background="@drawable/listview_style"
android:layout_centerHorizontal="true" />
我已经通过使用获取每个项目的特定索引
mAdapter.getItemId(getSelectedItemPosition())
有什么想法吗?请帮忙。谢谢
【问题讨论】:
标签: android listview android-listview