Android为TV端助力 listview与recyclerview上下联动

首先是主布局fragment里面的xml文件

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_reservation"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@drawable/new_main_bg"    tools:context="com.hhzt.iptv.lvb_w8.activity.ReservationActivity">

<LinearLayout        android:id="@+id/layout"        android:layout_width="@dimen/layx150"        android:layout_height="match_parent"        android:orientation="vertical"        >        <TextView            android:id="@+id/title_name"            android:layout_width="match_parent"            android:layout_height="@dimen/layx80"            android:textSize="@dimen/layx30"            android:textColor="@color/white"            android:singleLine="true"            android:layout_gravity="center"            android:gravity="center"            android:background="@color/food_title"            />

<ListView            android:id="@+id/listview_left"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:cacheColorHint="#00000000"            android:divider="@color/bian_white"            android:dividerHeight="@dimen/layx1"            android:drawSelectorOnTop="false"            android:fastScrollEnabled="true"            android:focusable="true"            android:background="@color/food_content"            android:listSelector="@drawable/food_menu_selector"            android:descendantFocusability="blocksDescendants"            android:scrollbars="none" >        </ListView>

</LinearLayout>

<LinearLayout        android:id="@+id/layout_ll"        android:layout_width="@dimen/layx400"        android:layout_height="@dimen/layx50"        android:orientation="horizontal"        android:layout_alignParentRight="true"        android:layout_marginRight="@dimen/layx50"        android:layout_marginTop="@dimen/layx50"        android:gravity="center"        >

<LinearLayout            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="match_parent"            android:orientation="horizontal"            android:layout_gravity="center"            android:gravity="center"            android:focusable="true"            android:clickable="true"            android:paddingLeft="@dimen/layx10"            android:paddingRight="@dimen/layx10"            android:background="@drawable/shopping_selector"            >            <ImageView                android:layout_width="0dp"                android:layout_weight="1"                android:layout_height="match_parent"                android:scaleType="fitXY"                android:padding="@dimen/layx5"                android:src="@drawable/shopping"                />            <TextView                android:id="@+id/shopping"                android:layout_width="0dp"                android:layout_weight="2"                android:layout_height="match_parent"                android:layout_gravity="center"                android:gravity="center"                android:singleLine="true"                android:ellipsize="marquee"                android:textColor="@color/food_d_text"                android:textSize="@dimen/layx28"                android:text="购物车"                />

</LinearLayout>

<LinearLayout            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="match_parent"            android:orientation="horizontal"            android:layout_gravity="center"            android:gravity="center"            android:focusable="true"            android:clickable="true"            android:paddingLeft="@dimen/layx10"            android:paddingRight="@dimen/layx10"            android:layout_marginLeft="@dimen/layx20"            android:background="@drawable/shopping_selector"            >            <ImageView                android:layout_width="0dp"                android:layout_weight="1"                android:layout_height="match_parent"                android:scaleType="fitXY"                android:padding="@dimen/layx5"                android:src="@drawable/history"                />            <TextView                android:id="@+id/history"                android:layout_width="0dp"                android:layout_weight="3"                android:layout_height="match_parent"                android:layout_gravity="center"                android:gravity="center"                android:singleLine="true"                android:ellipsize="marquee"                android:textColor="@color/food_d_text"                android:textSize="@dimen/layx28"                android:text="历史订单"                />        </LinearLayout>

</LinearLayout>

<android.support.v7.widget.RecyclerView        android:id="@+id/listview_right"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_toRightOf="@id/layout"        android:layout_below="@id/layout_ll"        android:paddingLeft="@dimen/layx20"        android:paddingRight="@dimen/layx20"        android:layout_marginTop="@dimen/layx20"        android:nextFocusUp="@+id/shopping"        android:scrollbars="none"/></RelativeLayout>

然后是activity上面的fragment
package com.hhzt.iptv.lvb_w8.fragment;

import android.os.Bundle;import android.support.v7.widget.GridLayoutManager;import android.support.v7.widget.RecyclerView;import android.view.KeyEvent;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ListView;import android.widget.TextView;

