滑动时更改列表视图的行背景

时间:2014-05-11 13:52:16

标签: android listview swipe

我正在使用47deg库来实现listview的滑动手势:https://github.com/47deg/android-swipelistview

我想在刷卡后改变每一行的背景,我有两个问题。

第一个问题是当我滑动例如第一行时,它的背景颜色完全改变但在滚动列表后我看到其他一些行的背景发生了变化。我搜索了很多,发现这个问题与重新使用listview中的视图有关但我无法弄清楚我应该如何解决我的问题。

第二个问题是,在我轻扫后,列表行的前半部分才会生效。实际上,当我滑动后半行时,我得到错误并强制关闭。也许这个问题与第一个有关,我不确定。

这是我的Adapter类:

public class WordsListAdapter extends BaseAdapter {

private List<WordsItem> data;
private Context context;

public WordsListAdapter(Context context, List<WordsItem> data){
    this.data = data;
    this.context = context;
}

@Override
public int getCount() {
    return data.size();
}

@Override
public WordsItem getItem(int position) {
    return data.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final WordsItem item = getItem(position);
    ViewHolder holder;
    if (convertView == null) {
        LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = li.inflate(R.layout.custom_row, parent, false);
        holder = new ViewHolder();
        holder.itemName = (TextView) convertView.findViewById(R.id.example_itemname);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    ((SwipeListView)parent).recycle(convertView, position);

    holder.itemName.setText(item.getItemName());

    return convertView;
}

private static class ViewHolder{
    public TextView itemName;
}

}

这是包含swipelistview的活动:

public class WordsActivity extends Activity {

private WordsListAdapter adapter;
private List<WordsItem> data;
private int i;

private SwipeListView swipeListView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_words);

    data = new ArrayList<WordsItem>();

    adapter = new WordsListAdapter(this, data);

    swipeListView = (SwipeListView) findViewById(R.id.example_swipe_lv_list);

    swipeListView.setSwipeListViewListener(new BaseSwipeListViewListener() {
      @Override
      public void onOpened(int position, boolean toRight) {
        swipeListView.closeAnimate(position);
      }

      @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) {
                swipeListView.getChildAt(position).findViewById(R.id.front).setBackgroundColor(Color.GREEN);
              } else {
                swipeListView.getChildAt(position).findViewById(R.id.front).setBackgroundColor(Color.RED);
              }
          }
      }

      @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));
          Toast.makeText(WordsActivity.this, "Clicked",Toast.LENGTH_SHORT).show();

      }

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

      }

  });

    swipeListView.setAdapter(adapter);

    reload();

    i = 0;

    for(String word : itemData){
        data.add(new WordsItem("item " + i));
        i++;
    }

    adapter.notifyDataSetChanged();

}

private void reload(){
    swipeListView.setSwipeMode(SwipeListView.SWIPE_MODE_BOTH); // there are five swiping modes
    swipeListView.setSwipeActionLeft(SwipeListView.SWIPE_ACTION_REVEAL); //there are four swipe actions 
    swipeListView.setSwipeActionRight(SwipeListView.SWIPE_ACTION_REVEAL);
    swipeListView.setOffsetLeft(convertDpToPixel(300f)); // left side offset
    swipeListView.setOffsetRight(convertDpToPixel(300f)); // right side offset
    swipeListView.setAnimationTime(100); // Animation time
    swipeListView.setSwipeOpenOnLongPress(true);
}

public int convertDpToPixel(float dp) {
    DisplayMetrics metrics = getResources().getDisplayMetrics();
    float px = dp * (metrics.densityDpi / 160f);
    return (int) px;
}

}

1 个答案:

答案 0 :(得分:0)

您可以做的是在callback中创建activity并在适配器中实施interface,这样您就可以通过发送已知的positions来了解在您的callbackonStartOpen()中通过onOpen()的位置,然后一旦您的适配器识别出这些位置,getView()就可以了,并且您可以只为这些位置设置更改背景的逻辑。

PS - 每当您尝试修改listview's个人观点时,由于回收,您将无法获得您所瞄准的结果,因此将所有与观看相关的任务委派给您的listview's adapter