recyclerview 是个不错的控件,本demo 支持下拉刷新,加载更多,瀑布流。
项目结构:
基本效果:
部分代码:
头部
public class SampleHeader extends RelativeLayout {
public SampleHeader(Context context) {
super(context);
init(context);
}
public SampleHeader(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public SampleHeader(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
public void init(Context context) {
inflate(context, R.layout.sample_header, this);
}
}
类似listview下拉刷新,加载更多
public class RecyclerViewActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
@Bind(R.id.swipe_container)
SwipeRefreshLayout swipeRefreshLayout;
private RecyclerView mRecyclerView = null;
private DataAdapter mDataAdapter = null;
private HeaderAndFooterRecyclerViewAdapter mHeaderAndFooterRecyclerViewAdapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_activity);
ButterKnife.bind(this);
mRecyclerView = (RecyclerView) findViewById(R.id.list);
//init data
loadData();
swipeRefreshLayout.setOnRefreshListener(this);
swipeRefreshLayout.post(new Runnable() {
@Override
public void run() {
swipeRefreshLayout.setRefreshing(true);
// loadData();
swipeRefreshLayout.setRefreshing(false);
}
});
mHeaderAndFooterRecyclerViewAdapter = new HeaderAndFooterRecyclerViewAdapter(mDataAdapter);
mRecyclerView.setAdapter(mHeaderAndFooterRecyclerViewAdapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//add a HeaderView
RecyclerViewUtils.setHeaderView(mRecyclerView, new SampleHeader(this));
//add a FooterView
RecyclerViewUtils.setFooterView(mRecyclerView, new
SampleFooter(this));
}
/**
* 初始化数据
*/
private void loadData(){
ArrayList<String> dataList = new ArrayList<>();
for (int i = 0; i < 15; i++) {
dataList.add("item" + i);
}
swipeRefreshLayout.setRefreshing(false);
mDataAdapter = new DataAdapter(this);
mDataAdapter.setData(dataList);
}
/**
*
*/
@Override
public void onRefresh() {
loadData();
}
private class DataAdapter extends RecyclerView.Adapter {
private LayoutInflater mLayoutInflater;
private ArrayList<String> mDataList = new ArrayList<>();
public DataAdapter(Context context) {
mLayoutInflater = LayoutInflater.from(context);
}
public void setData(ArrayList<String> list) {
this.mDataList = list;
notifyDataSetChanged();
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(mLayoutInflater.inflate(R.layout.sample_item_text, parent, false));
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
String item = mDataList.get(position);
ViewHolder viewHolder = (ViewHolder) holder;
viewHolder.textView.setText(item);
}
@Override
public int getItemCount() {
return mDataList.size();
}
private class ViewHolder extends RecyclerView.ViewHolder {
private TextView textView;
public ViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.info_text);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = mDataList.get(RecyclerViewUtils.getAdapterPosition(mRecyclerView, ViewHolder.this));
Toast.makeText(RecyclerViewActivity.this, text, Toast.LENGTH_SHORT).show();
}
});
}
}
}
}
源码下载地址,感兴趣的就下载看看
http://pan.baidu.com/s/1pJZFwIf
提取密码: ir8b
联系客服