import com.hhzt.iptv.R;import com.hhzt.iptv.lvb_w8.adapter.FoodAdapter;import com.hhzt.iptv.lvb_w8.adapter.FoodAdapterRight;import com.hhzt.iptv.lvb_w8.bean.FoodBean;import com.hhzt.iptv.lvb_w8.bean.FoodDetailsBean;import com.hhzt.iptv.lvb_w8.interfaces.IBeanOnSuccessCB;import com.hhzt.iptv.lvb_y.BaseActivity;import com.hhzt.iptv.lvb_y.business.UIDataller;import com.hhzt.iptv.lvb_y.log.LogUtil;import com.lidroid.xutils.ViewUtils;import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;

//订餐服务界面public class ReservationFragment extends BaseFragment implements FoodAdapterRight.OnItemClickListener{

@ViewInject(R.id.title_name)   private TextView mWelcomeTextView; //标题   @ViewInject(R.id.listview_left)   private ListView mListview_left; //列表list   @ViewInject(R.id.shopping)   private TextView mShopping; //购物车   @ViewInject(R.id.history)   private TextView mHistory; //历史订单   @ViewInject(R.id.listview_right)   private RecyclerView mListViewRight; //菜品详情RecyclerView   private ArrayList<FoodBean.ListBean> mBean;   //详情页的list   private ArrayList<FoodDetailsBean.ListBean> mFoodDetailsBean = new ArrayList<>();

private FoodAdapterRight mAdapterRight;   private String mCurTitle = "";   private int mTitlePos = 0;

@Override   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {      View view = inflater.inflate(R.layout.activity_reservation, container, false);      ViewUtils.inject(this, view);      return view;   }

@Override   public void onActivityCreated(Bundle savedInstanceState) {      super.onActivityCreated(savedInstanceState);      if (null == savedInstanceState) {         requseFoodType();         setMonitor();      }   }

private void setMonitor() {      mListview_left.setOnKeyListener(new View.OnKeyListener() {         @Override         public boolean onKey(View v, int keyCode, KeyEvent event) {            switch (keyCode){               case KeyEvent.KEYCODE_DPAD_UP:                  if(event.getAction() == KeyEvent.ACTION_DOWN){                     if(mTitlePos > 0){                        mTitlePos --;                        mListview_left.setSelection(mTitlePos);                        requseFoodDetailedMenu(mTitlePos,mBean.get(mTitlePos).getId(),1,1000);                     }                  }                  break;               case KeyEvent.KEYCODE_DPAD_DOWN:                  if(event.getAction() == KeyEvent.ACTION_DOWN){                     if(mTitlePos < mBean.size()-1){                        mTitlePos ++;                        mListview_left.setSelection(mTitlePos);                        requseFoodDetailedMenu(mTitlePos,mBean.get(mTitlePos).getId(),1,1000);                     }                  }                  break;            }

return false;         }      });

}

/**    * 请求右侧详细数据    */   private void requseFoodDetailedMenu(final int pos,int id,int pum,int size){      UIDataller.getDataller().gotoRequseFoodDetailedMenu(id, pum, size, new IBeanOnSuccessCB<ArrayList<FoodDetailsBean.ListBean>>() {         @Override         public void onSuccess(ArrayList<FoodDetailsBean.ListBean> bean) {            if(pos != mTitlePos){               return;            }            mFoodDetailsBean.clear();            mFoodDetailsBean = bean;            initRightAdapter();         }      });

}

/**    * 初始化右侧数据    */   private void initRightAdapter() {      if(mAdapterRight == null){         GridLayoutManager linearLayoutManager = new GridLayoutManager(getActivity(),4);         mListViewRight.setLayoutManager(linearLayoutManager);         LogUtil.i("TAG","mListViewRight:"+mListViewRight.getWidth());         mAdapterRight = new FoodAdapterRight(getActivity(), mFoodDetailsBean, mListViewRight);         mAdapterRight.setOnKey(this);         mListViewRight.setAdapter(mAdapterRight);      }else{         mAdapterRight.setDataList(mFoodDetailsBean);         mListViewRight.setAdapter(mAdapterRight);      }

}

/**    * 请求左侧分类列表    */   private void requseFoodType() {      UIDataller.getDataller().gotoRequseFoodType(new IBeanOnSuccessCB<FoodBean>() {         @Override         public void onSuccess(FoodBean bean) {            mWelcomeTextView.setText(bean.getClassName());            if(bean.getList() != null && bean.getList().size()>0){               mBean = bean.getList();               initTitleType();               requseFoodDetailedMenu(0,mBean.get(0).getId(),1,1000);            }         }      });   }

