使用SwipeActionAdapter在listview上获取特定的textview id

时间:2015-09-09 08:22:41

标签: android listview android-listview

我正在使用此适配器SwipeActionAdapter(https://github.com/wdullaer/SwipeActionAdapter

我想询问每次刷选所选项目时如何在自定义列表视图中获取textview 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())

有什么想法吗?请帮忙。感谢

2 个答案:

答案 0 :(得分:1)

您正在使用的库不提供此类功能。

但是,您可以尝试创建自己的函数以获取相应的View by adapter position。

您可以在下面的链接中查看如何操作。

android - listview get item view by position

答案 1 :(得分:1)

由于您正在使用ListActivity,因此您可以尝试:

final ListView listView = getListView();
listView.getChildAt(position - listView.getFirstVisiblePosition());