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的背景选择器都没有上传,因为带有图片
Android为TV端助力 listview与recyclerview上下联动的更多相关文章
- Android为TV端助力listview 非常重要的几个属性
首先是stackFromBottom属性,这只该属性之后你做好的列表就会显示你列表的最下面,值为true和false Android:stackFromBottom="true" ...
- Android为TV端助力 转载:RecyclerView分页加载
package com.android.ryane.pulltoloaddata_recyclerview; import android.os.Handler;import android.os.L ...
- Android为TV端助力 布局、绘制、内存泄露、响应速度、listview和bitmap、线程优化以及一些优化的建议!
1.布局优化 首先删除布局中无用的控件和层级,其次有选择地使用性能较低的viewgroup,比如布局中既可以使用RelativeLayout和LinearLayout,那我们就采用LinearLayo ...
- Android 为TV端助力之解决ViewPager嵌套RecyclerView水平滑动问题
public class MyViewPager extends ViewPager { private RecyclerView recyclerView; public MyViewPager(@ ...
- Android为TV端助力:RecyclerView更新数据时焦点丢失
1.adapter的setHasStableIds设置成true 2.重写adapter的getItemId方法 @Override public long getItemId(int positio ...
- Android为TV端助力(转载)
作者地址http://www.jianshu.com/u/63915ef020e2 针对Android Tv的自定义RecyclerView 作者 wenju_song 关注 2016.12.09 1 ...
- Android为TV端助力 不需要Socket的跨进程推送消息AIDL!
上篇介绍了跨进程实时通讯http://www.cnblogs.com/xiaoxiaing/p/5818161.html 但是他有个缺点就是服务端无法推送消息给客户端,今天这篇文章主要说的就是服务器推 ...
- Android为TV端助力 关于Fragment你所需知道的一切!
转载自刘明渊 的博客地址:http://blog.csdn.net/vanpersie_9987 Fragment 是 Android API 中的一个类,它代表Activity中的一部分界面:您可以 ...
- Android为TV端助力 apk静默安装
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/47803149 之前有很多朋友都问过我,在Android系统中怎样才能实现静默安装呢 ...
随机推荐
- [开源]MasterChief 快速开发辅助类库
C# 开发辅助类库,和士官长一样身经百战且越战越勇的战争机器,能力无人能出其右. GitHub:MasterChief 欢迎Star,欢迎Issues: 项目架构思维导图: 目录 1. 数据库访问 2 ...
- C#采用vony.Html.AIO插件批量爬MM网站图片
一.创建项目 1.创建一个.netframework的控制台项目命名为Crawler 2.安装nuget包搜索名称Ivony.Html.AIO,使用该类库什么方便类似jqury的选择器可以根据类名或者 ...
- Java PDF页面设置——页面大小、页边距、纸张方向、页面旋转
下面的示例将介绍通过Java编程来对PDF页面进行个性化设置的方法,包括设置页面大小.页边距.纸张方向.页面旋转等.这里有如下多种页面大小尺寸可供选择: 同时,设置文档内容旋转时,可支持如下角度进行内 ...
- pytest框架之命令行参数1
前言 pytest是一款强大的python自动化测试工具,可以胜任各种类型或者级别的软件测试工作.pytest提供了丰富的功能,包括assert重写,第三方插件,以及其他测试工具无法比拟的fixtur ...
- 逆向-攻防世界-crackme
查壳,nSpack壳,直接用软件脱壳,IDA载入程序. 很明显,就是将402130的数据和输入的数据进行异或,判断是否等于402150处的数据.dwrd占4字节. 这道题主要记录一下刚学到的,直接在I ...
- 在word中如何美观地插入代码
打开这个网站 http://www.planetb.ca/syntax-highlight-word 进去后我们看到下面的界面 中间的空白文本框,可以插入代码,下面可以选择代码种类,最后点击Show ...
- ASP.NET Core 身份验证(一)
前言 这篇文章我想带领大家了解一下 ASP.NET Core 中如何进行的身份验证,在开始之前强烈建议还没看过我写的 Identity 系列文章的同学先看一下. Identity 入门系列文章: Id ...
- Linux下ps -ef和ps aux的区别
Linux下显示系统进程的命令ps,最常用的有ps -ef 和ps aux.这两个到底有什么区别呢?两者没太大差别,讨论这个问题,要追溯到Unix系统中的两种风格,System V风格和BSD 风格, ...
- Servlet+JSP及Tomcat常见面试题(面试必备)
1. 什么是servlet? servlet是用来处理客户端请求并产生动态网页内容的java类 2. Tomcat的缺省端口是多少,怎么修改? a) 默认端口号是8080 b) ...
- RocketMQ4.4.0新特性分享
rocketmq1.架构 MQ历史 由数据结构队列发展而来 MQ使用场景 异步处理 解耦 削峰填谷 数据同步2.队列3.使用 生产 同步(sync) 默认重试2次总共3次 默认等待超时时间为3s 异步 ...