如何使用SwipeActionAdapter并显示应用程序 - [ANDROID]

时间:2015-01-27 17:16:57

标签: android list view adapter swipe

我找到了来自here

的代码

就像

public class MainActivity extends ListActivity implements
    SwipeActionAdapter.SwipeActionListener

{     protected SwipeActionAdapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    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))
    );
    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_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);
}


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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@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 direction == SwipeDirections.DIRECTION_NORMAL_LEFT;
}

@Override
public void onSwipe(int[] positionList, int[] directionList){
    for(int i=0;i<positionList.length;i++) {
        int direction = directionList[i];
        int position = positionList[i];
        String dir = "";

        switch (direction) {
            case SwipeDirections.DIRECTION_FAR_LEFT:
                dir = "Far left";
                break;
            case SwipeDirections.DIRECTION_NORMAL_LEFT:
                dir = "Left";
                break;
            case SwipeDirections.DIRECTION_FAR_RIGHT:
                dir = "Far right";
                break;
            case SwipeDirections.DIRECTION_NORMAL_RIGHT:
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Test Dialog").setMessage("You swiped right").create().show();
                dir = "Right";
                break;
        }
        Toast.makeText(
                this,
                dir + " swipe Action triggered on " + mAdapter.getItem(position),
                Toast.LENGTH_SHORT
        ).show();
        mAdapter.notifyDataSetChanged();
    }
}

}

我想删除字符串部分,并希望显示应用。 我想使用SwipeActionAdapter替换我的应用程序中的listview,我之前使用它来显示应用程序包但无法理解如何使用它...

如果这是一个非常noob-ish的问题,对我来说有点容易。我是个傻瓜。

1 个答案:

答案 0 :(得分:0)

我认为你误解了Adapter的目的。 Adapter是将数据提供给ListView的组件。 SwipeActionAdapter旨在包围您现有的Adapter实施,并提供回调功能,当有人在ListView中滑动某个项目时,您可以使用这些回调来执行操作。

您应该阅读更多教程。我可以极大地推荐Commonsware的Android开发繁忙编码器指南。 我相信你会在那里找到你需要的东西。