重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Android中RecyclerView拖拽、侧删功能的实现代码-创新互联

废话不多说,下面展示一下效果。

在漳县等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供做网站、成都网站制作 网站设计制作定制网站制作,公司网站建设,企业网站建设,成都品牌网站建设,网络营销推广,成都外贸网站建设,漳县网站建设费用合理。

这是GridView主文件实现。

public class GridViewActivity extends AppCompatActivity {
  RecyclerView mRecyclerView;
  List mStringList;
  RecyclerAdapter mRecyAdapter;
  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recyclerview);
    initView();
    initRecy();
  }
  private void initView() {
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    mRecyclerView = (RecyclerView) findViewById(R.id.view_recycler);
  }
  private void initRecy() {
    if (mStringList == null) {
      mStringList = new ArrayList<>();
    }
    mStringList.addAll(DataManager.getData(20 - mStringList.size()));
    mRecyAdapter = new RecyclerAdapter(R.layout.item_gridview, mStringList, true);
    mRecyclerView.setLayoutManager(new GridLayoutManager(this, 4));
    mRecyclerView.addItemDecoration(new DividerGriItemDecoration(this));
    mRecyclerView.setHasFixedSize(true);
    RecyItemTouchHelperCallback itemTouchHelperCallback = new RecyItemTouchHelperCallback(mRecyAdapter, false, true);
    final ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemTouchHelperCallback);
    itemTouchHelper.attachToRecyclerView(mRecyclerView);
    mRecyclerView.addOnItemTouchListener(new OnRecyclerItemClickListener(mRecyclerView) {
      @Override
      public void onItemClick(RecyclerView.ViewHolder viewHolder) {
        RecyclerAdapter.ViewHolder viewHolder1 = (RecyclerAdapter.ViewHolder) viewHolder;
        String tvString = viewHolder1.mTextView.getText().toString();
        Toast.makeText(GridViewActivity.this, "碰了一下 " + tvString, Toast.LENGTH_SHORT).show();
      }
      @Override
      public void onLongClick(RecyclerView.ViewHolder viewHolder) {
        RecyclerAdapter.ViewHolder viewHolder1 = (RecyclerAdapter.ViewHolder) viewHolder;
        String tvString = viewHolder1.mTextView.getText().toString();
        Toast.makeText(GridViewActivity.this, "长按不放可以拖动!", Toast.LENGTH_SHORT).show();
        if (viewHolder.getLayoutPosition() != 0) {
          itemTouchHelper.startDrag(viewHolder);
        }
      }
    });
    mRecyclerView.setAdapter(mRecyAdapter);
  }
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
      finish();
    }
    return super.onOptionsItemSelected(item);
  }
}

文章标题:Android中RecyclerView拖拽、侧删功能的实现代码-创新互联
文章地址:http://cqcxhl.com/article/joose.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP