Android Studio RecyclerView
程序员文章站
2024-03-21 13:36:16
...
RecyclerView三种布局
线性布局
网格布局
瀑布流
代码片段
主类
package com.example.day1.LinerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import com.example.day1.R;
import java.util.ArrayList;
public class Main2Activity extends AppCompatActivity {
RecyclerView recycler;
RVAdapter rvAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
recycler = findViewById(R.id.recycler);
//线性布局
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
// linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
// recycler.setLayoutManager(linearLayoutManager);
//网格布局
GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
// gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
// recycler.setLayoutManager(gridLayoutManager);
//瀑布流
StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
recycler.setLayoutManager(manager);
//垂直排列
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
dividerItemDecoration.setDrawable(getDrawable(R.drawable.recyc_divider));
recycler.addItemDecoration(dividerItemDecoration);
DefaultItemAnimator animator = new DefaultItemAnimator();
animator.setRemoveDuration(200);
animator.setAddDuration(200);
//设置动画
recycler.setItemAnimator(animator);
final ArrayList<String> list = new ArrayList<>();
for (int i = 1; i <= 100; i++) {
list.add(i + "");
}
rvAdapter = new RVAdapter(Main2Activity.this, list, new recycLisnter() {
@Override
public void itemClick(int i) {
list.remove(i);
rvAdapter.notifyItemRemoved(i);
//删除后更新下表
rvAdapter.notifyItemRangeChanged(0, list.size());
}
@Override
public void longClick(int i) {
ArrayList<String> newList = new ArrayList<>();
newList.add("A");
newList.add("B");
newList.add("C");
list.addAll(i, newList);
rvAdapter.notifyDataSetChanged();
}
});
recycler.setAdapter(rvAdapter);
}
}
适配器
package com.example.month3;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.RVHolder> {
Context context;
ArrayList<String> datas;
recycLisnter recycLisnter;
public RVAdapter(Context context, ArrayList<String> datas, com.example.month3.recycLisnter recycLisnter) {
this.context = context;
this.datas = datas;
this.recycLisnter = recycLisnter;
}
@NonNull
@Override
public RVHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.layout_recyc, viewGroup, false);
return new RVHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RVHolder rvHolder, final int i) {
rvHolder.textView.setText(datas.get(i));
int random = (int) (Math.random() * 101 + 100);
ViewGroup.LayoutParams params = rvHolder.textView.getLayoutParams();
params.height = random;
rvHolder.textView.setLayoutParams(params);
rvHolder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
recycLisnter.itemClick(i);
}
});
rvHolder.textView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
recycLisnter.longClick(i);
return true;
}
});
}
@Override
public int getItemCount() {
return datas.size();
}
class RVHolder extends RecyclerView.ViewHolder {
private TextView textView;
public RVHolder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.text);
}
}
}
定义点击长按接口
public interface recycLisnter {
void itemClick(int i);
void longClick(int i);
}
上一篇: 带头节点的不循环单链表相关操作
推荐阅读
-
Android Studio RecyclerView
-
Android开发之RecyclerView-List布局
-
Android Studio-开发少年强国App(附录一)support与androidx版本之间新旧引用的对应
-
Android 自定义View动画效果进阶
-
Android 自定义LinearLayout和Behavior实现嵌套滑动
-
android的自定义弹出窗
-
调用Popup时发生is your activity running 博客分类: 异常处理 Android UI 特效Android 开发Popup
-
Android 带动画的底部弹出视图
-
Android进阶之自定义View(2)高仿钉钉运动步数实现可动的进度圆环(上)
-
Android 沉浸式状态栏完美解决方案