/**    * 初始化左边分类并设置值    */   private void initTitleType() {

FoodAdapter adapter = new FoodAdapter(getActivity(),mBean);      mListview_left.setAdapter(adapter);   }

@Override   public void onItemClick(View view, int position) {      BaseActivity.getInstance().showToast("单击:"+position,0);   }

@Override   public void onKey(int keykode, View view, int position) {      switch (keykode){         case KeyEvent.KEYCODE_DPAD_LEFT:            mListview_left.setFocusable(true);            mListview_left.requestFocus();            mListview_left.setSelection(mTitlePos);            break;      }   }}
左侧listview的adapter
package com.hhzt.iptv.lvb_w8.adapter;

import android.content.Context;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Button;

import com.hhzt.iptv.R;import com.hhzt.iptv.lvb_w8.bean.FoodBean;import com.hhzt.iptv.lvb_y.utils.StringUtil;

import java.util.ArrayList;

/** * Created by Administrator on 2017-12-09. */

public class FoodAdapter extends BaseAdapter {

private static final String TAG = FoodAdapter.class.getSimpleName();    private Context mContext;    private ArrayList<FoodBean.ListBean> mDataList = new ArrayList<>();    private int mSelection = 0;

public FoodAdapter(Context mContext, ArrayList<FoodBean.ListBean> mDataList) {        this.mContext = mContext;        this.mDataList = mDataList;    }

@Override    public int getCount() {        if (null != mDataList)            return mDataList.size();        return 0;    }

@Override    public Object getItem(int i) {        if (null != mDataList)            return mDataList.get(i);        return null;    }

@Override    public long getItemId(int i) {        return 0;    }

@Override    public View getView(int position, View view, ViewGroup viewGroup) {        ViewHolder viewHolder = null;        if (null == view){            viewHolder = new ViewHolder();            view = LayoutInflater.from(mContext).inflate(R.layout.item_left, null);            viewHolder.textContent = (Button) view.findViewById(R.id.text_content);            view.setTag(viewHolder);        }else {            viewHolder = (ViewHolder) view.getTag();        }        if (null != viewHolder.textContent && null != mDataList && mDataList.size()>0){            viewHolder.textContent.setText(StringUtil.setText(mDataList.get(position).getName()));

}else {            Log.i(TAG, "getView: null == mDataList");        }        return view;    }

public int getSelection() {        return mSelection;    }

public void setSelection(int selection) {        mSelection = selection;        notifyDataSetChanged();    }

class ViewHolder{        Button textContent;    }}adapter里面的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">

<Button        android:id="@+id/text_content"        android:layout_width="match_parent"        android:layout_height="@dimen/layx60"        android:textColor="@color/bottom_city_color"        android:gravity="center"        android:layout_gravity="center"        android:background="@null"        android:textSize="@dimen/layx24"/></LinearLayout>
RecyclerView的adapter适配器
package com.hhzt.iptv.lvb_w8.adapter;

import android.content.Context;import android.support.v7.widget.RecyclerView;import android.util.Log;import android.view.KeyEvent;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.Button;import android.widget.ImageView;import android.widget.TextView;

import com.bumptech.glide.Glide;import com.hhzt.iptv.R;import com.hhzt.iptv.lvb_w8.bean.FoodDetailsBean;

import java.util.ArrayList;

/** * Created by zyf on 2017/5/8. * 右边RecyclerView适配器 */

