Skip to content

Commit

Permalink
删除,添加方法
Browse files Browse the repository at this point in the history
  • Loading branch information
Dev-Wiki committed May 18, 2016
1 parent c31e82c commit 2d6e8be
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ public void onBindViewHolder(final SingleHolder holder, int position) {
holder.nameView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((SingleItemClickListener) listener).onNameClick(getData(holder).getName());
((SingleItemClickListener) listener).onNameClick(getItem(holder).getName());
}
});

holder.ageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((SingleItemClickListener) listener).onAgeClick(getData(holder).getAge());
((SingleItemClickListener) listener).onAgeClick(getItem(holder).getAge());
}
});
}
Expand Down
53 changes: 44 additions & 9 deletions recycler/src/main/java/net/devwiki/recycler/BaseAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public BaseAdapter(@NonNull List<M> list) {

/**
* 设置一个Item点击回调接口,数据后续调用{@link BaseAdapter#fillList(List)}填充
*
* @param listener 回调接口
*/
public BaseAdapter(OnItemClickListener<H> listener) {
Expand All @@ -38,6 +39,7 @@ public BaseAdapter(OnItemClickListener<H> listener) {

/**
* 设置数据,并设置点击回调接口
*
* @param list
* @param listener
*/
Expand All @@ -64,49 +66,82 @@ public int getItemCount() {
return dataList.size();
}

public M getData(H holder) {
return dataList.get(holder.getLayoutPosition());
}

public void fillList(List<M> list) {
dataList.clear();
dataList.addAll(list);
}

/**
* 更新数据
* @param holder
* @param data
*
* @param holder item对应的holder
* @param data item的数据
*/
public void updateItem(H holder, M data) {
dataList.set(holder.getLayoutPosition(), data);
}

/**
* 获取一条数据
*
* @param holder item对应的holder
* @return 该item对应的数据
*/
public M getItem(H holder) {
return dataList.get(holder.getLayoutPosition());
}

/**
* 获取一条数据
*
* @param position item的位置
* @return item对应的数据
*/
public M getItem(int position) {
return dataList.get(position);
}

/**
* 追加一条数据
* @param data
*
* @param data 追击的数据
*/
public void appendItem(M data) {
dataList.add(data);
}

/**
* 追加一个集合数据
* @param list
*
* @param list 要追击的数据集合
*/
public void appendList(List<M> list) {
dataList.addAll(list);
}

public interface OnItemClickListener<H extends BaseHolder>{
/**
* 在最顶部前置数据
*
* @param data 要前置的数据
*/
public void preposeItem(M data) {
dataList.add(0, data);
}

/**
* 在顶部前置数据集合
*
* @param list 要前置的数据集合
*/
public void preposeList(List<M> list) {
dataList.addAll(0, list);
}

/**
*
* @param <H>
*/
public interface OnItemClickListener<H extends BaseHolder> {
void onItemClick(H holder);
}
}

0 comments on commit 2d6e8be

Please sign in to comment.