Android中RecyclerView上拉下拉,分割线,多条目的实例代码
短信预约 -IT技能 免费直播动态提醒
//activity的xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="baway.com.cj.myrecyclerview2.MainActivity">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_above="@+id/progress"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>
<ProgressBar
android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:visibility="gone"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
item1的xml和item2的xml就一个背景颜色不同
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f00000"
tools:context="baway.com.cj.myrecyclerview2.MainActivity">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="20sp"
android:gravity="center"
android:textColor="#00ff00"/>
</RelativeLayout>
item2
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f000ff"
tools:context="baway.com.cj.myrecyclerview2.MainActivity">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="20sp"
android:gravity="center"
android:textColor="#00ff00"/>
</RelativeLayout>
//activity,有上啦下拉,添加分割线,
package baway.com.cj.myrecyclerview2;
import android.graphics.Color;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private MainAdapter mainAdapter;
private SwipeRefreshLayout swipeRefreshLayout;
private LinearLayoutManager linearLayoutManager;
private ProgressBar progress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initView() {
recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
progress = (ProgressBar)findViewById(R.id.progress);
linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
mainAdapter = new MainAdapter(this);
mainAdapter.setOnClickListener(new MyItemOnClickListener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(MainActivity.this, " "+position, Toast.LENGTH_SHORT).show();
}
});
recyclerView.setAdapter(mainAdapter);
recyclerView.addItemDecoration(new MyItemDecoration());
recyclerView.setItemAnimator(new DefaultItemAnimator());
swipeRefreshLayout.setColorSchemeColors(Color.RED,Color.GREEN,Color.BLUE);
swipeRefreshLayout.setProgressViewOffset(true,0,200);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
swipeRefreshLayout.setRefreshing(false);
}
},3000);
}
});
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int lastVisibleItemPosition = linearLayoutManager.findLastVisibleItemPosition();
int itemCount = linearLayoutManager.getItemCount();
if (lastVisibleItemPosition + 1 >= itemCount && dy > 0) {
progress.setVisibility(View.VISIBLE);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
progress.setVisibility(View.GONE);
}
},3000);
}
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
});
}
private void initData() {
ArrayList<String> strings = new ArrayList<>();
for (int i = 0; i < 50; i++) {
strings.add("name "+i);
}
mainAdapter.setData(strings);
mainAdapter.notifyDataSetChanged();
}
}
//adpter有多条目展示
package baway.com.cj.myrecyclerview2;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
public class MainAdapter extends RecyclerView.Adapter {
private Context context;
private ArrayList<String> datas = new ArrayList<>();
private MyItemOnClickListener myItemOnClickListener;
private final int TYPE_0 = 0;
private final int TYPE_1 = 1;
public MainAdapter(Context context) {
this.context = context;
}
@Override
public int getItemViewType(int position) {
if (position %3 == 0) {
return TYPE_0;
} else {
return TYPE_1;
}
}
public void setOnClickListener(MyItemOnClickListener onClickListener) {
this.myItemOnClickListener = onClickListener;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_0) {
View inflate = LayoutInflater.from(context).inflate(R.layout.item_main, null);
return new MyViewHolder(inflate);
} else {
View inflate = LayoutInflater.from(context).inflate(R.layout.item_main2, null);
return new MyViewHolder2(inflate);
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
if (getItemViewType(position) == TYPE_0) {
MyViewHolder myViewHolder = (MyViewHolder)holder;
myViewHolder.name.setText(datas.get(position));
myViewHolder.name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myItemOnClickListener.onItemClick(v,position);
}
});
} else {
MyViewHolder2 myViewHolder = (MyViewHolder2)holder;
myViewHolder.name.setText(datas.get(position));
myViewHolder.name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myItemOnClickListener.onItemClick(v,position);
}
});
}
}
@Override
public int getItemCount() {
return datas.size();
}
public void setData(ArrayList<String> data) {
if (data == null) {
return;
}
this.datas.addAll(data);
}
public class MyViewHolder extends RecyclerView.ViewHolder{
public TextView name;
public MyViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name);
}
}
public class MyViewHolder2 extends RecyclerView.ViewHolder{
public TextView name;
public MyViewHolder2(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name);
}
}
}
//自定义分割线继承系统的 RecyclerView.ItemDecoration
package baway.com.cj.myrecyclerview2;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
public class MyItemDecoration extends RecyclerView.ItemDecoration {
private Paint paint;
public MyItemDecoration() {
paint = new Paint();
paint.setColor(Color.BLUE);
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDraw(c, parent, state);
//c.drawCircle(300,300,300,paint);
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View childAt = parent.getChildAt(i);
c.drawRect(childAt.getLeft(),childAt.getBottom(),childAt.getWidth()+childAt.getLeft(),childAt.getBottom()+5,paint);
}
}
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDrawOver(c, parent, state);
// int childCount = parent.getChildCount();
// for (int i = 0; i < childCount; i++) {
// View childAt = parent.getChildAt(i);
// c.drawRect(childAt.getLeft(),childAt.getTop(),childAt.getWidth()+childAt.getLeft(),childAt.getBottom()+5,paint);
// }
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
outRect.set(20,0,20,5);
}
}
以上所述是小编给大家介绍的Android中RecyclerView上拉下拉,分割线,多条目的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程网网站的支持!
您可能感兴趣的文章:Android RecyclerView实现点击条目删除Android自定义SwipeLayout仿QQ侧滑条目Android仿京东分类模块左侧分类条目效果Android更多条目收缩展开控件ExpandView的示例代码Android ListView自动生成列表条目的实例Android XRecyclerView实现多条目加载Android条目拖拽删除功能实例代码Android ListView 条目多样式展示实例详解android RecyclerView实现条目Item拖拽排序与滑动删除Android中listview和imageview实现条目单选效果Android编程实现canvas绘制饼状统计图功能示例【自动适应条目数量与大小】Android 中 SwipeLayout一个展示条目底层菜单的侧滑控件源码解析详解Android中实现ListView左右滑动删除条目的方法Android实现下拉展示条目效果
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341