public class FoodAdapterRight extends RecyclerView.Adapter<FoodAdapterRight.ViewHolder> {

private static final String TAG = FoodAdapterRight.class.getSimpleName();    private final LayoutInflater mInflater;    private final int w;    private final int h;    private Context mContext;    private ArrayList<FoodDetailsBean.ListBean> mDataList = new ArrayList<>();    private RecyclerView mRecyclerView;    private OnItemClickListener listener;

public FoodAdapterRight(Context context, ArrayList<FoodDetailsBean.ListBean> dataList , RecyclerView recyclerView) {        mContext = context;        mDataList = dataList;        mRecyclerView = recyclerView;        w = mRecyclerView.getWidth();        h = mRecyclerView.getHeight();        mInflater = LayoutInflater.from(context);    }

@Override    public FoodAdapterRight.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {        View view =  mInflater.inflate(R.layout.item_right,parent,false);

return new ViewHolder(view);    }

@Override    public void onBindViewHolder(FoodAdapterRight.ViewHolder holder, final int position) {        if (null != mDataList && mDataList.size() > 0) {            ViewGroup.LayoutParams parms = holder.itemView.getLayoutParams();            parms.width = w/4-20;            parms.height = h/2-20;            holder.itemView.setLayoutParams(parms);            holder.item_name.setText(mDataList.get(position).getName());            holder.item_price.setText("¥"+mDataList.get(position).getPrice());            Glide.with(mContext).load(mDataList.get(position).getPictureUrl()).into(holder.item_image);            holder.item_btn.setOnKeyListener(new View.OnKeyListener() {                @Override                public boolean onKey(View v, int keyCode, KeyEvent event) {                    switch (keyCode){                        case KeyEvent.KEYCODE_DPAD_LEFT:                            if(event.getAction() ==KeyEvent.ACTION_DOWN){                                if(position%4==0){                                    listener.onKey(KeyEvent.KEYCODE_DPAD_LEFT,v,position);                                    return true;                                }                            }                            break;                        case KeyEvent.KEYCODE_DPAD_CENTER:                        case KeyEvent.KEYCODE_ENTER:                            if(event.getAction() ==KeyEvent.ACTION_DOWN){                                listener.onItemClick(v,position);                                return true;                            }                            break;                    }                    return false;                }            });        } else {            Log.i(TAG, "getView: null == mDataList");        }    }

@Override    public int getItemCount() {        return mDataList.size();    }

class ViewHolder extends RecyclerView.ViewHolder {        Button item_btn;        ImageView item_image;        TextView item_name;        TextView item_price;        TextView select_share;        ViewHolder(View itemView) {            super(itemView);            item_btn = (Button) itemView.findViewById(R.id.item_btn);            item_image = (ImageView) itemView.findViewById(R.id.item_image);            item_name = (TextView) itemView.findViewById(R.id.item_name);            item_price = (TextView) itemView.findViewById(R.id.item_price);            select_share = (TextView) itemView.findViewById(R.id.select_share);        }    }

public void setDataList( ArrayList<FoodDetailsBean.ListBean> dataList){        mDataList.clear();        mDataList = dataList;        notifyDataSetChanged();    }    public  interface OnItemClickListener {        void onItemClick(View view , int position);        void onKey(int keykode,View view , int position);    }

public void setOnKey(OnItemClickListener listener){        this.listener = listener;    }

}适配器里面的xml文件
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/item__view_container"    android:layout_width="wrap_content"    android:descendantFocusability="beforeDescendants"    android:layout_height="wrap_content">

<Button        android:id="@+id/item_btn"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="@drawable/vod_item_bg_selector" />

<LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:padding="@dimen/layx7"        android:orientation="vertical"        >        <ImageView            android:id="@+id/item_image"            android:layout_width="match_parent"            android:layout_height="0dp"            android:layout_weight="3"            android:scaleType="fitXY" />

<RelativeLayout            android:layout_width="match_parent"            android:layout_height="0dp"            android:background="@color/white"            android:paddingLeft="@dimen/layx20"            android:paddingRight="@dimen/layx10"            android:layout_weight="1">            <TextView                android:id="@+id/item_price"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_alignParentBottom="true"                android:ellipsize="marquee"                android:gravity="center_vertical|left"                android:paddingBottom="@dimen/layx5"                android:paddingTop="@dimen/layx5"                android:singleLine="true"                android:textColor="@color/red"                android:textSize="@dimen/layx20" />

<TextView                android:id="@+id/item_name"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_above="@id/item_price"                android:ellipsize="marquee"                android:gravity="center_vertical|left"                android:paddingBottom="@dimen/layx5"                android:paddingTop="@dimen/layx5"                android:singleLine="true"                android:textColor="@color/black"                android:textSize="@dimen/layx20" />

<TextView                android:id="@+id/select_share"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:ellipsize="marquee"                android:gravity="bottom|center_horizontal"                android:layout_alignParentBottom="true"                android:layout_alignParentRight="true"                android:paddingBottom="@dimen/layx5"                android:paddingTop="@dimen/layx5"                android:singleLine="true"                android:textColor="@color/red"                android:textSize="@dimen/layx20" />        </RelativeLayout>    </LinearLayout></RelativeLayout>

