XRecyclerView : https://github.com/jianghejie/XRecyclerView
万能baseAdaper: https://github.com/hongyangAndroid/baseAdapter
添加依赖:
compile 'com.zhy:base-rvadapter:3.0.3' compile 'com.zhy:base-adapter:3.0.3' compile 'com.jcodecraeer:xrecyclerview:1.2.7'activity_recycler1.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<com.jcodecraeer.xrecyclerview.XRecyclerView
android:id="@+id/recyclerview1"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
item_comment.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/shape_white">
<ImageView
android:id="@+id/user_logo"
android:layout_width="40dp"
android:layout_height="40dp"
android:scaleType="centerInside"
android:src="@mipmap/girl" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/user_logo"
android:layout_alignTop="@id/user_logo"
android:layout_toRightOf="@id/user_logo">
<TextView
android:id="@+id/user_name"
style="@style/TextTheme"
android:layout_marginLeft="8dip"
android:text="岁月抚伤" />
<TextView
android:id="@+id/item_public_time"
style="@style/TextTheme"
android:layout_below="@id/user_name"
android:layout_marginLeft="8dip"
android:text="12点"
android:textColor="#888888"
android:textSize="15dp" />
</RelativeLayout>
<TextView
android:id="@+id/item_index_comment"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignBottom="@id/user_logo"
android:layout_alignParentRight="true"
android:layout_alignTop="@id/user_logo"
android:scaleType="centerInside"
android:text="1楼" />
<TextView
android:id="@+id/content_text"
style="@style/TextTheme"
android:layout_below="@id/user_logo"
android:layout_margin="4dp"
android:text="十年之前,我不认识你 你不属于我,我们还是一样,陪在一个陌生人左右,走过渐渐熟悉的街头;十年之后,我们是朋友, 还可以问候,只是那种温柔,再也找不到拥抱的理由,情人最后难免沦为朋友。" />
<TextView
android:id="@+id/item_action_comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@id/content_text"
android:layout_marginBottom="4dip"
android:layout_marginLeft="8dip"
android:layout_marginRight="8dip"
android:layout_marginTop="4dip"
android:drawableLeft="@mipmap/ic_action_edit"
android:gravity="center"
android:text="回复" />
<TextView
android:id="@+id/item_action_love"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/content_text"
android:layout_marginBottom="4dip"
android:layout_marginLeft="8dip"
android:layout_marginRight="8dip"
android:layout_marginTop="4dip"
android:layout_toLeftOf="@id/item_action_comment"
android:drawableLeft="@mipmap/ic_action_love"
android:gravity="center"
android:text="985赞"
android:textColor="#888888" />
</RelativeLayout>
Comment.java:
package com.example.superrecyclerviewtest;
/**
* Created by Keen on 11/14/2016.
*/
public class Comment {
private String userName;
private String contentText;
private String indexComment;
private String loveCount;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getContentText() {
return contentText;
}
public void setContentText(String contentText) {
this.contentText = contentText;
}
public String getIndexComment() {
return indexComment;
}
public void setIndexComment(String indexComment) {
this.indexComment = indexComment;
}
public String getLoveCount() {
return loveCount;
}
public void setLoveCount(String loveCount) {
this.loveCount = loveCount;
}
}
testrecycleradapter.java:
package adapter;
import android.content.Context;
import com.example.superrecyclerviewtest.Comment;
import com.example.superrecyclerviewtest.R;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;
import java.util.List;
/**
* Created by Keen on 11/13/2016.
*/
public class TestRecyclerAdapter extends CommonAdapter<Comment> {
public TestRecyclerAdapter(Context context, int layoutId, List<Comment> datas) {
super(context, layoutId, datas);
}
@Override
protected void convert(ViewHolder holder, Comment s, int position) {
holder.setText(R.id.content_text, s.getContentText());
holder.setText(R.id.item_index_comment, s.getIndexComment());
holder.setText(R.id.item_action_love, s.getLoveCount());
holder.setText(R.id.user_name, s.getUserName());
}
}
MainActivity.java:
package com.example.superrecyclerviewtest;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.zhy.adapter.recyclerview.CommonAdapter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import adapter.DividerItemDecoration;
import adapter.TestRecyclerAdapter;
public class MainActivity extends AppCompatActivity {
private XRecyclerView mRecyclerView;
private TestRecyclerAdapter adapter;
private List<Comment> mDatas;
private int refreshTime = 0;
private int times = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recyclerview1);
mRecyclerView = (XRecyclerView)findViewById(R.id.recyclerview1);
initDatas();
LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
adapter = new TestRecyclerAdapter(MainActivity.this, R.layout.item_coment, mDatas);
mRecyclerView.setAdapter(adapter);
adapter.setOnItemClickListener(new CommonAdapter.OnItemClickListener()
{
@Override
public void onItemClick(View view, RecyclerView.ViewHolder holder, int position)
{
Toast.makeText(MainActivity.this, "pos = " + position, Toast.LENGTH_SHORT).show();
//adapter.notifyItemRemoved(position);
}
@Override
public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position)
{
return false;
}
});
mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
@Override
public void onRefresh() {
//refresh data here
refreshTime ++;
times = 0;
new Handler().postDelayed(new Runnable(){
public void run() {
mDatas.clear();
for(int i = 0; i < 15 ;i++){
Comment comment = new Comment();
comment.setContentText("这是刷新内容的测试文本");
comment.setUserName("刷新测试");
comment.setLoveCount(Integer.toString(i*10) + "赞");
comment.setIndexComment(Integer.toString(i) + "楼");
Log.w("COMMENT", comment.getIndexComment());
mDatas.add(comment);
}
adapter.notifyDataSetChanged();
mRecyclerView.refreshComplete();
}
}, 1000); //refresh data here
}
@Override
public void onLoadMore() {
// load more data here
new Handler().postDelayed(new Runnable(){
public void run() {
int size = mDatas.size();
for(int i = 0; i < 15 ;i++){
Comment comment = new Comment();
comment.setContentText("这是自动加载的内容测试文本");
comment.setUserName("自动加载");
comment.setLoveCount(Integer.toString(size + i + 1) + "赞");
comment.setIndexComment(Integer.toString(size + i + 1) + "楼");
Log.w("COMMENT", comment.getIndexComment());
mDatas.add(comment);
}
mRecyclerView.loadMoreComplete();
adapter.notifyDataSetChanged();
}
}, 1000);
}
});
}
private void initDatas(){
mDatas = new ArrayList<Comment>();
for (int i = 1; i <= 10; i++){
Comment comment = new Comment();
comment.setContentText("十年之前,我不认识你 你不属于我,我们还是一样,陪在一个陌生人左右,走过渐渐熟悉的街头;十年之后,我们是朋友, 还可以问候,只是那种温柔,再也找不到拥抱的理由,情人最后难免沦为朋友。");
comment.setUserName("十年之恋");
comment.setIndexComment(Integer.toString(i) + "楼");
Log.w("COMMENT", comment.getIndexComment());
comment.setLoveCount(Integer.toString(i*10) + "赞");
mDatas.add(comment);
}
}
}
效果如下: