XRecyclerView和萬能baseeAdapter 實現上拉下拉重新整理列表
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);
}
}
}
效果如下:
相關文章
- Flutter 實現下拉重新整理&上拉載入Flutter
- 如何實現上拉載入,下拉重新整理?
- H5下拉重新整理和上拉載入實現原理淺析H5
- Movable-view實現列表的下拉重新整理上拉載入View
- vue 實現上拉載入下拉重新整理(思路賊清晰)Vue
- UITableView:下拉重新整理和上拉載入更多UIView
- 上拉重新整理,下拉載入
- Flutter——下拉重新整理,上拉載入Flutter
- Swift iOS : 上拉重新整理或者下拉重新整理SwiftiOS
- 移動端用下拉重新整理的方式實現上拉載入
- HarmonyOS 下拉重新整理 上拉載入更多 第二種實現方式
- uni-app | 上拉載入和下拉重新整理探索APP
- iOS自定義MJRefresh上拉和下拉重新整理動畫iOS動畫
- flutter - listView 下拉重新整理 上拉載入FlutterView
- 上拉載入下拉重新整理瞭解下
- flutter - RefreshIndicator實現下拉重新整理、上拉載入 | 掘金技術徵文FlutterIndicator
- Flutter listview下拉重新整理 上拉載入更多FlutterView
- RecyclerView下拉重新整理 上拉載入 原理DemoView
- Flutter下拉重新整理,上拉載入更多資料Flutter
- ListView下拉重新整理,上拉自動載入更多View
- React Native——自定義下拉重新整理上拉載入的列表React Native
- 【微信小程式】scroll-view 的上拉載入和下拉重新整理微信小程式View
- 淺談微信小程式中的下拉重新整理和上拉載入微信小程式
- Flutter ListView封裝,下拉重新整理、上拉載入更多FlutterView封裝
- recyclerView的側拉效果。上拉載入。下拉重新整理,點選事件等等View事件
- android開發(3):列表listview的實現 | 下拉重新整理AndroidView
- 入門微信小程式(含實戰) [第九篇] -- 下拉重新整理和上拉載入微信小程式
- 實現移動端上拉載入和下拉重新整理的vue外掛(mescroll.js)VueJS
- 下拉重新整理,上拉載入外掛mescroll原始碼分析原始碼
- 實現RecyclerView下拉重新整理View
- 移動端上拉和下拉重新整理程式碼例項
- 搭建自己的直播平臺,RecycleView下拉重新整理,上拉載入View
- 十分鐘搭建主流框架:下拉/上拉重新整理資料框架
- Flutter 下拉重新整理上拉載入更多Flutter
- AlloyTouch 實現下拉重新整理
- AlloyTouch實現下拉重新整理
- vue移動端下拉重新整理和上拉載入元件,體積小執行快Vue元件
- Flutter 入門與實戰(六):給列表增加下拉重新整理和上滑載入更多功能Flutter