【问题标题】:How to use SwipeActionAdapter and display app - [ANDROID]如何使用 SwipeActionAdapter 和显示应用程序 - [ANDROID]
【发布时间】:2015-01-27 17:16:57
【问题描述】:

我从here找到了一个代码

好像

public class MainActivity extends ListActivity implements
    SwipeActionAdapter.SwipeActionListener

{ 受保护的 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 替换我之前用来显示应用程序包的应用程序中的列表视图,但无法理解如何使用它来做到这一点..

如果这是一个非常菜鸟的问题,请对我放松一点。我是初学者。

【问题讨论】:

    标签: android list view adapter swipe


    【解决方案1】:

    我认为您误解了Adapter 的目的。 Adapter 是将数据输入您的ListView 的组件。 SwipeActionAdapter 旨在环绕您现有的 Adapter 实现,并提供回调,当有人在您的 ListView 中滑动项目时,您可以使用这些回调执行操作。

    您可能应该阅读更多教程。我可以强烈推荐 Commonsware 的 Busy Coders Android 开发指南。 我相信你会在那里找到你需要的东西。

    【讨论】:

      猜你喜欢
      • 2020-09-01
      • 1970-01-01
      • 2021-08-22
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多