【Android - 控件】之可悬浮列表StickyHeadersRecyclerView
这是timehop的GitHub上发表的一个控件框架,大家可以去参考它的【GitHub】。
这里先贴出GitHub上提供的效果图:
要使用这个框架,我们需要首先导入它的依赖:
compile 'com.timehop.stickyheadersrecyclerview:library:0.4.3'
compile 'com.android.support:recyclerview-v7:24.0.0'
这里需要说明的是,这个框架要求SDK的最小版本不能低于14;另外,这个框架在2016年4月之后就不再更新维护了。
下面让我们来看一下它的使用方式:
(1)在布局文件中,我们仍然使用RecyclerView:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/rv" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
(2)MainActivity中的代码如下:
package com.example.itgungnir.teststickyheadersrecyclerview; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import android.widget.Toast; import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersAdapter; import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersDecoration; import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersTouchListener; public class MainActivity extends AppCompatActivity { private RecyclerView rv; private AnimalsHeadersAdapter adapter; private StickyRecyclerHeadersDecoration headersDecor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rv = (RecyclerView) findViewById(R.id.rv); } @Override protected void onResume() { super.onResume(); initView(); initEvent(); } private void initView() { // 为RecyclerView设置适配器 adapter = new AnimalsHeadersAdapter(); adapter.addAll(getDummyDataSet()); rv.setAdapter(adapter); // 为RecyclerView添加LayoutManager final LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false); rv.setLayoutManager(layoutManager); // 为RecyclerView添加Decorator装饰器 // 为RecyclerView中的Item添加Header头部(自动获取头部ID,将相邻的ID相同的聚合到一起形成一个Header) headersDecor = new StickyRecyclerHeadersDecoration(adapter); rv.addItemDecoration(headersDecor); // 为RecyclerView添加Item之间的分隔线 rv.addItemDecoration(new DividerDecoration(this)); } private void initEvent() { // 为RecyclerView添加普通Item的点击事件(点击Header无效) rv.addOnItemTouchListener(new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() { @Override public void onItemClick(View view, int position) { Toast.makeText(MainActivity.this, adapter.getItem(position) + " Clicked", Toast.LENGTH_SHORT).show(); } })); // 为RecyclerView添加Header的点击事件 StickyRecyclerHeadersTouchListener touchListener = new StickyRecyclerHeadersTouchListener(rv, headersDecor); touchListener.setOnHeaderClickListener(new StickyRecyclerHeadersTouchListener.OnHeaderClickListener() { @Override public void onHeaderClick(View header, int position, final long headerId) { Toast.makeText(MainActivity.this, "Header position: " + position + ", id: " + headerId, Toast.LENGTH_SHORT).show(); } }); rv.addOnItemTouchListener(touchListener); } // 获取RecyclerView中展示的数据源 private String[] getDummyDataSet() { return getResources().getStringArray(R.array.animals); } // StickyHeadersRecyclerView的适配器类 private class AnimalsHeadersAdapter extends AnimalAdapter<RecyclerView.ViewHolder> implements StickyRecyclerHeadersAdapter<RecyclerView.ViewHolder> { @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listitem_normal, parent, false); return new RecyclerView.ViewHolder(view) { }; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { TextView textView = (TextView) holder.itemView; textView.setText(getItem(position)); } // 获取当前Item的首字母,按照首字母将相邻的Item聚集起来并添加统一的头部 @Override public long getHeaderId(int position) { return getItem(position).charAt(0); } // 获取头部布局 @Override public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listitem_header, parent, false); return new RecyclerView.ViewHolder(view) { }; } // 为头部布局中的控件绑定数据 @Override public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) { final TextView textView = (TextView) holder.itemView; textView.setText(String.valueOf(getItem(position).charAt(0))); } } }
(3)AnimalAdapter类中的代码:
import android.support.v7.widget.RecyclerView; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; /** * RecyclerView的适配器 */ public abstract class AnimalAdapter<VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> { private ArrayList<String> items = new ArrayList<String>(); public AnimalAdapter() { setHasStableIds(true); } public void add(String object) { items.add(object); notifyDataSetChanged(); } public void add(int index, String object) { items.add(index, object); notifyDataSetChanged(); } public void addAll(Collection<? extends String> collection) { if (collection != null) { items.addAll(collection); notifyDataSetChanged(); } } public void addAll(String... items) { addAll(Arrays.asList(items)); } public void clear() { items.clear(); notifyDataSetChanged(); } public void remove(String object) { items.remove(object); notifyDataSetChanged(); } public String getItem(int position) { return items.get(position); } @Override public long getItemId(int position) { return getItem(position).hashCode(); } @Override public int getItemCount() { return items.size(); } }
(4)RecyclerItemClickListener类中的代码:
import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View; public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener { private OnItemClickListener mListener; private GestureDetector mGestureDetector; public RecyclerItemClickListener(Context context, OnItemClickListener listener) { mListener = listener; mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() { @Override public boolean onSingleTapUp(MotionEvent e) { return true; } }); } @Override public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) { View childView = view.findChildViewUnder(e.getX(), e.getY()); if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) { mListener.onItemClick(childView, view.getChildAdapterPosition(childView)); } return false; } @Override public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { } @Override public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { // do nothing } public interface OnItemClickListener { void onItemClick(View view, int position); } }
posted on 2016-12-22 14:05 ITGungnir 阅读(8125) 评论(0) 编辑 收藏 举报