上述里面的button的背景选择器都没有上传,因为带有图片
 
 
				
时间: 2024-10-29 10:09:00

Android为TV端助力 listview与recyclerview上下联动的相关文章

Android为TV端助力 外挂字幕(设置颜色,大小,位置,微调字幕)

前提摘要:  可以给电影加字幕,目前支持srt和ass格式, 功能摘要:  支持微调字幕,设置大小,颜色,位置 1 .字幕解析类 package com.hhzt.iptv.lvb_x.utils; import android.os.Handler;import android.util.Log; import com.hhzt.iptv.lvb_x.Constant;import com.hhzt.iptv.lvb_x.log.LogUtil;import com.hhzt.iptv.lvb

Android为TV端助力 Linux命令查看包名类名

先运行apk 再输入logcat | grep START 查看当前启动apk的包名和类名 adb shell "pm list packages -f | grep com.yulong.android.launcher3" adb shell "pm list packages -f | grep com."显示包名的apk名称 原文地址:https://www.cnblogs.com/xiaoxiaing/p/8241873.html

Android为TV端助力:自定义view之太阳

先看效果图 package com.hhzt.iptv.lvb_w8.view; import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.PixelFormat;import android.graphics.PorterDuff;import android.o

Android为TV端助力:(转载)修改TextView字体样式

一.开篇 因为 Android 字体相关的内容还比较多的.有时候其实我们只需要调整一下属性就可以满足设计师的需求,或者是一个退后的方案(毕竟有发版的时间卡住了),有一些效果可以大概满足需求. 那么本文就先介绍一下再 Android 下原生内置的一些字体的一些基本概念和使用. 需要注意的是,内置的一些字体只对英文有效. 二.Android 的默认字体 Android 系统默认使用的是一款叫做 Robote 的字体.Robote 本身就是 Google 自己的字体格式,Android 和 Chrom

Android为TV端助力之解决setOnItemSelectedListener一进来就自动执行一次的问题

我们经常会遇到listview或者其他view设置setOnItemSelectedListener监听时,一加载界面,setOnItemSelectedListener监听就会自动执行一遍,导致你第一次移动上去的时候setOnItemSelectedListener监听就不会走的 所以我们采用自定义listview,主动来调用setOnItemSelectedListener监听回调,代码如下 public class MyListView extends ListView { private

Android为TV端助力 使用shared注意事项

不要存放大的key和value!我就不重复三遍了,会引起界面卡.频繁GC.占用内存等等,好自为之! 毫不相关的配置项就不要丢在一起了!文件越大读取越慢,不知不觉就被猪队友给坑了:蓝后,放进defalut的那个简直就是愚蠢行为! 读取频繁的key和不易变动的key尽量不要放在一起,影响速度.(如果整个文件很小,那么忽略吧,为了这点性能添加维护成本得不偿失) 不要乱edit和apply,尽量批量修改一次提交! 尽量不要存放JSON和HTML,这种场景请直接使用json! 不要指望用这货进行跨进程通信

Android长截屏-- ScrollView,ListView及RecyclerView截屏

http://blog.csdn.net/wbwjx/article/details/46674157       Android长截屏-- ScrollView,ListView及RecyclerView截屏 https://github.com/BoBoMEe/AndroidDev/blob/master/common/common/src/main/java/com/bobomee/android/common/util/ScreenUtil.java /* * Copyright (C)

Android添加图片到ListView或者 RecyclerView显示

先上图 点击+号就去选择图片 实际上这个添加本身就是一个ListView或者 RecyclerView 只是布局有些特殊 item <?xml version="1.0" encoding="utf-8"?> <liu.myrecyleviewchoosephoto.view.SquareRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android&quo

android listview 替代品recyclerview详解

安卓v7支持包下的ListView替代品————RecyclerView RecyclerView这个控件也出来很久了,相信大家也学习的差不多了,如果还没学习的,或许我可以带领大家体验一把这个艺术般的控件. 据官方介绍,该控件是属于之间用的非常多的ListView和GridView的替代品,既然能替代用的如此普遍的它们,这自然有其该有的优势. 1)相对于ListView而言RecyclerView的优势体现在: ①封装了之前ListView的优化,封装了之前ViewHolder的复用,这样在自定