您好,登錄后才能下訂單哦!
小編給大家分享一下Android TV如何使用RecycleView實現(xiàn)橫向的Listview并響應(yīng)點擊事件,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
具體如下:
1.先貼出自己的效果圖(可橫向滾動,并響應(yīng)item點擊事件):
2.關(guān)于點擊事件的實現(xiàn)細節(jié)
核心:使用接口回調(diào)
在adapter中自己定義了個接口,然后在onBindViewHolder中去為holder.itemView去設(shè)置相應(yīng)的監(jiān)聽最后回調(diào)我們設(shè)置的監(jiān)聽。
class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder> { //... public interface OnItemClickLitener { void onItemClick(View view, int position); void onItemLongClick(View view , int position); } private OnItemClickLitener mOnItemClickLitener; public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) { this.mOnItemClickLitener = mOnItemClickLitener; } @Override public void onBindViewHolder(final MyViewHolder holder, final int position) { holder.tv.setText(mDatas.get(position)); // 如果設(shè)置了回調(diào),則設(shè)置點擊事件 if (mOnItemClickLitener != null) { holder.itemView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { int pos = holder.getLayoutPosition(); mOnItemClickLitener.onItemClick(holder.itemView, pos); } }); holder.itemView.setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) { int pos = holder.getLayoutPosition(); mOnItemClickLitener.onItemLongClick(holder.itemView, pos); return false; } }); } } //... }
在activity設(shè)置監(jiān)聽,并實現(xiàn)接口
mAdapter.setOnItemClickLitener(new OnItemClickLitener() { @Override public void onItemClick(View view, int position) { Toast.makeText(HomeActivity.this, position + " click", Toast.LENGTH_SHORT).show(); } @Override public void onItemLongClick(View view, int position) { Toast.makeText(HomeActivity.this, position + " long click", Toast.LENGTH_SHORT).show(); mAdapter.removeData(position); } });
3.實現(xiàn)橫向效果
核心:LinearLayoutManager
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext,LinearLayoutManager.HORIZONTAL,false); mRecyclerView.setLayoutManager(linearLayoutManager);
4. 讓RecyclerView的子item獲得焦點
mRecyclerView.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if(hasFocus){ if(mRecyclerView.getChildCount()>0){ mRecyclerView.getChildAt(0).requestFocus(); } } } });
5.代碼實現(xiàn):
//定義自己的布局文件 public class EasyLayout extends LinearLayout{ private MyAdapter mAdapter; private RecyclerView mRecyclerView; private Context mContext; private List<String> mDatas; public EasyLayout(Context context) { super(context); mDatas = new ArrayList<String>(); for (int i=0;i<20;i++){ String str = "圖片:"+i; mDatas.add(str); } this.setBackgroundColor(Color.parseColor("#407EAf")); this.mContext = context; this.setOrientation(VERTICAL); createLayout(); } private void createLayout() { //空出上邊留白 TextView te = new TextView(mContext); ViewGroup.LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,160); this.addView(te,lp); mRecyclerView = new RecyclerView(mContext); lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT); this.addView(mRecyclerView,lp); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext,LinearLayoutManager.HORIZONTAL,false); mRecyclerView.setLayoutManager(linearLayoutManager); //設(shè)置適配器 mAdapter = new MyAdapter( mContext,mDatas); mRecyclerView.setAdapter(mAdapter); //監(jiān)聽點擊事件 mAdapter.setmOnItemClickLitener(new MyAdapter.OnItemClickLitener() { @Override public void onItemClick(View view, int position) { Toast.makeText(mContext,"點擊了圖片:"+position,Toast.LENGTH_SHORT).show(); } }); //讓RecyclerView的子item獲得焦點 mRecyclerView.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if(hasFocus){ if(mRecyclerView.getChildCount()>0){ mRecyclerView.getChildAt(0).requestFocus(); } } } }); }
//定義適配器類 public static class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { private Context mContext; private List<String> mDatas; private LayoutInflater mInflater; private OnItemClickLitener mOnItemClickLitener; //定義點擊事件的接口 public interface OnItemClickLitener { void onItemClick(View view, int position); } public MyAdapter( Context mContext,List<String> datats) { this.mContext = mContext; this.mDatas = datats; mInflater = LayoutInflater.from(this.mContext); } public void setmOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) { this.mOnItemClickLitener = mOnItemClickLitener; } @Override public int getItemCount() { return mDatas.size(); } /** * 創(chuàng)建ViewHolder */ @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int i) { View view = mInflater.inflate(R.layout.easy_item,null); ViewHolder viewHolder = new ViewHolder(view); return viewHolder; } /** * 設(shè)置值 */ @Override public void onBindViewHolder(final ViewHolder viewHolder, final int position) { viewHolder.btn.setText(mDatas.get(position)); viewHolder.btn.setTextSize(40); viewHolder.btn.setGravity(Gravity.CENTER); //設(shè)置回調(diào) if (mOnItemClickLitener != null){ viewHolder.btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { int pos = viewHolder.getLayoutPosition(); mOnItemClickLitener.onItemClick( viewHolder.btn,pos); } }); } } public class ViewHolder extends RecyclerView.ViewHolder //我們需要inflate的item布局需要傳入 { Button btn; public ViewHolder(View arg0) { super(arg0); btn = (Button) arg0.findViewById(R.id.btn); } } }
//在activity加載自己的布局 public class GallarRecyActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); EasyLayout easyLayout = new EasyLayout(this); setContentView(easyLayout); } }
easy_item.xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/btn" android:layout_width="320dp" android:layout_height="430dp" /> </LinearLayout>
以上是“Android TV如何使用RecycleView實現(xiàn)橫向的Listview并響應(yīng)點擊事件”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。