activity中的代码---------------------------
设置条目间距的网址http://blog.csdn.net/csdndouniwan/article/details/51762269
public class ThirdActivity extends Activity implements OnItemClickListner {
private RecyclerView recyclerView;
private ArrayList<String> list;
private MoreItemAdapter moreItemAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
recyclerView = findViewById(R.id.recycler_view);
//数据
list = new ArrayList<>();
for (int i=0; i<1000;i++){
list.add("这是条目"+i);
}
recyclerView.setLayoutManager(new LinearLayoutManager(ThirdActivity.this));
//添加分割线....new DividerItemDecoration(this, DividerItemDecoration.VERTICAL)默认的类似黑色的分割线
//可以使用shape画出一条分割线....然后使用自己的分割线(自己试)
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
dividerItemDecoration.setDrawable(getResources().getDrawable(R.mipmap.ic_launcher));
recyclerView.addItemDecoration(dividerItemDecoration);
//
moreItemAdapter = new MoreItemAdapter(ThirdActivity.this, list);
recyclerView.setAdapter(moreItemAdapter);
//设置条目的点击事件....实际上是对适配器设置点击事件
moreItemAdapter.setOnitemClickListner(this);
}
@Override
public void onItemClick(int position) {
Toast.makeText(ThirdActivity.this,"点击了:"+list.get(position),Toast.LENGTH_SHORT).show();
}
@Override
public void onItemLongClickListner(int position) {
Toast.makeText(ThirdActivity.this,"长按了:"+list.get(position),Toast.LENGTH_SHORT).show();
}
}
适配器中的代码---------------------
/**
* Created by Dash on 2017/11/27.
*
* 做多条目的适配器
*/
public class MoreItemAdapter extends RecyclerView.Adapter{
private Context context;
private ArrayList<String> list;
private final int TEXT_ONLY = 0;
private final int IMAGE_LEFT = 1;
private final int IMAGE_RIGHT = 2;
private OnItemClickListner onItemClickListner;
public MoreItemAdapter(Context context, ArrayList<String> list) {
this.context = context;
this.list = list;
}
//根据当前展示的视图创建ViewHolder
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder holder = null;
View view;
switch (viewType){
case TEXT_ONLY:
view = LayoutInflater.from(context).inflate(R.layout.item_water_layout,parent,false);
holder = new TextOnlyHolder(view);
break;
case IMAGE_LEFT:
view = LayoutInflater.from(context).inflate(R.layout.image_left_layout,parent,false);
holder = new ImageLeftHolder(view);
break;
case IMAGE_RIGHT:
view = LayoutInflater.from(context).inflate(R.layout.image_right_layout,parent,false);
holder = new ImageRightHolder(view);
break;
}
return holder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
//赋值
if (getItemViewType(position) == TEXT_ONLY){
((TextOnlyHolder)holder).textView.setText(list.get(position));
//触发点击事件
((TextOnlyHolder)holder).textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onItemClickListner.onItemClick(position);
//吐司
}
});
((TextOnlyHolder)holder).textView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
onItemClickListner.onItemLongClickListner(position);
return true;//自己处理长按事件,,,事件不再向下传递,,,点击事件不会执行
}
});
}else if (getItemViewType(position) == IMAGE_LEFT){
((ImageLeftHolder)holder).textView.setText(list.get(position));
}else if (getItemViewType(position) == IMAGE_RIGHT){
((ImageRightHolder)holder).textView.setText(list.get(position));
}
}
@Override
public int getItemCount() {
return list.size();
}
/**
* 返回的是当前条目的类型
* @param position
* @return
*/
@Override
public int getItemViewType(int position) {
if (position%3 == 0){
return TEXT_ONLY;
}else if (position%3 ==1){
return IMAGE_LEFT;
}else {
return IMAGE_RIGHT;
}
}
/**
* new OnItemClickListner() {
@Override
public void onItemClick(int position) {
Toast.makeText(ThirdActivity.this,"点击了:"+list.get(position),Toast.LENGTH_SHORT).show();
}
@Override
public void onItemLongClickListner(int position) {
Toast.makeText(ThirdActivity.this,"长按了:"+list.get(position),Toast.LENGTH_SHORT).show();
}
}
* @param onItemClickListner
*/
public void setOnitemClickListner(OnItemClickListner onItemClickListner) {
this.onItemClickListner = onItemClickListner;
//onItemClickListner.onItemClick();
}
}
ViewHolder中的代码-----------------------
public class TextOnlyHolder extends RecyclerView.ViewHolder {
public TextView textView;
public TextOnlyHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.text_view);
}
}